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: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...
随机推荐
- SQL Server脚本
-- 清楚缓冲区 DBCC DROPCLEANBUFFERS -- 删除计划高速缓存中的元素 DBCC FREEPROCCACHE -- 执行时间 SET STATISTICS TIME ON -- ...
- EF:分页查询 + 条件查询 + 排序
/// <summary> /// linq扩展类---zxh /// </summary> /// <typeparam name="T">& ...
- HTML表单简单练习
代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...
- 论文阅读(Weilin Huang——【arXiv2016】Accurate Text Localization in Natural Image with Cascaded Convolutional Text Network)
Weilin Huang——[arXiv2016]Accurate Text Localization in Natural Image with Cascaded Convolutional Tex ...
- linux下tomcat的https访问
1.安装JDK(省略…) 2.安装tomcat(省略…) 3.配置SSL 进入JDK的安装目录 # cd /usr/java/jdk1..0_03/bin # ./keytool -genkey -a ...
- Windows中的原语与原子
目前对原语与原子的理解为: 原语: 由内核提供的核外调用的一段具有特定功能的方法或者函数称之为---原语 原语操作不允许发生中断. 原子: 在多进程多线程的操作系统中不允许其他进程或者 ...
- MyBatis探究-----传递参数详解
1.单个参数 mybatis不会做特殊处理,#{参数名/任意名}:取出参数值 例如:接口中方法 public Employee getEmpById(String empId); XML中 <s ...
- 用Java实现图片验证码功能
一.什么是图片验证码? 可以参考下面这张图: 我们在一些网站注册的时候,经常需要填写以上图片的信息. 1.图片生成实体类: package com.hexianwei.graphic; import ...
- PostgreSql之在group by查询下拼接列字符串
首先创建group_concat聚集函数: CREATE AGGREGATE group_concat(anyelement) ( sfunc = array_append, -- 每行的操作函数,将 ...
- Liunx中三种网络模式配置及Xshell连接
Liunx网络配置 NAT模式下的网络配置: 首先打开网络配置文件:vi /etc/sysconfig/network-scripts/ifcfg-ens33 修改网卡信息,配置动态Ip过程中,只 ...