zoj 3768 Continuous 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)

题意:给出n求出最少要用多少等差数列的连续和组成。

sl:本以为是dp没想到n这么大,天啊,跳河,之后就没做,只敲了下dp代码看了看有没有什么规律。

打了表没看出来。后来才知道原来最终结果不超过3,再看看我的表确实如此。有表都没看出规律。

之后就是暴力枚举了。 时间复杂度是1e6刚好。

 外付打表代码。

 1 //water
 2 /*
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<cmath>
 8 using namespace std;
 9 typedef long long LL;
10 const int inf = 0x3f3f3f3f;
11 const int MAX = 1e6+10;
12 int dp[MAX];
13 int main()
14 {
15     int n;
16     while(scanf("%d",&n)==1)
17     {
18         for(int i=0;i<MAX;i++) dp[i]=inf;
19         dp[1]=1; dp[0]=0;
20         for(int i=2;i<100;i++)
21         {
22             for(int j=1;j<100;j++)
23             {
24                 int t=(i-j+1)*(1+i-j+1)/2;
25                 if(t<=i)
26                 dp[i]=min(dp[i],dp[i-t]+1);
27             }
28         }
29         printf("%d ",dp[n]);
30     }
31     return 0;
32 }
33 全然不超过3个
34 */
35 #include <cstring>
36 #include <algorithm>
37 #include <cstdio>
38 #include <map>
39 using namespace std;
40 const int MAX = 1e6+10;
41 map<int,int> val;
42 map<int,int> cnt;
43 
44 void init()
45 {
46     int start=1int cur=1int a=1;
47     while(start<=123456789)
48     {
49         val[cur]=start;
50         cnt[start]=cur;
51         a++;
52         start+=a;
53         cur++;
54     }
55 }
56 
57 int main()
58 {
59     int cas,n;
60     scanf("%d",&cas);
61     while(cas--)
62     {
63         val.clear(); cnt.clear();
64         init();
65         scanf("%d",&n);
66 
67         if(cnt[n])
68         printf("%d ",cnt[n]);
69         else
70         {
71             int end=1int flag=1;
72             while(val[end]<n) end++;
73             for(int i=1;i<=end;i++)
74             {
75                 if(val[n-val[i]])
76                 {
77                     flag=0;
78                     printf("%d %d ",cnt[val[i]],cnt[n-val[i]]);
79                 }
80             }
81             for(int i=1;i<=end&&flag;i++)
82             {
83                 for(int j=end;j>=1&&flag;j--)
84                 {
85                     int t=n-val[j]-val[i];
86                     if(t>0)
87                     {
88                         if(val[t])
89                         {
90                             printf("%d %d %d ",cnt[val[i]],cnt[val[j]],cnt[t]);
91                         }
92                     }
93                 }
94             }
95         }
96     }
97 
98     return 0;
99 }
原文地址:https://www.cnblogs.com/acvc/p/3649206.html