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: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...
随机推荐
- stm32定时器输出移相PWM(非主从模式)
背景:由于项目需要,需要stm32输出任意相角度的PWM 前提知识: 1.stm32定时器的Tim,一般有多个OC.具体差别根据型号来定. 2.定时器的使能,理论上是多个通道同时使能 3.TIM_OC ...
- Cocos Creator 获取当前URL取参数
利用Javascript获取当前页的URL,这个问题起来好像很复杂,如果第一次去想这个问题,很多人估计又在琢磨到底又是哪个神一般的Javascript函数. 其实不是,Javascript获取当前页的 ...
- python中利用matplotlib绘图可视化知识归纳
python中利用matplotlib绘图可视化知识归纳: (1)matplotlib图标正常显示中文 import matplotlib.pyplot as plt plt.rcParams['fo ...
- matlab——之class类(详细总结)
https://blog.csdn.net/qinze5857/article/details/80545885 开篇:搜了一下网上介绍matlab的class类,信息不全,且总结不全面,于是单独he ...
- C# 数组在内存中的存储
C# 数组是引用类型,那么在内存中是如何存储的呢? 在VS中调试C#程序,如何查看内存.寄存器.反汇编 在这篇文章里看到了如何在VS 中查看内存 先断点打在数组创建后语句那里,点debug->W ...
- [小程序] 微信小程序 picker 中range-key中必须带单引号
原文地址:http://blog.csdn.net/u012329294/article/details/74906504 <view class="section"> ...
- 《ASP.NET Core In Action》读书笔记系列二 ASP.NET Core 能用于什么样的应用,什么时候选择ASP.NET Core
ASP.NET Core 能用于什么样的应用 ASP.NET Core 可以用作传统的web服务.RESTful服务.远程过程调用(RPC)服务.微服务,这归功于它的跨平台支持和轻量级设计.如下图所示 ...
- uva 10123 - No Tipping dp 记忆化搜索
这题的题意是 在双脚天平上有N块东西,依次从上面取走一些,最后使得这个天平保持平衡! 解题: 逆着来依次放入,如果可行那就可以,记得得有木板自身的重量. /********************** ...
- linux重启Oracle服务
linux重启oracle服务命令(完整版) (1) 以oracle身份登录数据库,命令:su – oracle (2) 进入Sqlplus控制台,命令:sqlplus /nolog (3) 以系统管 ...
- minSdkVersion
10 It is indeed possible to increase minSdkVersion, but it took me way too much time to find it ou ...