题目链接

思路:

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

参考博客

模板:

LL a[20000];
bool min_pell(LL d, LL &x, LL &y) {LL m = floor(sqrt(d+0.5)); if(m*m == d) return false;int cnt = 0;a[cnt++] = m;LL b = m, c = 1;double sq = sqrt(d);do {c = (d - b*b)/c;a[cnt++] = (LL)floor((sq+b)/c);b = a[cnt-1]*c - b;}while(a[cnt-1] != 2*a[0]);LL p = 1, q = 0;for (int j = cnt-2; j >= 0; --j) {LL t = p;p = a[j]*p + q;q = t;} if(cnt%2) x = p, y = q;else x = 2*p*p+1, y = 2*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 stubBigInteger 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);}
}

转载于:https://www.cnblogs.com/widsom/p/10415547.html

Project Euler 66: Diophantine equation相关推荐

  1. Project Euler Problem 66

    Problem 66 Consider quadratic Diophantine equations of the form: x2 – Dy2 = 1 For example, when D=13 ...

  2. 硬币游戏 Project Euler 232

    原帖:http://hi.baidu.com/atyuwen/blog/item/160bd024531e3034c995591d.html Project Euler上最近的题目都还比较意思,来看看 ...

  3. [Project Euler] 来做欧拉项目练习题吧: 题目004

        [Project Euler] 来做欧拉项目练习题吧: 题目004 周银辉 问题描述: A palindromic number reads the same both ways. The l ...

  4. [Project Euler] 来做欧拉项目练习题吧: 题目017

    [Project Euler] 来做欧拉项目练习题吧: 题目017 周银辉 题目描述: If the numbers 1 to 5 are written out in words: one, two ...

  5. [Project Euler] 来做欧拉项目练习题吧: 题目012

      [Project Euler] 来做欧拉项目练习题吧: 题目012 周银辉 问题描述: The sequence of triangle numbers is generated by addin ...

  6. Project Euler

    最近发现了一个很有趣的网站,Project Euler 上面全是数学题,不过大多需要用编程解决 Problem 3: 求:600851475143的最大素因子. 解:编了个程序,迅速水过,看官方的题解 ...

  7. Project Euler Problem 27小结

    Project Euler上有很多有意思的问题,刚做到第27题,对这个问题做个小结. Problem 27: Euler有一个著名的方程n^2+n+41,当n=0到39时,方程结果均为质数.如今人们用 ...

  8. Project Euler Problem 104 Pandigital Fibonacci ends

    Pandigital Fibonacci ends Problem 104 The Fibonacci sequence is defined by the recurrence relation: ...

  9. Project Euler 3 Largest prime factor

    Project Euler 3 Largest prime factor ''' The prime factors of 13195 are 5, 7, 13 and 29. What is the ...

最新文章

  1. 【重磅】吴恩达宣布 Drive.ai 自动驾驶汽车服务落地 理想就这样成了现实!
  2. 设计模式 -- 策略模式
  3. cactiez----监控windows主机 server 2008 r2
  4. gatdata获取曲线_GetData软件使用--获取曲线图中的数据
  5. C# NPOCO 轻量级ORM框架(进阶)
  6. Qt工程pro文件配置详解
  7. Ubuntu 安装 typora
  8. 单元测试的必要性 从bug修复 费用成本和时间成本综合考虑
  9. 图像变换中的常用插值方法(含公式推导)
  10. 服务器密码机的标准和产品应用要点
  11. WorldView卫星遥感影像数据/米级分辨率遥感影像
  12. VSTO:使用C#开发Excel、Word【10】
  13. Django框架之美多商城项目
  14. 微信公众号拉取扫码功能
  15. ubuntu18.04添加自定义分辨率
  16. Uniswap计算过程推演
  17. 清分系统层即综合中央计算机,轨道交通AFC系统介绍
  18. win7 使用mklink瘦身C盘空间
  19. [附源码]Java计算机毕业设计SSM道路桥梁工程知识文库系统
  20. uva11300(数学)

热门文章

  1. WDF框架下 硬盘的虚拟
  2. SqlSever2005 一千万条以上记录分页数据库优化经验总结【索引优化 + 代码优化】一周搞定...
  3. 从动态表单得到的一些思考
  4. Git系列一之安装管理
  5. 『Spring.Net』为什么使用?
  6. C++ 中宏的使用 --来自:http://blog.csdn.net/hgl868/article/details/7058906
  7. 调用函数的ALV、面向对象的ALV设置带选择列
  8. 判断一个字符串的字符是不是唯一
  9. Python安装时import matplotlib.pyplot as plt报错
  10. 大数据开发笔记(一):HDFS介绍