SPOJ 1739 Yet Another Equation(Pell方程)
2013-12-02 19:42
393 查看
题目链接:http://www.spoj.com/problems/EQU2/
题意:给出方程x^2-n*y^2=1的最小整数解。
思路:参见金斌大牛的论文《欧几里得算法的应用》。
题意:给出方程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 1771 NQUEEN Yet Another N-Queen Problem
- 2012ICPC长春站 K Yet Another Multiple Problem 【BFS+同余模定理】
- 【BZOJ】【P2225】【Spoj 2371】【Another Longest Increasing】【树套树】
- 【矩阵快速幂】UVA 10698 G - Yet another Number Sequence
- Educational Codeforces Round 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence
- HDU 4596 Yet another end of the world(一阶不定方程是否可解,gcd)
- YetAnotherForum集成AD用户混合登录(三) enhance DB.cs
- spoj 1470 Another Sequence Problem
- 【 CodeForces - 392C】 Yet Another Number Sequence (二项式展开+矩阵加速)
- An example of delegate Command to yet another Command.
- Yacc: Yet Another Compiler-Compiler
- 高精度 I - Yet another A + B
- Yet Another PhotoMosaic Generator
- ZOJ 3964 Yet Another Game of Stones(博弈论)
- [HDOJ4596] Yet another end of the world(数学,gcd,二元一次方程)
- Yet another implementation of a lock-free circular array queue
- YARN(YetAnother Resource Negotiator)
- Non-square Equation(数学问题方程转化)
- K - Yet Another Story of Rock-paper-scissors
- 12年成都现场赛K_Yet Another Multiple Problem