#include<vector> #include <iostream> #include<algorithm> #include<queue> #include<fstream> using namespace std; typedef long long ll; #define f first #define s second #define pb push_back int min_val[1000001]; int n; int akt=0,l=1; int count(int a) { int licz=0; while(a){licz+=a%2; a/=2;} return licz; } int bin(int cel) { int p=0,k=l; int wyn=l; while(p<k) { int sr=(p+k)/2; if(min_val[sr]>cel){wyn=sr; k=sr;} if(min_val[sr]==cel){return sr;} if(min_val[sr]<cel){p=sr+1;} } return wyn; } int main() { ios_base::sync_with_stdio(); cin.tie(); cin>>n; while(1) { akt+=count(l); min_val[l]=akt; if(akt>n)break; l++; } // for(int i=0;i<=l;i++)cout<<min_val[i]<<' '; vector<int> wynik; while(n) { int a=bin(n); wynik.pb(a); n-=count(a); } cout<<wynik.size()<<endl; for(int i : wynik)cout<<i<<' '; 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 | #include<vector> #include <iostream> #include<algorithm> #include<queue> #include<fstream> using namespace std; typedef long long ll; #define f first #define s second #define pb push_back int min_val[1000001]; int n; int akt=0,l=1; int count(int a) { int licz=0; while(a){licz+=a%2; a/=2;} return licz; } int bin(int cel) { int p=0,k=l; int wyn=l; while(p<k) { int sr=(p+k)/2; if(min_val[sr]>cel){wyn=sr; k=sr;} if(min_val[sr]==cel){return sr;} if(min_val[sr]<cel){p=sr+1;} } return wyn; } int main() { ios_base::sync_with_stdio(); cin.tie(); cin>>n; while(1) { akt+=count(l); min_val[l]=akt; if(akt>n)break; l++; } // for(int i=0;i<=l;i++)cout<<min_val[i]<<' '; vector<int> wynik; while(n) { int a=bin(n); wynik.pb(a); n-=count(a); } cout<<wynik.size()<<endl; for(int i : wynik)cout<<i<<' '; return 0; } |