#include <bits/stdc++.h> using namespace std; struct node{int a,b; node():a(-1),b(-1){}; }; vector<node> g(101); void Horner(int k,int &l,int &r, int &n) { if (k==1) return; Horner(k/2,l,r,n); //cout << "r "<< n+1<<" "<<n+2<<endl; g[r].a=n+1; g[r].b=n+2; g[n+1].a=n+2; n+=2; r=n; if (k%2) { //cout << "l " << n+1 << endl; g[n+1].a=l; g[n+1].b=r; n+=1; l=n; } } int main() { ios_base::sync_with_stdio(0); int k; cin >> k; int l=2,r=2,n=2; Horner(k,l,r,n); g[1].a=l; n++; g[r].a=n; cout << n << endl; for (int i=1;i<=n;i++) cout << g[i].a << " " << g[i].b << endl; 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 | #include <bits/stdc++.h> using namespace std; struct node{int a,b; node():a(-1),b(-1){}; }; vector<node> g(101); void Horner(int k,int &l,int &r, int &n) { if (k==1) return; Horner(k/2,l,r,n); //cout << "r "<< n+1<<" "<<n+2<<endl; g[r].a=n+1; g[r].b=n+2; g[n+1].a=n+2; n+=2; r=n; if (k%2) { //cout << "l " << n+1 << endl; g[n+1].a=l; g[n+1].b=r; n+=1; l=n; } } int main() { ios_base::sync_with_stdio(0); int k; cin >> k; int l=2,r=2,n=2; Horner(k,l,r,n); g[1].a=l; n++; g[r].a=n; cout << n << endl; for (int i=1;i<=n;i++) cout << g[i].a << " " << g[i].b << endl; return 0; } |