#include<bits/stdc++.h> using namespace std; int tab[109][3]; int main() { int k,a=3,b=2,c=3,d=1; scanf("%d", &k); if(k == 1) { printf("2\n2 -1\n-1 -1\n"); return 0; } tab[1][1] = 2; tab[1][2] = 3; for(int i=1; i<=32; i++) { if(k & d) { tab[b][1] = -2; tab[b][2] = a+1; a++; b = a; k -= d; } if(2 * d == k) { a++; tab[b][1] = tab[c][1] = a; tab[b][2] = tab[c][2] = tab[a][1] = tab[a][2] = -1; break; } tab[b][1] = tab[c][1] = a+1; tab[b][2] = tab[c][2] = a+2; b = a + 1; c = a + 2; a += 2; d *= 2; } printf("%d\n", a); for(int i=1; i<=a; i++) { if(tab[i][1] == -2) printf("%d %d\n", a, tab[i][2]); else printf("%d %d\n", tab[i][1], tab[i][2]); } 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 | #include<bits/stdc++.h> using namespace std; int tab[109][3]; int main() { int k,a=3,b=2,c=3,d=1; scanf("%d", &k); if(k == 1) { printf("2\n2 -1\n-1 -1\n"); return 0; } tab[1][1] = 2; tab[1][2] = 3; for(int i=1; i<=32; i++) { if(k & d) { tab[b][1] = -2; tab[b][2] = a+1; a++; b = a; k -= d; } if(2 * d == k) { a++; tab[b][1] = tab[c][1] = a; tab[b][2] = tab[c][2] = tab[a][1] = tab[a][2] = -1; break; } tab[b][1] = tab[c][1] = a+1; tab[b][2] = tab[c][2] = a+2; b = a + 1; c = a + 2; a += 2; d *= 2; } printf("%d\n", a); for(int i=1; i<=a; i++) { if(tab[i][1] == -2) printf("%d %d\n", a, tab[i][2]); else printf("%d %d\n", tab[i][1], tab[i][2]); } return 0; } |