#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;
}
/*非常裸的佩尔方程模板,输入一个数k,求方程ans^2 = k*n*n+1 中ans的最小整数解(n是大于等于一的整数,无上限),多积累数论,这个原理仍待了解,//求出p2 - D * q2 = 1的基解(最小正整数解),这个可能溢出,有必要的话,用java, 写成类比较好
bool PQA(LLI D, LLI &p, LLI &q) {//来自于PQA算法的一个特例
    LLI d = sqrt(D);
    if ((d + 1) * (d + 1) == D) return false;
    if (d * d == D)             return false;
    if ((d - 1) * (d - 1) == D) return false;//这里是判断佩尔方程有没有解
    LLI u = 0, v = 1, a = int(sqrt(D)), a0 = a, lastp = 1, lastq = 0;
    p = a, q = 1;
    do {
        u = a * v - u;
        v = (D - u * u) / v;
        a = (a0 + u) / v;
        LLI thisp = p, thisq = q;
        p = a * p + lastp;
        q = a * q + lastq;
        lastp = thisp;
        lastq = thisq;
    } while ((v != 1 && a <= a0));//这里一定要用do~while循环
    p = lastp;
    q = lastq;
    //这样求出后的(p,q)是p2 – D * q2 = (-1)k的解,也就是说p2 – D * q2可能等于1也可能等于-1,如果等于1,(p,q)就是解,如果等于-1还要通过(p2 + D * q2,2 * p * q)来求解,如下
    if (p * p - D * q * q == -1) {
        p = lastp * lastp + D * lastq * lastq;
        q = 2 * lastp * lastq;
    }
    return true;
}
*/
 

2010辽宁省赛G(佩尔方程)的更多相关文章

  1. ZOJ 1985 Largest Rectangle in a Histogram(刷广告)2010辽宁省赛

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21204 ...

  2. NBUT 1221 Intermediary 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB It is widely known that any two strangers can get to know ...

  3. 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 enoug ...

  4. NBUT 1222 English Game 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB This English game is a simple English words connection gam ...

  5. NBUT 1225 NEW RDSP MODE I 2010辽宁省赛

    Time limit  1000 ms Memory limit  131072 kB Little A has became fascinated with the game Dota recent ...

  6. NBUT 1218 You are my brother 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB Little A gets to know a new friend, Little B, recently. On ...

  7. NBUT 1220 SPY 2010辽宁省赛

    Time limit  1000 ms Memory limit  131072 kB The National Intelligence Council of X Nation receives a ...

  8. NBUT 1219 Time 2010辽宁省赛

    Time limit   1000 ms Memory limit   131072 kB Digital clock use 4 digits to express time, each digit ...

  9. NBUT 1223 Friends number 2010辽宁省赛

    Time limit  1000 ms Memory limit   131072 kB Paula and Tai are couple. There are many stories betwee ...

随机推荐

  1. python cookbook第三版学习笔记六:迭代器与生成器

    假如我们有一个列表 items=[1,2,3].我们要遍历这个列表我们会用下面的方式 For i in items:   Print i 首先介绍几个概念:容器,可迭代对象,迭代器 容器是一种存储数据 ...

  2. Java异步编程第2篇

    假如如今有一个Buttonbutton,Buttonbutton上有click和doubleclick事件. 两个不同的事件须要进行不同的处理.这时候就须要为对应的事件注冊Listener了.改动后的 ...

  3. JDBC通用方法实现

    在一些测试项目中会用到纯粹的jdbc操作数据库,下面提供统一的方法实现. import java.sql.CallableStatement; import java.sql.Connection; ...

  4. systemclock sleep 睡眠

  5. 《avascript 高级程序设计(第三版)》 ---第二章 在HTML中使用Javascript

    本章主要讲解了,怎么在HTML中使用: 1.<script src=""></script> 属性:defer="defer" 表示脚本 ...

  6. 关于connect: network is unreachable 问题的解决【转】

    本文转载自:https://blog.csdn.net/liukun321/article/details/6662950 由于发现原创文章,在未署名作者及出处的情况下被转载.在以后所有的原创文章开头 ...

  7. Rabbitmq+Nginx+keepalived高可用热备

    摘自: http://www.cnblogs.com/wangyichen/p/4917241.html 公司两台文件服务器要做高可用,避免单点故障,故采用keepalived实现,其中一台宕机,依靠 ...

  8. zabbix 监控 haproxy 记录

    1. 当配置好后,遇到 Value should be a JSON object. 该如何是好? sudo 文件增加: zabbix ALL=(ALL) NOPASSWD: ALL haproxy ...

  9. 纯CSS3左右滑动开关按钮

    纯CSS3特效左右滑动开关按钮是一款非常酷的CSS3 3D开关按钮,点击按钮可以左右滑动,就像开关打开闭合一样的效果. http://www.huiyi8.com/sc/10626.html

  10. Spring MVC文件上传下载工具类

    import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import ...