LightOJ-1008 Fibsieve`s Fantabulous Birthday

£神魔★判官ぃ 2022-08-18 01:54 179阅读 0赞

1008 - Fibsieve`s Fantabulous Birthday










PDF (English) Statistics Forum







Time Limit: 0.5 second(s) Memory Limit: 32 MB

Fibsieve had a fantabulous (yes, it’s an actual word) birthday party this year. He had so many gifts that he was actually thinking of not having a party next year.

Among these gifts there was an N x N glass chessboard that had a light in each of its cells. When the board was turned on a distinct cell would light up every second, and then go dark.

The cells would light up in the sequence shown in the diagram. Each cell is marked with the second in which it would light up.

Center

(The numbers in the grids stand for the time when the corresponding cell lights up)

In the first second the light at cell (1, 1) would be on. And in the 5th second the cell (3, 1) would be on. Now, Fibsieve is trying to predict which cell will light up at a certain time (given in seconds). Assume that N is large enough.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case will contain an integer S (1 ≤ S ≤ 1015) which stands for the time.

Output

For each case you have to print the case number and two numbers (x, y), the column and the row number.












Sample Input

Output for Sample Input

3

8

20

25

Case 1: 2 3

Case 2: 5 4

Case 3: 1 5

解题思路

通过数的分布我们不难看出可以通过数的开方找到他在第几层,这里的第几层意思是从右下角开始的第几层,然后通过分类讨论找到第几行或第几列。

主要是注意超时,不能使用for循环。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8. int t;
  9. scanf("%d",&t);
  10. int aa=0;
  11. while(t--)
  12. {
  13. long long n;
  14. scanf("%lld",&n);
  15. printf("Case %d: ",++aa);
  16. if(n==1)
  17. {
  18. printf("1 1\n");
  19. continue;
  20. }
  21. double map;
  22. map=sqrt(n);
  23. long long ans;
  24. long long i,j;
  25. ans=map;//ceng
  26. if(ans<map)
  27. {
  28. ans++;
  29. }
  30. if(ans%2==0)
  31. {long long cnm=pow(ans,2);
  32. long long kai;
  33. long long end;
  34. kai=cnm;
  35. end=kai-(2*ans-1)+1;
  36. long long zz;
  37. zz=(kai+end)/2;
  38. if(zz==n)
  39. printf("%d %d\n",ans,ans);
  40. else
  41. {
  42. if(n<zz)
  43. {
  44. printf("%d %d\n",n-end+1,ans);
  45. }
  46. else
  47. {
  48. printf("%d %d\n",ans,kai-n+1);
  49. }
  50. }
  51. }
  52. else
  53. {long long cnm=pow(ans,2);
  54. long long kai;
  55. long long end;
  56. kai=cnm;
  57. end=kai-(2*ans-1)+1;
  58. long long zz;
  59. zz=(kai+end)/2;
  60. if(zz==n)
  61. printf("%d %d\n",ans,ans);
  62. else
  63. {
  64. if(n<zz)
  65. {
  66. printf("%d %d\n",ans,n-end+1);
  67. }
  68. else
  69. {
  70. printf("%d %d\n",kai-n+1,ans);
  71. }
  72. }
  73. }
  74. }
  75. return 0;
  76. }

发表评论

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

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

相关阅读