ZOJ 3768 Continuous Login(找规律+哈希)

系统管理员 2022-06-03 08:59 158阅读 0赞

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 Npoints 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

  1. 4
  2. 20
  3. 19
  4. 6
  5. 9

Sample Output

  1. 4 4
  2. 3 4 2
  3. 3
  4. 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组组成一个数字,然后就打表,用map记录下该数字下是加到几的结果,然后分三种情况:第一种是一个数字就可以组成,这个直接哈希判断就好,第二种是两个数字组成,这个用枚举第一个数字+第二个哈希判断就好了,第三种是三个数字组成,枚举前两个第三个数字用哈希判断

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<queue>
  5. #include<stack>
  6. #include<math.h>
  7. #include<vector>
  8. #include<map>
  9. #include<set>
  10. #include<stdlib.h>
  11. #include<cmath>
  12. #include<string>
  13. #include<algorithm>
  14. #include<iostream>
  15. #include<stdio.h>
  16. using namespace std;
  17. #define ll long long
  18. map<int,int>M;
  19. int a[15750];
  20. void init()
  21. {
  22. int i,j,s=0;
  23. for(i=1;i<=15720;i++)
  24. {
  25. s+=i;
  26. M[s]=i;
  27. a[i]=s;
  28. }
  29. }
  30. int main()
  31. {
  32. int i,j,k,n,test,tag;
  33. init();
  34. while(scanf("%d",&test)!=EOF)
  35. {
  36. while(test--)
  37. {
  38. scanf("%d",&n);
  39. if(M[n])
  40. {
  41. printf("%d\n",M[n]);
  42. }
  43. else
  44. {
  45. tag=0;
  46. for(i=1;a[i]<n;i++)
  47. {
  48. if(M[n-a[i]])
  49. {
  50. printf("%d %d\n",i,M[n-a[i]]);
  51. tag=1;
  52. break;
  53. }
  54. }
  55. if(!tag)
  56. {
  57. for(i=1;a[i]<n;i++)
  58. {
  59. for(j=1;a[i]+a[j]<n;j++)
  60. {
  61. if(M[n-a[i]-a[j]])
  62. {
  63. printf("%d %d %d\n",i,j,M[n-a[i]-a[j]]);
  64. goto loop;
  65. }
  66. }
  67. }
  68. loop:;
  69. }
  70. }
  71. }
  72. }
  73. return 0;
  74. }

发表评论

表情:
评论列表 (有 0 条评论,158人围观)

还没有评论,来说两句吧...

相关阅读

    相关 关于关于关于

    今天老师讲了哈希,草草地整理一下: 哈希表,也称散列表,是一种高效的数据结构。它的最大优点就是把数据存储和查找所消耗的时间大大降低,几乎可以看成是 O(1)的,而代价是消耗比