zoj Continuous 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. 

 

 

  1 #include<iostream>

  2 #include<stdio.h>

  3 #include<cstring>

  4 #include<cstdlib>

  5 #include<algorithm>

  6 #include<queue>

  7 

  8 int val[16001];

  9 bool hash[16002];

 10 struct node

 11 {

 12     int val;

 13     int i;

 14     struct node *next;

 15 }f[16002];

 16 

 17 void Insert(int x,int i)

 18 {

 19     int k;

 20     node *p;

 21     k=x%16001;

 22     p=&f[k];

 23     while(p!=NULL && p->val!=x)

 24     {

 25         p=p->next;

 26     }

 27     if(p==NULL)

 28     {

 29         p=(struct node*)malloc(sizeof(struct node));

 30         p->val=x;

 31         p->i=i;

 32         p->next=f[k].next;

 33         f[k].next=p;

 34     }

 35 }

 36 bool found(int x,int &i)

 37 {

 38     int k;

 39     node *p;

 40     k=x%16001;

 41     p=&f[k];

 42     while(p!=NULL && p->val!=x)

 43     {

 44         p=p->next;

 45     }

 46     if(p==NULL) return false;

 47     if(p->val==x)

 48     {

 49         i=p->i;

 50         return true;

 51     }

 52     return false;

 53 }

 54 void prepare()

 55 {

 56     int i;

 57     val[0]=0;

 58     for(i=0;i<=16000;i++)

 59     {

 60         f[i].val=0;

 61         f[i].i=0;

 62         f[i].next=NULL;

 63     }

 64     for(i=1;i<=16000;i++)

 65     {

 66         val[i]=val[i-1]+i;

 67         Insert(val[i],i);

 68     }    

 69 }

 70 void solve(int n)

 71 {

 72     int i,wz,k,j,s;

 73     //one 

 74     if(found(n,wz)==true)

 75     {

 76         printf("%d\n",wz);

 77         return;

 78     }

 79     for(i=16000;;i--)

 80     {

 81         if(val[i]<n)

 82         {

 83             s=i;

 84             break;

 85         }

 86     }

 87     for(i=1;i<=s;i++)

 88     {

 89         k=n-val[i];

 90         if(found(k,wz)==true)

 91         {

 92             printf("%d %d\n",i,wz);

 93             return;

 94         }

 95     }

 96     for(i=1;i<=s;i++)

 97     {

 98         for(j=s;j>=1;j--)

 99         {

100             k=n-val[i]-val[j];

101             if(k<0)continue;

102             if(found(k,wz)==true)

103             {

104                 printf("%d %d %d\n",i,j,wz);

105                 return;

106             }

107         }

108     }

109 }

110 int main()

111 {

112     int T,n;

113     prepare();

114     scanf("%d",&T);

115     while(T--)

116     {

117         scanf("%d",&n);

118         solve(n);

119     }

120     return 0;

121 }

 

你可能感兴趣的:(login)