#include <bits/stdc++.h> using namespace std; // count bits inspirowane z geeksofgeeks int countbits(int n, vector<int>& dp, vector<int>& last_occurence) { if(dp[n]!=-1) return dp[n]; int result = (n & 1) + countbits(n >> 1, dp, last_occurence); dp[n] = result; if(last_occurence[result]<n) last_occurence[result] = n; return result; } int main(){ // data int n; cin>>n; vector<int> dp(n+1, -1); vector<int> last_occurence(n+1, -1); dp[0] = 0; int sum = 1, i = 1; // count while(sum<n){ i++; if(dp[i]==-1) countbits(i, dp, last_occurence); sum+=dp[i]; } // first case if(sum==n){ cout<<i<<"\n"; while(i>0){ cout<<i<<" "; i--; } // second case } else{ int r = sum-n; int bad = last_occurence[r]; cout<<i-1<<"\n"; while(i>0){ if(i!=bad) cout<<i<<" "; 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 | #include <bits/stdc++.h> using namespace std; // count bits inspirowane z geeksofgeeks int countbits(int n, vector<int>& dp, vector<int>& last_occurence) { if(dp[n]!=-1) return dp[n]; int result = (n & 1) + countbits(n >> 1, dp, last_occurence); dp[n] = result; if(last_occurence[result]<n) last_occurence[result] = n; return result; } int main(){ // data int n; cin>>n; vector<int> dp(n+1, -1); vector<int> last_occurence(n+1, -1); dp[0] = 0; int sum = 1, i = 1; // count while(sum<n){ i++; if(dp[i]==-1) countbits(i, dp, last_occurence); sum+=dp[i]; } // first case if(sum==n){ cout<<i<<"\n"; while(i>0){ cout<<i<<" "; i--; } // second case } else{ int r = sum-n; int bad = last_occurence[r]; cout<<i-1<<"\n"; while(i>0){ if(i!=bad) cout<<i<<" "; i--; } } return 0; } |