Diophantine reciprocals 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.


丢番图倒数I

在如下方程中,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的更多相关文章

  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. bzoj 4459: [Jsoi2013]丢番图 -- 数学

    4459: [Jsoi2013]丢番图 Time Limit: 10 Sec  Memory Limit: 64 MB Description 丢番图是亚历山大时期埃及著名的数学家.他是最早研究整数系 ...

  3. 【bzoj4459】[Jsoi2013]丢番图 分解质因数

    题目描述 丢番图是亚历山大时期埃及著名的数学家.他是最早研究整数系数不定方程的数学家之一.为了纪念他,这些方程一般被称作丢番图方程.最著名的丢番图方程之一是x^N+y^n=z^N.费马提出,对于N&g ...

  4. BZOJ_4459_[Jsoi2013]丢番图_数学+分解质因数

    BZOJ_4459_[Jsoi2013]丢番图_数学+分解质因数 Description 丢番图是亚历山大时期埃及著名的数学家.他是最早研究整数系数不定方程的数学家之一. 为了纪念他,这些方程一般被称 ...

  5. bzoj4459[Jsoi2013]丢番图

    bzoj4459[Jsoi2013]丢番图 题意: 丢番图方程:1/x+1/y=1/n(x,y,n∈N+) ,给定n,求出关于n的丢番图方程有多少组解.n≤10^14. 题解: 通分得yn+xn=xy ...

  6. Project Euler 66: Diophantine equation

    题目链接 思路: 连分数求佩尔方程最小特解 参考博客 模板: LL a[]; bool min_pell(LL d, LL &x, LL &y) { LL m = floor(sqrt ...

  7. [luogu5253]丢番图【数学】

    传送门 [传送门] 题目大意 求\(\frac{1}{x}+\frac{1}{y}=\frac{1}{n}\)有多少组不同的解. 分析 将式子转化成\((n-x)(n-y)=n^2\)的形式. 那么很 ...

  8. 【bzoj4459】JSOI2013丢番图

    某JSOI夏令营出题人啊,naive! 你还是得学习个,搬这种原题不得被我一眼看穿? 求个n^2的约数除以二,向上取整. #include<bits/stdc++.h> using nam ...

  9. BZOJ 4459: [Jsoi2013]丢番图 数学推导

    之前绝对做过几乎一模一样的题,现在做竟然忘了. code: #include <bits/stdc++.h> #define ll long long #define setIO(s) f ...

随机推荐

  1. ExtJS FormPanel不执行校验

    经检查问题原因在于使用了 validator 属性. 使用validator属性,必须添加返回值.不添加返回值,就会出现FormPanel不执行校验的问题.

  2. 关于Objective-C格式化处理相关规范

    Objective-C格式字符串和C#有很大的差别,下面我们就来看看 在C#中我们可以这么做,简单例举几个: //格式化输出字符串 string word = "world"; s ...

  3. SQL SERVER定时任务执行跟踪--供远程查看 [原创]

    一.背景 每次查需要优化的SQL都需要上外网,通过Profiler,报表或者DMV执行特定sql来查找,来回跑很麻烦,能不能在本地直接监控外网的好性能的SQL呢?方法是有的,我们可以通过把Profil ...

  4. Liferay JSP中常用的标签

    (本文转载自http://www.cnblogs.com/edwardlauxh/archive/2010/03/26/1918614.html) 在Liferay框架中拥有它自身的标签,虽然Port ...

  5. 浅谈GitHub

    Git 是一个面向开源及私有软件项目的托管平台,因为只支持Git作为唯一的版本库格式进行托管,故名GitHub. Gith是一个基于 git 的社会化代码分享社区,所谓 social coding.你 ...

  6. IT小小鸟读后感

    我是一只小小鸟(读后感) 当我成为闽江学院的一名学子时,我选择了我自己的专业(软件工程).成为大一新生的我对着未来充满着希望憧憬.但现实并不是美好的我得替今后的我想想以后大学毕业了到底我到了社会上要用 ...

  7. redis.conf配置

    # Redis configuration file example # Redis示例配置文件 # Note on units: when memory size is needed, it is ...

  8. netty 粘包问题处理

    netty 粘包问题处理 key words: netty 粘包 解包 半包 TCP 一般TCP粘包/拆包解决办法 定长消息,例如每个报文长度固定,不够补空格 使用回车换行符分割,在包尾加上分割符,例 ...

  9. Visual Studio快捷键设置

    1.查看当前快捷键:环境-键盘-按快捷键2.文本编辑器-C#-显示-行号3.文本编辑器-C#-制表符-插入空格4.文本编辑器-所有语言-没有选定内容时对空行应用剪切或复制命令5.Ctrl+Shift- ...

  10. shadowmap 及优化

    对于子阴影的走样, 条纹 开zbias resterizeState zbias = 1000...大概这样 另一个方法是画背面 backface是指一个人肚子那面,后背那面 而不是肚子的里面那层 所 ...