C數列 vijos P1350 IDS 搜索

見前一發題解,無非就是把數字的範圍換了一下,運算的方式換了一下
並沒有什麼
傳送門

下面是代碼

#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]<<" ";
}

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章