Description
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 9Sample Output
4 4 3 4 2 3 2 3Hint
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.
用求和分式列出只要一组就能组成的数 。 再查找时,先查只有一组的,如果没有,再查两组的,两组都是建立在一组基础之上。如果两组不能组成就用三组来构成输入的数M。用求和分式列出只要一组就能组成的数 。 再查找时,先查只有一组的,如果没有,再查两组的,两组都是建立在一组基础之上。如果两组不能组成就用三组来构成输入的数M。
要用scanf输入不然会超时,下面是我的AC代码:
#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; } } } } } }<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; } } } } } }