Problem: SDOI2007 数列

Problem: SDOI2007 数列

Description
有一个数列,具有这样的性质: a1 = 1 ,对于数列中的其他数 ak= ai + aj (1<=i<=j<=n ),现在给出数列的最后一个数an,求使 n 最小的数列。

Input
一行,只有一个整数 an , ( 1 <= n <= 1000 )

Output
第一行输出 n 。第二行输出数列,每两个数之间有且仅有一个空格。

Sample Input
4
Sample Output
3

#include<bits/stdc++.h>
using namespace std;
int tag[1000],n,ans=999999;
void dfs(int dep) 
{
    int temp1=tag[dep-1];
    int sum=0;
    while(temp1<=n)    
    {
        temp1*=2;
        sum++;
    }
    if(dep+sum-1>=ans)     
       return;
    for(int i=dep-1; i>=1; i--)     
    for(int j=i; j>=1; j--)     
    {
            int temp=tag[i]+tag[j];
            if(temp>tag[dep-1]&&temp<=n)         
            {
                tag[dep]=temp;
                if(dep<ans&&tag[dep]==n)             
                {
                    ans=dep;
                    return;
                }
                dfs(dep+1);
            }
        }
}
int main() 
{
    ans=999999;
    cin>>n;
    if(n<=2) cout<<n<<endl;
    else    {
        tag[1]=1;
        tag[2]=2;
        dfs(3);
        cout<<ans<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41654438/article/details/81448653