NEU 1009: Happiness Hotel 数论 佩尔方程

//此是标准程序
#include <iostream>
#include <cmath>
using namespace std;
int can[1005]={0};
int a[10005][605]={0};
int x[6005],y[6005],t[6005];
int h1,h2;
int bb,ee,xx,yx,c,n;
void gui(int ji,int many,int ma,int kk);
int main()
{
// freopen("exp.in","r",stdin);
// freopen("exp.out","w",stdout);
for (int i=1;i<=31;i++)
can[i*i]=true;
for (int i=1;i<=1000;i++)
if (!can[i])
{
a[i][600]=1;
bb=1;
ee=(int)sqrt(i);
a[i][0]=ee;
ee=-ee;
xx=bb;
yx=ee;
xx=-yx;
yx=i-yx*yx;
n=0;
while ((xx-yx)*(xx-yx)<i||xx>=0)
{
xx-=yx;
n++;
}
a[i][1]=n;
c=xx;
xx=yx;
yx=c;
while (xx!=bb||yx!=ee)
{
a[i][600]++;
c=xx;
xx=-yx;
yx=i-yx*yx;
yx=yx/c;
n=0;
while ((xx-yx)*(xx-yx)<i||xx>=0)
{
xx-=yx;
n++;
}
a[i][a[i][600]]=n;
c=xx;
xx=yx;
yx=c;
}
}
int i;
while (scanf("%d",&i)!=EOF)
if (!can[i])
{
if (a[i][600]%2)
gui(1,a[i][0],i,a[i][600]*2);
else
gui(1,a[i][0],i,a[i][600]);
for (int j=x[0];j>=1;j--)
printf("%d",x[j]);
printf("\n");
}
else
printf("no solution\n");
// system("pause");
return 0;
}
void gui(int ji,int many,int ma,int kk)
{
if (ji<kk)
gui(ji+1,a[ma][(ji-1)%a[ma][600]+1],ma,kk);
else
{
h1=1;
h2=1;
x[1]=many;
y[1]=1;
return;
}
for (int i=1;i<=h1;i++)
t[i]=x[i];
for (int i=1;i<=h2;i++)
x[i]=y[i];
for (int i=1;i<=h1;i++)
y[i]=t[i];
c=h1;
h1=h2;
h2=c;
for (int i=1;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=1;i<h1;i++)
if (x[i]>=10)
{
x[i+1]+=x[i]/10;
x[i]%=10;
}
while (x[h1]>=10)
{
x[h1+1]=x[h1]/10;
x[h1]%=10;
h1++;
}
x[0]=h1;
}

题目描述:

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,然后。冯斐神牛跟我说这是佩尔方程。然后此题就可以完美解决了。  下面贴的程序是标准程序,非本人所写,特此注明!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值