ZOJ 3768Continuous Login(找规律然后二分)

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




      题目大意:意思让你找最少的组数,使得找的所有的组等差数列和的全部和为所给数,具体题目意思看下题目的数列形式即可。

解题思路:

好吧,思路开始天马行空的,先打了个表存放前n项和,发现打到13000。然后就开始yy了,想直接从大往小找,但发现不能保证组数最小。

然后想写bfs,但是标记又标不了,发现还是不行,于是开始找规律。

我列举了1~20可以拆分的,发现最多只有三组。

然后就开始敲代码了,但是还是不是很确定。就让豪爷在旁边写枚举前100项的分组数,,


先看能不能分一组,如果可以就一组,

不行再试下两组,第一组枚举,第二组二分。

不行就试下三组,枚举前两组,第三组二分。

如果实在不行,就没办法了。

其实,我二分还debug了半天,,,,

算法复杂度O(n*n*log2(n))


 AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#define ll long long
using namespace std;

int dp[20005];
int len;

int erfen(int x)
{
    int l=1,r=len;
    int mid;
    mid=(l+r)>>1;

    while(r>l)
    {
        if(dp[mid]==x) return mid;
        if(dp[mid]<x) l=mid+1;
        else r=mid-1;
        mid=(l+r)>>1;
    }

    if(dp[l]>x) l--;
    return l;
}

int main()
{
    int i;
    dp[0]=0;
    dp[1]=1;
    for(i=1;i<=20000;i++)
    {
        dp[i]=dp[i-1]+i;
        if(dp[i]>=123456789)
            break;
    }

    len=i;

    int tes,n;
    cin>>tes;

    int a,b,c;
    while(tes--)
    {
        cin>>n;
        int p=erfen(n);
        if(dp[p]==n)
        {
            printf("%d\n",p);
            continue;
        }

        int flag=0;
        for(a=p;a>=1;a--)
        {
            b=erfen(n-dp[a]);
            if(dp[b]+dp[a]==n)
            {
                flag=1;
                break;
            }
        }

        if(flag)
        {
            printf("%d %d\n",a,b);
            continue;
        }

        flag=0;
        for(a=p;a>=1;a--)
        {
            int s=erfen(n-dp[a]);
            for(b=s;b>=1;b--)
            {
                c=erfen(n-dp[a]-dp[b]);
                if(dp[a]+dp[b]+dp[c]==n)
                {
                    flag=1;
                    break;
                }
            }
            if(flag) break;
        }

        if(flag)
        {
            printf("%d %d %d\n",a,b,c);
            continue;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值