见前一发题解,无非就是把数字的范围换了一下,运算的方式换了一下
并没有什么
传送门
下面是代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define N 1000
using namespace std;
int a[N];
int ar=1;
bool s=false;
int x;
bool DFS(int depth,int sum,int will)
{
/*printf("check at LINE 13\n");
printf("depth=%d sum=%d will=%d\n",depth,sum,will);
for(int i=1;i<=ar;++i)
cout<<a[i]<<" ";
puts("");*/
if(depth>will){if(sum==x){return true;}return false;}
/*printf("%d depth=%d will=%d a[%d]=%d\n",sum<<(will-depth-1) ,depth,will,ar,a[ar]);*/
if(sum<<(will-depth+1)<x)return false;
for(int i=ar;i>=1;--i)
if(sum+a[i]<=x)
{a[++ar]=sum+a[i];if(DFS(depth+1,sum+a[i],will))return true;ar--;}
return false;
}
int main()
{
cin>>x;
a[1]=1;
int cnt=0;
while(1)
{
++cnt;
if(DFS(1,1,cnt))break;
}
cout<<cnt+1<<endl;
for(int i=1;i<=ar;++i)
cout<<a[i]<<" ";
}