ZOJ3768Continuous Login (等差求和公式+二分)

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. 

解题:通过列出组和方式,得知组和的组数最多只有3个,输出原则是:先组数最小,组数相等的每组的数字尽量平分。

首先:用求和分式列出只要一组就能组成的数 。  再查找时,先查只有一组的,如果没有,再查两组的,两组都是建立在一组基础之上。如果两组不能组成就用三组来构成输入的数M。

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define N 123456789
int ans[20000],n;
int find(int M)
{
    int l,r,m;
    l=1; r=n;
    while(l<=r)
    {
        m=(l+r)/2;
        if(M==ans[m])return m;
        if(M>ans[m])l=m+1;
        else r=m-1;
    }
    if(ans[m]>M)m--;
    return m;
}
int main()
{
    int k[3],M,t,a,b,c,flog;
    for(n=1;n*(n+1)/2<=N;n++)
        ans[n]=n*(n+1)/2;
        n=n-1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&M);
        k[0]=find(M);
        if(ans[k[0]]==M) printf("%d\n",k[0]);
        else{
            a=M/2; b=M-a; flog=0;
            k[1]=find(b);
            if(ans[k[1]]<b)k[1]++;
            for(;ans[k[1]]<M&&k[1]<=n;k[1]++)
            {
                k[0]=find(M-ans[k[1]]);
                if(ans[k[0]]+ans[k[1]]==M)
                {
                    printf("%d %d\n",k[0],k[1]);
                    flog=1; break;
                }
            }

            if(flog==0)
            {
                int tem;
               a=M/3; k[0]=find(a);
                for(;k[0]>0&&flog==0;k[0]--)
                for(k[1]=k[0];ans[k[0]]+ans[k[1]]<M&&k[1]<=n;k[1]++)
                {
                    c=M-ans[k[0]]-ans[k[1]];
                    k[2]=find(c);
                    if(ans[k[0]]+ans[k[1]]+ans[k[2]]==M)
                    {
                        if(k[2]>k[0]){tem=k[2]; k[2]=k[0]; k[0]=tem; }
                        if(k[2]>k[1]){tem=k[2]; k[2]=k[1]; k[1]=tem; }
                        if(k[0]>k[1]){tem=k[0]; k[0]=k[1]; k[1]=tem; }
                        printf("%d %d %d\n",k[0],k[1],k[2]);
                        flog=1; break;
                    }
                }
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值