#include <cstdio> using namespace std; long long int fib[45]; int left[100]; int right[100]; int used = 0; // Function for extended Euclidean Algorithm long long int gcdExtended(long long int a, long long int b, long long int *x, long long int *y) { // Base Case if (a == 0) { *x = 0; *y = 1; return b; } long long int x1, y1; // To store results of recursive call long long int gcd = gcdExtended(b%a, a, &x1, &y1); // Update x and y using results of // recursive call *x = y1 - (b/a) * x1; *y = x1; return gcd; } void build_graph(int root, int ds) { if(left[root] > 0) { // musze dolozyc nowy // printf("Dokladam zrodlo, dodaje krawedz %d->%d\n", root, used); right[root] = used; used++; build_graph(right[root], ds); } else { int prev = root; int prevprev; for(int i=1;i<ds;i++) { left[prev] = used; // printf("dodaje lewa krawedz: %d->%d\n", prev, used); if (i>1) { right[prevprev] = used; // printf("dodaje prawa krawedz: %d->%d\n", prevprev, used); } prevprev=prev; prev=used; used++; } } } void solve(int k, int root) { // printf("tuti %d %d\n", k, root); // jezli k jest liczba fib if(k == 1) { right[root] = 1000; // printf("zamykam %d\n", root); return; } for (int i = 0; i<45; i++) { if (fib[i] == k) { build_graph(root, i+1); return; } } if(k == 4) { left[root] = used; right[root] = used + 1; left[used] = used + 1; left[used+1] = used + 2; right[used+1] = used + 3; left[used+2] = used +3; used += 4; return; } if(k == 6) { left[root] = used; right[root] = used + 1; left[used] = used + 1; left[used+1] = used + 2; right[used+1] = used + 3; left[used+2] = used +3; right[used+2] = used+4; left[used+3] = used+4; used += 5; return; } for(int i = 2; i<42;i++) { if(fib[i+1] > k) continue; long long int x,y; gcdExtended(fib[i], fib[i+1], &x, &y); x*=k; y*=k; // x0 = x + fib[i+1]*t > 0 // y0 = y - fib[i]*t > 0 if(-1*x/fib[i+1]+1 < y/fib[i]) { int t = -1*x/fib[i+1]+1; x = x + t*fib[i+1]; y = y - t*fib[i]; // printf("%lld * [%lld] + %lld * [%lld] = %d\n", x, fib[i], y, fib[i+1], k); // printf("%lld*%lld + %lld*%lld\n", x, fib[i], y,fib[i+1]); // printf("Buduje graf z korzenia do fib[%d], czyli %lld\n", i+1, fib[i+1]); build_graph(root, i+2); int new_root = used; // printf("Rozwiązuje z wezla %d dajacy %lld krawedzi\n", new_root-2, x); solve(x, new_root-2); // printf("Rozwiązuje z wezla %d dajacy %lld krawedzi\n", new_root-1, y); solve(y, new_root-1); break; } } } int main(int argc, char const *argv[]) { int k; scanf("%d", &k); fib[0] = fib[1]= 1; for (int i = 2; i < 50; ++i) { fib[i] = fib[i-1] + fib[i-2]; // printf("%d %lld\n", i, fib[i]); } for(int i=0;i<100;i++) { left[i] = right[i] = -1; } used=1; // root if(k <= 3) { build_graph(0, k+1); } else{ if(k % 2 == 0) solve(k, 0); else { left[0] = 1; right[0] = 1000; used=2; solve(k-1,1); } } printf("%d\n", used+1); for(int i=0;i<used;i++) { printf("%d %d\n", left[i] == -1 ? used +1 : left[i] + 1, right[i] == 1000 ? used+1 : right[i] == -1 ? -1 : right[i] + 1); // // if( left[i] == -1 && right[i]== -1) { // printf("%d: %d %d\n", i+1, used+1, -1); // } else if(right[i] == -1) { // printf("%d -1\n", left[i]+1) // } else // printf("%d: %d %d\n", i+1, left[i]+1, right[i]+1); // } } printf("-1 -1 \n"); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | #include <cstdio> using namespace std; long long int fib[45]; int left[100]; int right[100]; int used = 0; // Function for extended Euclidean Algorithm long long int gcdExtended(long long int a, long long int b, long long int *x, long long int *y) { // Base Case if (a == 0) { *x = 0; *y = 1; return b; } long long int x1, y1; // To store results of recursive call long long int gcd = gcdExtended(b%a, a, &x1, &y1); // Update x and y using results of // recursive call *x = y1 - (b/a) * x1; *y = x1; return gcd; } void build_graph(int root, int ds) { if(left[root] > 0) { // musze dolozyc nowy // printf("Dokladam zrodlo, dodaje krawedz %d->%d\n", root, used); right[root] = used; used++; build_graph(right[root], ds); } else { int prev = root; int prevprev; for(int i=1;i<ds;i++) { left[prev] = used; // printf("dodaje lewa krawedz: %d->%d\n", prev, used); if (i>1) { right[prevprev] = used; // printf("dodaje prawa krawedz: %d->%d\n", prevprev, used); } prevprev=prev; prev=used; used++; } } } void solve(int k, int root) { // printf("tuti %d %d\n", k, root); // jezli k jest liczba fib if(k == 1) { right[root] = 1000; // printf("zamykam %d\n", root); return; } for (int i = 0; i<45; i++) { if (fib[i] == k) { build_graph(root, i+1); return; } } if(k == 4) { left[root] = used; right[root] = used + 1; left[used] = used + 1; left[used+1] = used + 2; right[used+1] = used + 3; left[used+2] = used +3; used += 4; return; } if(k == 6) { left[root] = used; right[root] = used + 1; left[used] = used + 1; left[used+1] = used + 2; right[used+1] = used + 3; left[used+2] = used +3; right[used+2] = used+4; left[used+3] = used+4; used += 5; return; } for(int i = 2; i<42;i++) { if(fib[i+1] > k) continue; long long int x,y; gcdExtended(fib[i], fib[i+1], &x, &y); x*=k; y*=k; // x0 = x + fib[i+1]*t > 0 // y0 = y - fib[i]*t > 0 if(-1*x/fib[i+1]+1 < y/fib[i]) { int t = -1*x/fib[i+1]+1; x = x + t*fib[i+1]; y = y - t*fib[i]; // printf("%lld * [%lld] + %lld * [%lld] = %d\n", x, fib[i], y, fib[i+1], k); // printf("%lld*%lld + %lld*%lld\n", x, fib[i], y,fib[i+1]); // printf("Buduje graf z korzenia do fib[%d], czyli %lld\n", i+1, fib[i+1]); build_graph(root, i+2); int new_root = used; // printf("Rozwiązuje z wezla %d dajacy %lld krawedzi\n", new_root-2, x); solve(x, new_root-2); // printf("Rozwiązuje z wezla %d dajacy %lld krawedzi\n", new_root-1, y); solve(y, new_root-1); break; } } } int main(int argc, char const *argv[]) { int k; scanf("%d", &k); fib[0] = fib[1]= 1; for (int i = 2; i < 50; ++i) { fib[i] = fib[i-1] + fib[i-2]; // printf("%d %lld\n", i, fib[i]); } for(int i=0;i<100;i++) { left[i] = right[i] = -1; } used=1; // root if(k <= 3) { build_graph(0, k+1); } else{ if(k % 2 == 0) solve(k, 0); else { left[0] = 1; right[0] = 1000; used=2; solve(k-1,1); } } printf("%d\n", used+1); for(int i=0;i<used;i++) { printf("%d %d\n", left[i] == -1 ? used +1 : left[i] + 1, right[i] == 1000 ? used+1 : right[i] == -1 ? -1 : right[i] + 1); // // if( left[i] == -1 && right[i]== -1) { // printf("%d: %d %d\n", i+1, used+1, -1); // } else if(right[i] == -1) { // printf("%d -1\n", left[i]+1) // } else // printf("%d: %d %d\n", i+1, left[i]+1, right[i]+1); // } } printf("-1 -1 \n"); return 0; } |