Project Euler 108:Diophantine reciprocals I 丢番图倒数I
In the following equation x, y, and n are positive integers.

For n = 4 there are exactly three distinct solutions:

What is the least value of n for which the number of distinct solutions exceeds one-thousand?
NOTE: This problem is an easier version of Problem 110; it is strongly advised that you solve this one first.
在如下方程中,x、y、n均为正整数。

对于n = 4,上述方程恰好有3个不同的解:

使得不同的解的数目超过1000的最小n值是多少?
注意:这个问题是第110题的简单版本;强烈推荐先解决这一题。
解题
这是一道很难的题目
先说暴力解法,普通暴力不行
1/x + 1/y = 1/n -> y = (n*x)/(x -n) 可以发现 只要 n*x能够整除(x-n)就是一个解了
能够找出x的范围就可以暴力解决
y = (n*x)/(x -n) 可以发现 x一定大于n,由于x y一定要是正数。
x=y=2n的时候上面等式也成了
假设x<=y 则y/x>=1 n/(x-n)>=1
2n>=x
x的取值范围就是n 到 2n
用javalong出现溢出的情况,Python好像时间很长,我用180180验证能够出来的
# coding=gbk def run():
for n in range(10,180181):
count = 0
for x in range(n+1,2*n+1):
if (n*x)%(x-n)==0:
count+=1
if count>1000:
print n if __name__ == '__main__':
run()
技巧性解题


截图于mathblog
问题就转化为求d(N) 下面就很简单了
1,如何求许多素数,筛选法求素数
2,其他根据上面的就可以直接求解了
学会分析问题很重要,根据已学到的知识,解决没有见过的问题
package Level4; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set; public class PE0108{
public static void run(){
ArrayList<Integer> prime = getPrime(20); int n=2;
while(true){ int count = getDiophantine(n,prime);
count = (count+1)/2; if(count> 500){
System.out.println(n+"\t"+count);
if(count>1000)
break;
}
n++;
}
}
// 174720 608
// 175560 851
// 176400 563
// 177840 608
// 179520 527
// 180180 1013
public static int getDiophantine(int n,ArrayList<Integer> prime){ int count = 1;
int i=0;
for(i=0;i<prime.size();i++){
int p = prime.get(i); int exp = 1;
while(n %p ==0){
exp+=2;
n=n/p;
}
count *=exp;
if(n==1)
return count;
}
return count;
} // 求 前num个素数
public static ArrayList<Integer> getPrime(int num){
ArrayList<Integer> prime=new ArrayList<Integer>();
prime.add(2);
// 除以已经判断是素数的数
boolean isPrime = true;
for(int i=3;prime.size()<num;i++){
isPrime = true;
for(int j=0;j<prime.size();j++){
if(i%prime.get(j) ==0){
isPrime = false;
break;
}
}
if(isPrime == true){
prime.add(i);
}
}
return prime;
}
public static void run1(){
int MAX = 1000;
int n=4;
// 内存溢出使得程序运行错误
System.out.println(getCount(180180));
while(true){
int count =getCount(n); if(count >=600){
System.out.println(n+"\t"+count);
if(count>MAX)
break;
}
n++;
}
}
public static int getCount(int n){
int count =0; for(int x=n+1;x<=2*n;x++){
long nx = n*x;
System.out.println(nx);
if( nx%(x-n)==0){
count++;
}
}
return count;
} public static void main(String[] args){
long t0 = System.currentTimeMillis();
run1();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms");
}
}
Java Code
Project Euler 108:Diophantine reciprocals I 丢番图倒数I的更多相关文章
- 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 ...
- bzoj 4459: [Jsoi2013]丢番图 -- 数学
4459: [Jsoi2013]丢番图 Time Limit: 10 Sec Memory Limit: 64 MB Description 丢番图是亚历山大时期埃及著名的数学家.他是最早研究整数系 ...
- 【bzoj4459】[Jsoi2013]丢番图 分解质因数
题目描述 丢番图是亚历山大时期埃及著名的数学家.他是最早研究整数系数不定方程的数学家之一.为了纪念他,这些方程一般被称作丢番图方程.最著名的丢番图方程之一是x^N+y^n=z^N.费马提出,对于N&g ...
- BZOJ_4459_[Jsoi2013]丢番图_数学+分解质因数
BZOJ_4459_[Jsoi2013]丢番图_数学+分解质因数 Description 丢番图是亚历山大时期埃及著名的数学家.他是最早研究整数系数不定方程的数学家之一. 为了纪念他,这些方程一般被称 ...
- bzoj4459[Jsoi2013]丢番图
bzoj4459[Jsoi2013]丢番图 题意: 丢番图方程:1/x+1/y=1/n(x,y,n∈N+) ,给定n,求出关于n的丢番图方程有多少组解.n≤10^14. 题解: 通分得yn+xn=xy ...
- Project Euler 66: Diophantine equation
题目链接 思路: 连分数求佩尔方程最小特解 参考博客 模板: LL a[]; bool min_pell(LL d, LL &x, LL &y) { LL m = floor(sqrt ...
- [luogu5253]丢番图【数学】
传送门 [传送门] 题目大意 求\(\frac{1}{x}+\frac{1}{y}=\frac{1}{n}\)有多少组不同的解. 分析 将式子转化成\((n-x)(n-y)=n^2\)的形式. 那么很 ...
- 【bzoj4459】JSOI2013丢番图
某JSOI夏令营出题人啊,naive! 你还是得学习个,搬这种原题不得被我一眼看穿? 求个n^2的约数除以二,向上取整. #include<bits/stdc++.h> using nam ...
- BZOJ 4459: [Jsoi2013]丢番图 数学推导
之前绝对做过几乎一模一样的题,现在做竟然忘了. code: #include <bits/stdc++.h> #define ll long long #define setIO(s) f ...
随机推荐
- ios开发--常用宏定义(部分转)
1.release时,屏蔽log #if defined (DEBUG) && DEBUG == 1 #else #define NSLog(...) {}; #endif #if d ...
- [原创]flexslider 中文文档 使用教程 参数手册
要改前人用的flexslider功能,但苦于找不到详细的文档教程,折磨了好久……(所以我才说不爱乱用插件) 为了福利下之后也苦于这个问题的人,我整理总结了下我找到的一些东西.可能没那么完善正确,欢迎在 ...
- sharepoint 2010 找不到搜索不到ad里的用户
前提条件: 1.这个用户是在ad中存在的. 2.这个用户也同步到了userprofile中. 问题现象: 在sharepoint的人员选择器中,搜索不到已经添加的用户. 可能原因: 1.有人说需要将 ...
- Ubuntu14.04安装配置ndnSIM
Ubuntu14.04安装配置ndnSIM 预环境 Ubuntu14.04官方系统 请先使用sudo apt-get update更新一下源列表 安装步骤 安装boost-lib sudo apt-g ...
- sql优化点整理
此文是我最早开始sql优化至今整理的小知识点和经常遇到的问题,弄懂这些对优化大型的sql会有不少帮助 ---------------------------------使用了多余的外连接------- ...
- linux中的文件类型
1.使用ls -l命令可以查看文件的类型和权限 [tansheng@localhost etc]$ ls -l ----------. root root 10月 : gshadow -------- ...
- PHP URL 重定向 的三种方法(转载)
为了方便查询,转载一篇. 1.使用header()函数 PHP的HTTP相关函数种提供了一个 header()函数,首先要清楚,header()函数必须放在php程序的开头部分,而且之前不能有另 ...
- Implementation Documentation[转]
原文地址:http://prasanna-adf.blogspot.tw/2009/04/implementation-documentation.html Following are the lis ...
- Github的使用以及Git的简单入门 - 课程作业三
GitHub创建项目 登录GitHub,在个人主页创建项目(repository) 创建后会生成2个文件,README.md和.gitignore.如图 创建本地仓库 如果是第一次使用git的话,需要 ...
- Python2.x与3.x版本区别
Python的3.0版本,常被称为Python 3000,或简称Py3k.相对于Python的早期版本,这是一个较大的升级. 为了不带入过多的累赘,Python 3.0在设计的时候没有考虑向下相容 ...