题目链接:http://www.spoj.com/problems/EQU2/

题意:给出方程x^2-n*y^2=1的最小整数解。

思路:参见金斌大牛的论文《欧几里得算法的应用》。

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {

    static BigInteger ONE=BigInteger.valueOf(1);
    static BigInteger ZERO=BigInteger.valueOf(0);

    public static BigInteger V(int x)
    {
        return BigInteger.valueOf(x);
    }
    public static void main(String[] args) {
        Scanner S=new Scanner(System.in);
        int T;
        T=S.nextInt();
        while(T--!=0)
        {
            BigInteger p0,p1,p2,q0,q1,q2,g0,g1,h0,h1,a,a0,n;
            n=S.nextBigInteger();
            p0=ZERO; p1=ONE;
            q0=ONE; q1=ZERO;
            a0=a=V((int)Math.sqrt(n.intValue()));
            g0=ZERO; h0=ONE;
            while(true)
            {
                g1=ZERO.subtract(g0).add(a.multiply(h0));
                h1=n.subtract(g1.multiply(g1)).divide(h0);
                p2=a.multiply(p1).add(p0);
                q2=a.multiply(q1).add(q0);
                a=g1.add(a0).divide(h1);
                if(p2.multiply(p2).subtract(n.multiply(q2).multiply(q2)).compareTo(ONE)==0)
                {
                    break;
                }
                p0=p1; p1=p2;
                q0=q1; q1=q2;
                g0=g1; h0=h1;
            }
            System.out.print(p2);
            System.out.print(' ');
            System.out.println(q2);
        }
    }
}

  

SPOJ 1739 Yet Another Equation(Pell方程)的更多相关文章

  1. Pell方程及其一般形式

    一.Pell方程 形如x^2-dy^2=1的不定方程叫做Pell方程,其中d为正整数,则易得当d是完全平方数的时候这方程无正整数解,所以下面讨论d不是完全平方数的情况. 设Pell方程的最小正整数解为 ...

  2. hdu3293(pell方程+快速幂)

    裸的pell方程. 然后加个快速幂. No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: ...

  3. HDU 2281 Square Number Pell方程

    http://acm.hdu.edu.cn/showproblem.php?pid=2281 又是一道Pell方程 化简构造以后的Pell方程为 求出其前15个解,但这些解不一定满足等式,判断后只有5 ...

  4. POJ 1320 Street Numbers Pell方程

    http://poj.org/problem?id=1320 题意很简单,有序列 1,2,3...(a-1),a,(a+1)...b  要使以a为分界的 前缀和 和 后缀和 相等 求a,b 因为序列很 ...

  5. POJ 2427 Smith's Problem Pell方程

    题目链接 :  http://poj.org/problem?id=2427 PELL方程几个学习的网址: http://mathworld.wolfram.com/PellEquation.html ...

  6. HDU 6222 Heron and His Triangle (pell 方程)

    题面(本人翻译) A triangle is a Heron's triangle if it satisfies that the side lengths of it are consecutiv ...

  7. [LeetCode] Solve the Equation 解方程

    Solve a given equation and return the value of x in the form of string "x=#value". The equ ...

  8. eikonal equation - 程函方程

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2018/08/08 eikonal equation如下:$$|\nabla_x \tau (x)| = n(x).$ ...

  9. hdu2199Can you solve this equation?(解方程+二分)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

随机推荐

  1. noi2006day2_最大获利 网络流

    这道题是上一题的数据加强版,dinic表示毫无压力: #include<iostream> #include<cstdio> #include<cstring> # ...

  2. AutoResetEvent 运用

    static AutoResetEvent are = new AutoResetEvent(true);//初始化为开 static void Main(string[] args) { //如果这 ...

  3. 利用URLRewriter.dll 实现ASP.NET实现伪静态

    大家一定经常在网络上看到很多网站的地址后缀都是用XX.HTML或者XX.ASPX等类似静态文件的标示来操作的吧,那么大家有怀疑过他真的是一个一个的静态生成的文件么,静态文件的生成的优缺有好有坏,对于访 ...

  4. 在 OS X Yosemite 中部署Mesos

    1)从mesos的官网下载mesos的最新稳定版本:http://mesos.apache.org/downloads/,本文为mesos-0.22.1版本. 2)移动至你喜欢的目录(你在该目录下具有 ...

  5. sematext

    https://sematext.atlassian.net/wiki/display/PUBLOGSENE/Syslog

  6. poj 3710 Christmas Game 博弈论

    思路:首先用Tarjan算法找出树中的环,环为奇数变为边,为偶数变为点. 之后用博弈论的知识:某点的SG值等于子节点+1后的异或和. 代码如下: #include<iostream> #i ...

  7. centOS学习part3:远程工具VNC的安装与配置

    0 上一篇(http://www.cnblogs.com/souvenir/p/3875484.html)我们介绍了通过yum安装JDK的实例,初步见识了yum命令的强大.本章我们将继续使用yum命令 ...

  8. python_ftplib实现通过FTP下载文件

    1.  Ftplib常用函数介绍 Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,本次主要介绍连接FTP并且进行文件下载功能,可 ...

  9. 美丽渐变色的Form

    一直都非常喜欢渐变色的界面,但是没想到漂亮的渐变Form原来这么简单...实在是没想到...看来我不仅技术水平低,脑袋里的创意也是空空如也... --------------------------- ...

  10. 关于html5与jsp页面同样的html代码展示的页面效果不一样的问题

    原文:关于html5与jsp页面同样的html代码展示的页面效果不一样的问题 html5默认的声明为 <!DOCTYPE html> jsp默认的声明头部为 <%@ page con ...