#include<iostream> #include<cstdio> using namespace std; inline int read() { int n=0,f=1,ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { n=n*10+ch-'0'; ch=getchar(); } return n*f; } bool vis[10000005]; int main() { int n,maxn=0; n=read(); int sth=0; for(int i=1;i<=n;i++) { sth+=__builtin_popcount(i); if(sth>=n) { maxn=i; break; } } if(sth==n) { printf("%d\n",maxn); for(int i=maxn;i>=1;i--)printf("%d ",i); return 0; } for(int i=maxn;i>=1;i--) { if(__builtin_popcount(i)<=sth-n) { vis[i]=true; sth-=__builtin_popcount(i); break; } } int tsl=0; for(int i=1;i<=maxn;i++)if(vis[i]==false)tsl++; printf("%d\n",tsl); for(int i=maxn;i>=1;i--)if(vis[i]==false)printf("%d ",i); printf("\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 | #include<iostream> #include<cstdio> using namespace std; inline int read() { int n=0,f=1,ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { n=n*10+ch-'0'; ch=getchar(); } return n*f; } bool vis[10000005]; int main() { int n,maxn=0; n=read(); int sth=0; for(int i=1;i<=n;i++) { sth+=__builtin_popcount(i); if(sth>=n) { maxn=i; break; } } if(sth==n) { printf("%d\n",maxn); for(int i=maxn;i>=1;i--)printf("%d ",i); return 0; } for(int i=maxn;i>=1;i--) { if(__builtin_popcount(i)<=sth-n) { vis[i]=true; sth-=__builtin_popcount(i); break; } } int tsl=0; for(int i=1;i<=maxn;i++)if(vis[i]==false)tsl++; printf("%d\n",tsl); for(int i=maxn;i>=1;i--)if(vis[i]==false)printf("%d ",i); printf("\n"); return 0; } |