sgu 375

水题。。(我只会做水题。。)
http://acm.sgu.ru/problem.php?contest=0&problem=375
很明显一点要是奇数才有解。。然后倒过来看。奇数2N+1可以由N和N+1变成。。其中只有一个奇数。。直接推就可以了。。O(log n)。。。
#include<iostream>
#include<cstdlib>
#include<stack>
using namespace std;
stack<int> S;
void Cant()
{
cout<<"No solution"<<endl;
exit(0);
}
int main()
{
int n;cin>>n;
if(n%2==0)
Cant();
while(n!=1)
{
int x=(n-1)/2;
if(x%2)
S.push(2);
else
x++,S.push(1);
n=x;
}
cout<<S.size()<<endl;
while(S.size())
{ cout<<S.top()<<" ";S.pop();}
cout<<endl;
}

3 thoughts on “sgu 375

Leave a Reply to WJBZBMR Cancel reply

Your email address will not be published. Required fields are marked *