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
题意:找最少的组数,使得找的所有的组等差数列和的全部和为所给数
解题思路:暴力找了下,发现最多也就三,然后暴力+二分
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
int a[16100],cnt;
void init()
{
memset(a,0,sizeof a);
cnt=1;
int sum=0;
for(int i=1;; i++)
{
sum+=i;
if(sum<=123456789) a[cnt++]=sum;
else break;
}
//printf("%d\n",cnt-1);
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
auto it=lower_bound(a,a+cnt,n);
if(*it==n) printf("%d\n",it-a);
else
{
bool flag=0;
for(int i=1; a[i]<=n/2; i++)
{
int m=n-a[i];
auto it=lower_bound(a,a+cnt,m);
if(*it==m)
{
printf("%d %d\n",i,it-a);
flag=1;
break;
}
}
if(flag) continue;
for(int i=1; a[i]<n/2; i++)
{
int m=n-a[i];
for(int j=i; a[j]<m/2; j++)
{
int nm=m-a[j];
auto it=lower_bound(a,a+cnt,nm);
if(*it==nm)
{
printf("%d %d %d\n",i,j,it-a);
flag=1;
break;
}
}
if(flag)break;
}
if(!flag)printf("cnm\n");
}
}
return 0;
}