#include<iostream> using namespace std; int main(){ int k; cin>>k; string binar; if (k == 1){ cout<<2<<endl; cout<<2<<' '<<-1<<endl; cout<<-1<<' '<<-1<<endl; return 0; } while (k > 0){ if (k%2 == 0) binar += '0'; else binar += '1'; k /= 2; } int w = (binar.size()-1)*3 + 1; int G[w+1][2]; for (int i = 1; i <= w; i++){ if (i%3 == 1){ G[i][0] = i+1; G[i][1] = i+2; } else if (i%3 == 0){ G[i][0] = i+1; G[i][1] = -1; } else { G[i][0] = i+2; if (binar[i/3] == 0) G[i][1] = -1; else G[i][1] = w-2; } } G[w][0] = -1; G[w][1] = -1; G[w-2][1] = -1; //cout<<binar<<' '<<binar.size()<<' '<<binar[binar.size()-1]<<endl; if (binar[binar.size()-1] == '1') G[w-1][1] = w-2; //cout<<G[w-1][1]<<endl; cout<<w<<endl; for (int i = 1; i <= w; i++){ cout<<G[i][0]<<' '<<G[i][1]<<endl; } }
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 | #include<iostream> using namespace std; int main(){ int k; cin>>k; string binar; if (k == 1){ cout<<2<<endl; cout<<2<<' '<<-1<<endl; cout<<-1<<' '<<-1<<endl; return 0; } while (k > 0){ if (k%2 == 0) binar += '0'; else binar += '1'; k /= 2; } int w = (binar.size()-1)*3 + 1; int G[w+1][2]; for (int i = 1; i <= w; i++){ if (i%3 == 1){ G[i][0] = i+1; G[i][1] = i+2; } else if (i%3 == 0){ G[i][0] = i+1; G[i][1] = -1; } else { G[i][0] = i+2; if (binar[i/3] == 0) G[i][1] = -1; else G[i][1] = w-2; } } G[w][0] = -1; G[w][1] = -1; G[w-2][1] = -1; //cout<<binar<<' '<<binar.size()<<' '<<binar[binar.size()-1]<<endl; if (binar[binar.size()-1] == '1') G[w-1][1] = w-2; //cout<<G[w-1][1]<<endl; cout<<w<<endl; for (int i = 1; i <= w; i++){ cout<<G[i][0]<<' '<<G[i][1]<<endl; } } |