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 ( ...
随机推荐
- bzoj 3232 01分数规划+最大权封闭子图判定
我们的目标是使v/c最小化,所以构造函数g(x)=v-x*c,那么 二分一个X,判断当时的v-x*c的值是多少,然后根据g(x)函数的 单调递减性来二分,判断,直到g(x)=0的时候当前的X就是答案. ...
- Leetcode#117 Populating Next Right Pointers in Each Node II
原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...
- 第k短路
poj 2449 模板题 A*+spfa #include<iostream> #include<cstdio> #include<cstring> #inclu ...
- Javascript动态生成表格的性能调优
vision 0.8 [耗时672ms]终极优化 将字符串作为数组对象的方式是目前效率最高,性能最优的方式. <script> var t1 = new Date(); < ...
- .NET设计模式(12):外观模式(Façade Pattern)(转)
概述 在软件开发系统中,客户程序经常会与复杂系统的内部子系统之间产生耦合,而导致客户程序随着子系统的变化而变化.那么如何简化客户程序与子系统之间的交互接口?如何将复杂系统的内部子系统与客户程序之间的依 ...
- Mac OS 上设置 JAVA_HOME
Mac OS 上设置 JAVA_HOME 原文链接:http://han.guokai.blog.163.com/blog/static/136718271201301183938165/ 由于需要, ...
- 收集几个Web前端UI框架
原文:http://www.isaced.com/post-200.html 关于Web前端UI库/框架,我觉得是非常方便的东西,对于我们这种业余的Web开发人员,有时候要写点前端代码的时候把UI框架 ...
- iOS生成本地随机验证码
原文链接:http://www.cnblogs.com/jerehedu/p/4527707.html 效果图:
- light oj 1068 - Investigation 数位DP
思路:典型的数位DP!!! dp[i][j][k]:第i位,对mod取余为j,数字和对mod取余为k. 注意:由于32位数字和小于95,所以当k>=95时,结果肯定为0. 这样数组就可以开小点, ...
- IEEE 802.3 Ethernet
Introduction Ethernet 是过去30年以来最为成功的局域网(local area networking)技术. 1. First widely used LAN technology ...