题目链接

思路:

连分数求佩尔方程最小特解

参考博客

模板:

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的更多相关文章

  1. 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 ...

  2. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  3. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  4. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  5. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  6. 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 ...

  7. project euler 169

    project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...

  8. 【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 × ...

  9. Project Euler 第一题效率分析

    Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...

随机推荐

  1. (转载)MyBatis传入多个参数的问题

    原文地址:https://www.cnblogs.com/mingyue1818/p/3714162.html 一.单个参数: public List<XXBean> getXXBeanL ...

  2. Oracle 10g RAC OCR、Voting disk更换

    环境:OEL 5.7 + Oracle 10.2.0.5 RAC 需求:更换存储,OCR.Voting disk同时需要更换到新存储. 1.替换OCR 2.替换voting disk 1.替换OCR ...

  3. git二、基本使用

    1:创建仓库  git init - 当前目录下初始化仓库,根目录产生.git文件-包含元数据文件,为其他git命令提供环境 2:克隆仓库  git clone url - 拷贝一个 Git 仓库到本 ...

  4. BigDecimal源码

    1 public BigDecimal(char[] in, int offset, int len, MathContext mc) {// 使用字符数组的构造方法,一般我们推荐使用的是一Strin ...

  5. Android -- Glide框架详解(一)

    1,使用这个框架快两年了,今天去github上去看了一下,貌似已经从3.X升级到4.X了,想着自己还没有对这个框架在博客上做过总结,所以这里打算出三篇博客来介绍,内容有基本使用.3.X与4.X的不通. ...

  6. Oracle函数中对于NO_DATA_FOUND异常处理的研究

    一直以来有一个困惑,一直没解决,昨天一哥们问我这个问题,决心弄清楚,终于得到了答案.先看下面这个函数: create or replace function fn_test(c_xm varchar) ...

  7. 关于Java多线程的线程同步和线程通信的一些小问题(顺便分享几篇高质量的博文)

    Java多线程的线程同步和线程通信的一些小问题(顺便分享几篇质量高的博文) 前言:在学习多线程时,遇到了一些问题,这里我将这些问题都分享出来,同时也分享了几篇其他博客主的博客,并且将我个人的理解也分享 ...

  8. fusioncharts的3D饼图固定大小和角度

    3D饼图的pieRadius和startingAngle属性 pieRadius:饼图的半径 startingAngle:饼图的角度(旋转) 在固定大小的div里面,饼图上如果显示label或者val ...

  9. Hadoop 进程配置总结

    HDFS: NameNode: core-site.xml <property> <name>fs.defaultFS</name> <value>hd ...

  10. tiny6410 烧写uboot 转载

    #烧录 参考: 03- Tiny6410刷机指南.pdf 假设拿到的Tiny6410开发板没有提前下载任何程序,包括Bootloader. ##Bootloader - Superboot Super ...