Happiness Hotel 數論 佩爾方程

  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,然後。馮斐神牛跟我說這是佩爾方程。然後此題就可以完美解決了。  下面貼的程序是標準程序,非本人所寫,特此註明!
發佈了28 篇原創文章 · 獲贊 8 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章