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 ( ...
随机推荐
- date format 精辟讲解
link: http://stackoverflow.com/questions/19533933/nsdateformatter-how-to-convert-wed-23-oct-2013-045 ...
- sql server 数据页缓冲区的内存瓶颈分析
查看数据库的计数器: SELECT * FROM sys.dm_os_performance_counters **也可以使用系统的性能计监测器查看. 右键图表-> 添加计数器. ...
- Topcoder srm 632 div2
脑洞太大,简单东西就是想复杂,活该一直DIV2; A:水,基本判断A[I]<=A[I-1],ANS++; B:不知道别人怎么做的,我的是100*N*N;没办法想的太多了,忘记是连续的数列 我们枚 ...
- 一道题DP
Problem Description 小明明又被大威鱼抓住了,大威鱼把小明明关在地牢里,地牢由n * n 个房间组成,小明被困在地牢的最左上角的房间中,出口在最右下角,他想逃出这个诡异的地牢,但是他 ...
- codeforces #236 div2 简洁题解
A:A. Nuts time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- 真机模拟器.a文件编译报错
- 获取及管理Android 手机运营商及状态
主要类 TelephonyManager: telephonyManager.getCellLocation();//获得服务区 telephonyManager.getCellId();//获得服务 ...
- 转载——有感于三个50岁的美国程序员的生活状态与IT职业杂想
明天就是国庆节了,今天也不想干活干的太累了!写一篇以前去美国出差的杂想,对比于美国50多岁的程序员和大多数50多岁国内父母的生活状态有感而发. 前几年正好有一个项目的机会出差去了一次美国,地点是美国中 ...
- Jmeter正则表达式提取器的使用方法(转)
下面简单介绍一下Jmeter正则表达式提取器的使用方法. 1.添加Jmeter正则表达式提取器:在具体的Request下添加Jmeter正则表达式提取器(Jmeter正则表达式在“后置处理器”下面) ...