SPOJ 1739 Yet Another Equation(Pell方程)
题目链接: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方程)的更多相关文章
- Pell方程及其一般形式
一.Pell方程 形如x^2-dy^2=1的不定方程叫做Pell方程,其中d为正整数,则易得当d是完全平方数的时候这方程无正整数解,所以下面讨论d不是完全平方数的情况. 设Pell方程的最小正整数解为 ...
- hdu3293(pell方程+快速幂)
裸的pell方程. 然后加个快速幂. No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others) Memory Limit: ...
- HDU 2281 Square Number Pell方程
http://acm.hdu.edu.cn/showproblem.php?pid=2281 又是一道Pell方程 化简构造以后的Pell方程为 求出其前15个解,但这些解不一定满足等式,判断后只有5 ...
- POJ 1320 Street Numbers Pell方程
http://poj.org/problem?id=1320 题意很简单,有序列 1,2,3...(a-1),a,(a+1)...b 要使以a为分界的 前缀和 和 后缀和 相等 求a,b 因为序列很 ...
- POJ 2427 Smith's Problem Pell方程
题目链接 : http://poj.org/problem?id=2427 PELL方程几个学习的网址: http://mathworld.wolfram.com/PellEquation.html ...
- 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 ...
- [LeetCode] Solve the Equation 解方程
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...
- eikonal equation - 程函方程
[转载请注明出处]http://www.cnblogs.com/mashiqi 2018/08/08 eikonal equation如下:$$|\nabla_x \tau (x)| = n(x).$ ...
- hdu2199Can you solve this equation?(解方程+二分)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- C#_音乐播放器_用ListBox显示歌词
在用ListBox显示歌词的时候,可以显示多行,同时可以控制每一行显示的样式等等.控制显示样式是在它的DrawItem事件中来控制的.首先要先将ListBox的DrawMode属性设置为OwnerDr ...
- java中byte和blob互转
1. btye[]转blob byte[] bs = ... Blob blob = conn.createBlob(); blob.setBytes(1, bs); ps.setBlob(2, bl ...
- 对LVS DR模式的理解
Client向vip发请求,lvs接收 Src mac Dst mac type … Src ip Src port Dst ip Dst port … CRC Mac1 Mac2 … … 192.1 ...
- VSFTPD全攻略(/etc/vsftpd/vsftpd.conf文件详解)
/etc/vsftpd/vsftpd.conf文件详解,分好类,方便大家查找与学习 #################匿名权限控制############### anonymous_enable=YE ...
- E3: PS4/PC 莎木3 众筹200万美元 9小时内达成
这次E3任天堂没有新掌机(传说中的XDS呢.呵呵)和口袋的消息, 被喷得很严重, 索尼的FF7重制版和莎木3是亮点. 而莎木3的众筹速度据说创了记录, 玩家的情怀大胜. 笔者看到国内也有赞助几十刀的玩 ...
- 在JavaScript中判断整型的N种方法
原文:http://www.cnblogs.com/YcYYcY/p/3759184.html 整数类型(Integer)在JavaScript经常会导致一些奇怪的问题.在ECMAScript的规范中 ...
- URL编码方法比较
javascript中存在几种对URL字符串进行编码的方法:escape(),encodeURI(),以及encodeURIComponent().这几种编码所起的作用各不相同. escape() 方 ...
- 帝国cms无法注册登录快速解决方法 附路径
帝国cms安装相对比较简单,一路next,一般从网上下载的系统都会带一些数据,恢复备份数据后,清除缓存,更新数据,一个copy版的网站就出来了.但是为了se的友好需要改动很多地方,不然很容易被认为是s ...
- IE6下position解决父元素被撑开的问题
在IE6下面当子元素的宽度/高度大于父元素时, 父元素的宽度/高度就被撑开.IE7以上是不会被撑开的 <style> .f{width:100px; height:100px; backg ...
- LVM quick start
这里记录一些任务用到的快速命令,详细LVM管理可参考: http://wenku.baidu.com/view/c29b8bc4bb4cf7ec4afed0ad.html 1.把home分区的磁盘空间 ...