[迭代加深dfs] zoj 3768 Continuous Login

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3768

Continuous Login

Time Limit: 2 Seconds       Memory Limit: 131072 KB       Special Judge

Pierre is recently obsessed with an online game. To encourage users to log in, this game will give users a continuous login reward. The mechanism of continuous login reward is as follows: If you have not logged in on a certain day, the reward of that day is 0, otherwise the reward is the previous day's plus 1.

On the other hand, Pierre is very fond of the number N. He wants to get exactly N points reward with the least possible interruption of continuous login.

Input

There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:

There is one integer N (1 <= N <= 123456789).

Output

For each test case, output the days of continuous login, separated by a space.

This problem is special judged so any correct answer will be accepted.

Sample Input
4
20
19
6
9
Sample Output
4 4
3 4 2
3
2 3
Hint

20 = (1 + 2 + 3 + 4) + (1 + 2 + 3 + 4)

19 = (1 + 2 + 3) + (1 + 2 + 3 + 4) + (1 + 2)

6 = (1 + 2 + 3)

9 = (1 + 2) + (1 + 2 + 3)

Some problem has a simple, fast and correct solution.


Author:  ZHOU, Yuchen
Source:  The 14th Zhejiang University Programming Contest
Submit     Status

题目意思:

给一个数n,把n分解成多个1+2+3+..+i的和的形式(i任意),求中断数最少的分法。

n<=12345678

解题思路:

迭代加深dfs

预处理出12345678内的所有可以被一个凑成的数,然后控制分的部分,然后从小于当前的最大的找起,如果找到就马上退出。

分成的部分数不会很多。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 110000

int dp[Maxn],n;
map<int,int>myp;
vector<int>ans;
bool flag;

void init()
{
    dp[0]=0;
    for(int i=1;i<=20000;i++)
    {
        dp[i]=dp[i-1]+i;
        myp[dp[i]]=i;
    }
    //printf("%d\n",dp[20000]);
}
void dfs(int dep,int cur)
{
    if(dep==1) //已经走完
    {
        if(myp[cur]) //最后一个可以用一部分凑出
        {
            ans.push_back(myp[cur]);
            flag=true;
            return ;
        }
        return ;
    }
    int i=upper_bound(dp+1,dp+20000+1,cur)-dp; //找到小于等于cur的最大的那个
    i--;
    for(;i>=1;i--) //从尽可能大的找起
    {
        ans.push_back(myp[dp[i]]);
        dfs(dep-1,cur-dp[i]); 
        if(flag)
            return ;
        ans.erase(--ans.end());

    }
}

int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   init();
   int t;

   scanf("%d",&t);
   while(t--)
   {
       scanf("%d",&n);
       ans.clear();
       flag=false;

       for(int i=1;;i++) //控制部分数
       {
           dfs(i,n);
           if(flag)
           {
               printf("%d",ans[0]);
               for(int j=1;j<ans.size();j++)
                    printf(" %d",ans[j]);
               putchar('\n');
               break;
           }
       }
   }
   return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值