#include <bits/stdc++.h>
using namespace std;
int n;
int wynik;
const int k=120207;
int tab[1000000];
vector <int> res;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
tab[1] = 1;
tab[0] = 0;
int pot=2;
for (int i = 2; i < k; i++)
{
if (2*pot<=i)
pot*=2;
tab[i] = 1 + tab[i - pot];
}
for(int i=0;i<k;i++)
{
tab[i]+=tab[i-1];
}
//bin search po i żeby znaleźć odpowiedni
while (n>0)
{
int l=0;
int m=0;
int r=k;
while(l<r)
{
m=(l+r)/2;
if(tab[m]>=n)
r=m;
else
l=m+1;
}
wynik++;
res.push_back(l);
n-=(tab[l]-tab[l-1]);
}
cout<< wynik << endl;
for(auto x : res)
cout << x << " ";
cout << 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 | #include <bits/stdc++.h> using namespace std; int n; int wynik; const int k=120207; int tab[1000000]; vector <int> res; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; tab[1] = 1; tab[0] = 0; int pot=2; for (int i = 2; i < k; i++) { if (2*pot<=i) pot*=2; tab[i] = 1 + tab[i - pot]; } for(int i=0;i<k;i++) { tab[i]+=tab[i-1]; } //bin search po i żeby znaleźć odpowiedni while (n>0) { int l=0; int m=0; int r=k; while(l<r) { m=(l+r)/2; if(tab[m]>=n) r=m; else l=m+1; } wynik++; res.push_back(l); n-=(tab[l]-tab[l-1]); } cout<< wynik << endl; for(auto x : res) cout << x << " "; cout << endl; } |
English