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
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
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
#include <iostream>
#include <cmath>
using namespace std; int can[];
int a[][];
int x[],y[],t[];
int h1,h2;
int bb,ee,xx,yx,c,n; void gui(int ji,int many,int ma,int kk)
{
if (ji<kk)
gui(ji+,a[ma][(ji-)%a[ma][]+],ma,kk);
else
{
h1=;
h2=;
x[]=many;
y[]=;
return;
}
for (int i=;i<=h1;i++)
t[i]=x[i];
for (int i=;i<=h2;i++)
x[i]=y[i];
for (int i=;i<=h1;i++)
y[i]=t[i];
c=h1;
h1=h2;
h2=c;
for (int i=;i<=h2;i++)
{
if (i<=h1)
x[i]+=many*y[i];
else
x[i]=many*y[i];
}
if (h2>h1)
h1=h2;
for (int i=;i<h1;i++)
{
if (x[i]>=)
{
x[i+]+=x[i]/;
x[i]%=;
}
}
while (x[h1]>=)
{
x[h1+]=x[h1]/;
x[h1]%=;
h1++;
}
x[]=h1;
} int main()
{ for (int i=;i<=;i++)
can[i*i]=true;
for (int i=;i<=;i++)
if (!can[i])
{
a[i][]=;
bb=;
ee=(int)sqrt((double)i);
a[i][]=ee;
ee=-ee;
xx=bb;
yx=ee;
xx=-yx;
yx=i-yx*yx;
n=;
while ((xx-yx)*(xx-yx)<i||xx>=)
{
xx-=yx;
n++;
}
a[i][]=n;
c=xx;
xx=yx;
yx=c;
while (xx!=bb||yx!=ee)
{
a[i][]++;
c=xx;
xx=-yx;
yx=i-yx*yx;
yx=yx/c;
n=;
while ((xx-yx)*(xx-yx)<i||xx>=)
{
xx-=yx;
n++;
}
a[i][a[i][]]=n;
c=xx;
xx=yx;
yx=c;
}
}
int i;
while (scanf("%d",&i)!=EOF)
{
if (!can[i])
{
if (a[i][]%)
gui(,a[i][],i,a[i][]*);
else
gui(,a[i][],i,a[i][]);
for (int j=x[];j>=;j--)
printf("%d",x[j]);
printf("\n");
}
else
printf("no solution\n");
}
return ;
}