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 ( ...
随机推荐
- ios 缓存相关信息收集
链接:http://www.cnblogs.com/pengyingh/category/353093.html 使用NSURLCache让本地数据来代替远程UIWebView请求 摘要: 原文作者: ...
- TGL 月光精品教程整理
月光站群培训班目录(不定时更新中) wordpress快速配置,插件,模板知识汇总(不定期更新) 适合国人的英文站思维(1) 英语思维-送给正在苦于学英语的童鞋 做seo的重要心态 seo排名难易度分 ...
- Uart串口与RS232串口的区别
Uart指的是TTL电平的串口:RS232指的是RS232电平的串口. TTL电平是3.3V的,而RS232是负逻辑电平,它定义+5~+12V为低电平,而-12~-5V为高电平. Uart串口的RXD ...
- EditorWindow edit ScriptableObject
using UnityEngine; [System.Serializable] public class Weapon { //[SerializeField] public string weap ...
- Unity3D 使用 Editor 脚本,自定义 脚本的属性面板
1. 先有一个普通的 继承自 MonoBehaviour 的脚本. 2. 创建一个 Editor 文件夹, 写 关于 UnityEditor 的脚本 都要放在这个文件夹下,不然会编译出错. 具体的实现 ...
- APM终端用户体验监控分析(下)
一.前言 [APM 终端用户体验监控分析(上)][1]从 APM 终端用户产品特性.使用建议.以及从[真实用户体验][2]和[模拟性能监控][3]两方面入手给大家进行了简单的分享. 本文为下篇,将给大 ...
- JAVA 异常对于性能的影响
陶炳哲 - MAY 12, 2015 在对OneAPM的客户做技术支持时,我们常常会看到很多客户根本没意识到的异常.在消除了这些异常之后,代码运行速度与以前相比大幅提升.这让我们产生一种猜测,就是在代 ...
- POJ 2185 Milking Grid (KMP,求最小覆盖子矩阵,好题)
题意:给出一个大矩阵,求最小覆盖矩阵,大矩阵可由这个小矩阵拼成.(就如同拼磁砖,允许最后有残缺) 正确解法的参考链接:http://poj.org/showmessage?message_id=153 ...
- [STL]heap和priority_queue
一.heap 在STL中,priority_queue(优先权队列)的底层机制是最大堆,因此有必要先来了解一下heap.heap采用完全二叉树的结构,当然不是真正的binary tree,因为对于完全 ...
- Javascript 正则表达式笔记
\d 元字符 + 量词 \w 常用的字符a-zA-Z0-9 .除回车之外的字符 ?0-1个字符 量词 只有前面是元字符,才变现量词 * 0-n 量词 /^\d+$/ 以字符开头,义字符结尾 [0-9] ...