NBUT 1224 - Happiness Hotel【佩尔方程】

Happiness Hotel
Time Limit:1000MS     Memory Limit:131072KB
Description:
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



题意:
输入一个整数k,求解使得方程 ans ^ 2 = k * n ^ 2 + 1 成立的 ans 最小值(n是大于1的整数)

算法思路:
从题意可以看出是佩尔方程,直接调用佩尔方程模板解决

代码实现:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
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, yy, c, n;

void Pell(int ji,int many,int ma,int kk)
{
    if (ji < kk)
        Pell(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;
}

void solve()
{
    int i, j;
    for (j = 1; j <= 31; j++)
        can[j*j] = true;
    for(i = 1; i <= 1000; i++)
    {
        if(!can[i])
        {
            a[i][600]=1;
            bb = 1;
            ee = (int)sqrt((double)i);
            a[i][0] = ee;
            ee =- ee;
            xx = bb;
            yy = ee;
            xx =- yy;
            yy = i - yy * yy;
            n=0;
            while((xx - yy) * (xx - yy) < i || xx >= 0)
            {
                xx -= yy;
                n++;
            }
            a[i][1] = n;
            c = xx, xx = yy, yy = c;
            while(xx != bb || yy != ee)
            {
                a[i][600]++;
                c = xx;
                xx =- yy;
                yy = i - yy * yy;
                yy = yy / c;
                n = 0;
                while ((xx - yy) * (xx - yy) < i || xx >= 0)
                {
                    xx -= yy;
                    n++;
                }
                a[i][a[i][600]] = n;
                c = xx, xx = yy, yy = c;
            }
        }
    }
}

int main()
{
    int k;
    solve();
    while(scanf("%d",&k)!=EOF)///ans^2 = k * n ^ 2 + 1,求ans的最小值(k <= 1000)
    {
        if(!can[k])
        {
            if(a[k][600] % 2)
                Pell(1, a[k][0], k, a[k][600]*2);
            else
                Pell(1, a[k][0], k, a[k][600]);
            for (int j = x[0]; j >= 1; j--)
                printf("%d",x[j]);
            printf("\n");
        }
        else
            printf("no solution\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值