题目链接

思路:

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

参考博客

模板:

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. Google搜索中的突变XSS-JavaScript Library Introduced XSS Flaw in Google Search

    前言2018年9月26日,开源Closure库(最初由谷歌创建并用于谷歌搜索)的一名开发人员创建了一个提交,删除了部分输入过滤.据推测,这是因为开发人员在用户界面设计方面出现了问题.但此次提交的开发人 ...

  2. How to Preloading content with rel preload

    The preload value of the <link> element's rel attribute allows you to write declarative fetch ...

  3. leetcode143. Reorder List

    用快慢双指针,可以使慢指针到达中间的时候快指针到达最后一个元素(奇数),或者倒数第二个元素(偶数).慢指针后面的元素是后半个链表,把后半个链表进行reverse,然后再插在原来的链表中就可以了 /** ...

  4. 再次聊一聊promise settimeout asycn awiat执行顺序---js执行机制 EVENT LOOP

    首先js是单线程 分为同步和异步,异步又分为(macrotask 宏任务 和 microtask微任务 ), 这图还是很清晰嘛,再来一张 总结一下,就是遇到同步先执行同步,异步的丢到一边依次排队,先排 ...

  5. Cocos Creator_继承组件单例

    前言 单例,在游戏开发中是比较常用的功能,全局唯一,可以在任何地方直接获取, 省去了方法赋值 或者 属性面板拖动的麻烦. 普通单例_饿汉模式 不管有没调用,一开始就创建单例 1 // Singleto ...

  6. kali,parrot最新更新debain源

    deb http://mirrors.163.com/debian/ jessie main non-free contribdeb http://mirrors.163.com/debian/ je ...

  7. spring boot 整合js css 静态文件

    一,添加配置 spring: application: name: interview-server resources: static-locations: file:config/statics ...

  8. P3974 [TJOI2015]组合数学

    题目描述 为了提高智商,ZJY开始学习组合数学.某一天她解决了这样一个问题:给一个网格图,其中某些格子有财宝.每次从左上角出发,只能往右或下走.问至少要走几次才可能把财宝全捡完. 但是她还不知足,想到 ...

  9. Vim搜索、取消高亮、显示行数、取消行数

    1.显示行数 :set nu 2.取消行号 :set nu! 3.高亮搜索 /target 4.取消高亮 :noh

  10. 团队的Kick off

    团队名称:Super power 团队介绍:我们是一个积极向上不乏活力快咯的团队,在一起的任务是happy高效地完成我们的项目. 团队成员自我介绍: 李洪超(项目经理):(男,帅)性格内向,爱好学习, ...