Happiness Hotel 数论 佩尔方程

来源:互联网 发布:vr相机软件 编辑:程序博客网 时间:2024/04/28 02:02
[cpp] view plaincopy
  1. //此是标准程序  
  2. #include <iostream>  
  3. #include <cmath>  
  4. using namespace std;  
  5. int can[1005]={0};  
  6. int a[10005][605]={0};  
  7. int x[6005],y[6005],t[6005];  
  8. int h1,h2;  
  9. int bb,ee,xx,yx,c,n;  
  10. void gui(int ji,int many,int ma,int kk);  
  11. int main()  
  12. {  
  13. // freopen("exp.in","r",stdin);  
  14. // freopen("exp.out","w",stdout);  
  15. for (int i=1;i<=31;i++)  
  16. can[i*i]=true;  
  17. for (int i=1;i<=1000;i++)  
  18. if (!can[i])  
  19. {  
  20. a[i][600]=1;  
  21. bb=1;  
  22. ee=(int)sqrt(i);  
  23. a[i][0]=ee;  
  24. ee=-ee;  
  25. xx=bb;  
  26. yx=ee;  
  27. xx=-yx;  
  28. yx=i-yx*yx;  
  29. n=0;  
  30. while ((xx-yx)*(xx-yx)<i||xx>=0)  
  31. {  
  32. xx-=yx;  
  33. n++;  
  34. }  
  35. a[i][1]=n;  
  36. c=xx;  
  37. xx=yx;  
  38. yx=c;  
  39. while (xx!=bb||yx!=ee)  
  40. {  
  41. a[i][600]++;  
  42. c=xx;  
  43. xx=-yx;  
  44. yx=i-yx*yx;  
  45. yx=yx/c;  
  46. n=0;  
  47. while ((xx-yx)*(xx-yx)<i||xx>=0)  
  48. {  
  49. xx-=yx;  
  50. n++;  
  51. }  
  52. a[i][a[i][600]]=n;  
  53. c=xx;  
  54. xx=yx;  
  55. yx=c;  
  56. }  
  57. }  
  58. int i;  
  59. while (scanf("%d",&i)!=EOF)  
  60. if (!can[i])  
  61. {  
  62. if (a[i][600]%2)  
  63. gui(1,a[i][0],i,a[i][600]*2);  
  64. else  
  65. gui(1,a[i][0],i,a[i][600]);  
  66. for (int j=x[0];j>=1;j--)  
  67. printf("%d",x[j]);  
  68. printf("\n");  
  69. }  
  70. else  
  71. printf("no solution\n");  
  72. // system("pause");  
  73. return 0;  
  74. }  
  75. void gui(int ji,int many,int ma,int kk)  
  76. {  
  77. if (ji<kk)  
  78. gui(ji+1,a[ma][(ji-1)%a[ma][600]+1],ma,kk);  
  79. else  
  80. {  
  81. h1=1;  
  82. h2=1;  
  83. x[1]=many;  
  84. y[1]=1;  
  85. return;  
  86. }  
  87. for (int i=1;i<=h1;i++)  
  88. t[i]=x[i];  
  89. for (int i=1;i<=h2;i++)  
  90. x[i]=y[i];  
  91. for (int i=1;i<=h1;i++)  
  92. y[i]=t[i];  
  93. c=h1;  
  94. h1=h2;  
  95. h2=c;  
  96. for (int i=1;i<=h2;i++)  
  97. if (i<=h1)  
  98. x[i]+=many*y[i];  
  99. else  
  100. x[i]=many*y[i];  
  101. if (h2>h1)  
  102. h1=h2;  
  103. for (int i=1;i<h1;i++)  
  104. if (x[i]>=10)  
  105. {  
  106. x[i+1]+=x[i]/10;  
  107. x[i]%=10;  
  108. }  
  109. while (x[h1]>=10)  
  110. {  
  111. x[h1+1]=x[h1]/10;  
  112. x[h1]%=10;  
  113. h1++;  
  114. }  
  115. x[0]=h1;  
  116. }  

题目描述:

The life of Little A is good, and, he managed to get enough money to run a hotel. The best for him is that he need not go to work outside, just wait for the money to go into his pocket. Little A wants everything to be perfect, he has a wonderful plan that he will keep one most beautiful reception whose size is 1()(which means the reception is 1 square meter). There are other k rooms that have the same area, and the area is x^2(), x is an integer; Little A wants his hotel to be a square. Little A is a good thinker, but not a good maker. As his poor performance on math, he cannot calculate the least area needed to build such a hotel of his will. Now, this task belongs to you, solve this problem to make Little A’s dream of Happy Hotel come true. Please be careful, the whole area should only contain k rooms, and the reception, there should not be any vacant place.


Input:

There are several test cases.

Each case contains only one integer k(1<=k<=1000) ,the number of rooms the hotel should have in one line.

Proceed to the end of file.

Output:

      Output one integer d, means the hotel’s area is d^2(If there is no answer, output “no solution”) .The output of one test case occupied exactly one line.


思路:

你很容易得出。。题目意思就是这样一条方程式  ans^2 = k*n*n+1 ,(n>=1是一个整数(无上限)) ,求ans的最小整数解,然后输入的是k,然后。冯斐神牛跟我说这是佩尔方程。然后此题就可以完美解决了。  下面贴的程序是标准程序,非本人所写,特此注明!
原创粉丝点击