首页 > 代码库 > NBUT 1224 Happiness Hotel 2010辽宁省赛

NBUT 1224 Happiness Hotel 2010辽宁省赛

Time limit 1000 ms

Memory limit 131072 kB

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.

Sample Input

1
2
3

Sample Output

no solution
3
2


要求n*a*a+1=b*b,a,b都是整数,n<=1000,看有没有这样的a,b

网上copy的代码。。还没理解还没自己写。。先记着
来自http://blog.sina.com.cn/s/blog_7e9a88f70100sot1.html
  1 #include <iostream>
  2 #include <cmath>
  3 using namespace std;
  4 
  5 int can[1005];
  6 int a[10005][605];
  7 int x[6005],y[6005],t[6005];
  8 int h1,h2;
  9 int bb,ee,xx,yx,c,n;
 10 
 11 void gui(int ji,int many,int ma,int kk)
 12 {
 13     if (ji<kk)
 14         gui(ji+1,a[ma][(ji-1)%a[ma][600]+1],ma,kk);
 15     else
 16     {
 17         h1=1;
 18         h2=1;
 19         x[1]=many;
 20         y[1]=1;
 21         return;
 22     }
 23     for (int i=1;i<=h1;i++)
 24         t[i]=x[i];
 25     for (int i=1;i<=h2;i++)
 26         x[i]=y[i];
 27     for (int i=1;i<=h1;i++)
 28         y[i]=t[i];
 29     c=h1;
 30     h1=h2;
 31     h2=c;
 32     for (int i=1;i<=h2;i++)
 33     {
 34         if (i<=h1)
 35             x[i]+=many*y[i];
 36         else
 37             x[i]=many*y[i];
 38     }
 39     if (h2>h1)
 40         h1=h2;
 41     for (int i=1;i<h1;i++)
 42     {
 43         if (x[i]>=10)
 44         {
 45             x[i+1]+=x[i]/10;
 46             x[i]%=10;
 47         }
 48     }
 49     while (x[h1]>=10)
 50     {
 51         x[h1+1]=x[h1]/10;
 52         x[h1]%=10;
 53         h1++;
 54     }
 55     x[0]=h1;
 56 }
 57 
 58 int main()
 59 {
 60 
 61     for (int i=1;i<=31;i++)
 62         can[i*i]=true;
 63     for (int i=1;i<=1000;i++)
 64     if (!can[i])
 65     {
 66         a[i][600]=1;
 67         bb=1;
 68         ee=(int)sqrt((double)i);
 69         a[i][0]=ee;
 70         ee=-ee;
 71         xx=bb;
 72         yx=ee;
 73         xx=-yx;
 74         yx=i-yx*yx;
 75         n=0;
 76         while ((xx-yx)*(xx-yx)<i||xx>=0)
 77         {
 78             xx-=yx;
 79             n++;
 80         }
 81         a[i][1]=n;
 82         c=xx;
 83         xx=yx;
 84         yx=c;
 85         while (xx!=bb||yx!=ee)
 86         {
 87             a[i][600]++;
 88             c=xx;
 89             xx=-yx;
 90             yx=i-yx*yx;
 91             yx=yx/c;
 92             n=0;
 93             while ((xx-yx)*(xx-yx)<i||xx>=0)
 94             {
 95                 xx-=yx;
 96                 n++;
 97             }
 98             a[i][a[i][600]]=n;
 99             c=xx;
100             xx=yx;
101             yx=c;
102         }
103     }
104     int i;
105     while (scanf("%d",&i)!=EOF)
106     {
107         if (!can[i])
108         {
109             if (a[i][600]%2)
110                 gui(1,a[i][0],i,a[i][600]*2);
111             else
112                 gui(1,a[i][0],i,a[i][600]);
113             for (int j=x[0];j>=1;j--)
114                 printf("%d",x[j]);
115             printf("\n");
116         }
117         else
118             printf("no solution\n");
119     }
120     return 0;
121 }

 

NBUT 1224 Happiness Hotel 2010辽宁省赛