Project Euler 66: Diophantine equation
思路:
连分数求佩尔方程最小特解
模板:
LL a[];
bool min_pell(LL d, LL &x, LL &y) {
LL m = floor(sqrt(d+0.5));
if(m*m == d) return false;
int cnt = ;
a[cnt++] = m;
LL b = m, c = ;
double sq = sqrt(d);
do {
c = (d - b*b)/c;
a[cnt++] = (LL)floor((sq+b)/c);
b = a[cnt-]*c - b;
}while(a[cnt-] != *a[]);
LL p = , q = ;
for (int j = cnt-; j >= ; --j) {
LL t = p;
p = a[j]*p + q;
q = t;
}
if(cnt%) x = p, y = q;
else x = *p*p+, y = *p*q;
return true;
}
由于某些解超出long long范围,所以用到java大数
代码:
import java.math.*;
import java.util.*; public class Main {
public static long a[] = new long [200000];
public static BigInteger x, y;
public static boolean min_pell(long d) {
long m = (long)Math.floor(Math.sqrt(d+0.5));
if(m*m == d) return false;
int cnt = 0;
a[cnt++] = m;
long b = m, c = 1;
double sq = Math.sqrt(d);
do {
c = (d - b*b)/c;
a[cnt++] = (long)Math.floor((sq+b)/c);
b = a[cnt-1]*c - b;
}while(a[cnt-1] != 2*a[0]);
BigInteger p = BigInteger.ONE, q = BigInteger.ZERO;
for (int j = cnt-2; j >= 0; --j) {
BigInteger t = p;
p = p.multiply(BigInteger.valueOf(a[j])).add(q);
q = t;
}
if(cnt%2 != 0) {
x = p;
y = q;
}
else {
x = p.multiply(p).multiply(BigInteger.valueOf(2)).add(BigInteger.ONE);
y = p.multiply(q).multiply(BigInteger.valueOf(2));
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BigInteger mx = BigInteger.valueOf(0), ans = BigInteger.valueOf(5);
for (long d = 1; d <= 1000; ++d) {
if(!min_pell(d)) continue;
//System.out.println(x);
if(x.compareTo(mx) > 0) {
mx = x;
ans = BigInteger.valueOf(d);
}
}
System.out.println(ans); }
}
Project Euler 66: Diophantine equation的更多相关文章
- Project Euler 110:Diophantine reciprocals II 丢番图倒数II
Diophantine reciprocals II In the following equation x, y, and n are positive integers. For n = 4 th ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- project euler 169
project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...
- 【Project Euler 8】Largest product in a series
题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...
- Project Euler 第一题效率分析
Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...
随机推荐
- PHP洗牌、猴子选大王两个小算法
<一>洗牌算法 /** *洗牌算法washCard *@param $cardNum *@return array */ function washCard($cardNum) { $ca ...
- Nodejs在Ubuntu的部署和配置 samba
在Ubuntu上安装samba 在10.04上安装samba时,先把samba卸载,不然会影响后面的安装. 0.卸载samba sudo apt-get remove samba-common sud ...
- centos7 源码安装php7
因为php安装需要编译,所以服务器应该保证gcc和g++环境的安装 1. 首先释放安装包: tar -xvzf php-7.0.5.tar.gz cd php-7.0.5 2. 接下来进行参数配置,配 ...
- C# 截取两个字符串中间的子字符串
/// <summary> /// 截取中间字符 /// </summary> /// <param name="text">全字符串</ ...
- python 之路 day5 - 常用模块
模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configparser has ...
- go语言time包的使用
时间类型 time.Time类型表示时间. //时间类型 func timeDemo() { now := time.Now() fmt.Println(now) //2019-04-20 13:52 ...
- 记使用expo与expoKit分离工程遇到的坑
expoKit是支持expo平台的Objective-C和Java库,比纯RN一个个引入包开发效率会高一些,比如react-native-vector-icons包已经集成在expoKit中了. 假定 ...
- Visual Studio 2010 集成 SP1 补丁 制作 Visual Studio 2010 Service Pack 1 完整版安装光盘的方法
Now that Visual Studio 2010 SP1 has been released, administrators and developers may wish to install ...
- Http请求头和常见响应状态码
请求头: Accept:指浏览器或其他客户可以接爱的MIME文件格式.可以根据它判断并返回适当的文件格式. Accept-Charset:指出浏览器可以接受的字符编码.英文浏览器的默认值是ISO-88 ...
- C++ 创建快捷方式
https://blog.csdn.net/morewindows/article/details/6686683