比赛 20120224 评测结果 WWWWWWWW
题目名称 神奇的数列 最终得分 0
用户昵称 Citron酱 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2012-02-24 21:19:01
显示代码纯文本
#include <cstdio>
#include <cstdlib>

#define I_F "chain.in"
#define O_F "chain.out"

const short Maxm=70;
const short P=20;

long long n;
long long two[Maxm]={1,0};
long long ans[Maxm*2];
short m=0, l=0;

void Input();
void Search(const long long);
template <typename Any>
inline void Swap(Any&, Any&);
void Sort(const short, const short);
void Zone();
void Output();

int main()
{
	Input();
	Search(n);
	Zone();
	Output();
	return 0;
}

void Input()
{
	freopen(I_F,"r",stdin);
	scanf("%lld",&n);
}

void Search(const long long n)
{
	if (two[m]>=n)
		for (short i=m; i>=0; i--)
			if (two[i]==n)
				return;
			else if (two[i]<n)
			{
				ans[l++]=n-two[i];
				Search(n-two[i]);
				return;
			}
	for (; two[m]*2<=n; m++)
		two[m+1]=two[m]*2;
	if (two[m]<n)
	{
		ans[l++]=n-two[m];
		Search(n-two[m]);
		return;
	}
}

template <typename Any>
inline void Swap(Any &a, Any &b)
{
	Any t=a;
	a=b;
	b=t;
}

void Sort(const short l, const short r)
{
	if (r-l>P)
	{
		short i=l, j=r;
		long long x=ans[l+rand()%(r-l+1)];
		do
		{
			while (ans[i]<x) i++;
			while (ans[j]>x) j--;
			if (i<=j)
				Swap(ans[i++],ans[j--]);
		} while (i<j);
		if (i<r) Sort(i,r);
		if (l<j) Sort(l,j);
	}
	else
	{
		bool flag=true;
		for (short i=l; i<r && flag; i++)
		{
			flag=false;
			for (short j=r; j>i; j--)
				if (ans[j-1]>ans[j])
					Swap(ans[j-1],ans[j]),
					flag=true;
		}
	}
}

void Zone()
{
	for (short i=0; i<=m; i++)
		ans[l++]=two[i];
	Sort(0,l-1);
}

void Output()
{
	freopen(O_F,"w",stdout);
	printf("%hd\n",l);
	for (short i=0; i<l; printf("%hd ",ans[i++]));
}