Project Euler 100 : Arranged probability 安排概率
If a box contains twenty-one coloured discs, composed of fifteen blue discs and six red discs, and two discs were taken at random, it can be seen that the probability of taking two blue discs, P(BB) = (15/21)×(14/20) = 1/2.
The next such arrangement, for which there is exactly 50% chance of taking two blue discs at random, is a box containing eighty-five blue discs and thirty-five red discs.
By finding the first arrangement to contain over 1012 = 1,000,000,000,000 discs in total, determine the number of blue discs that the box would contain.
在一个盒子中装有21个彩色碟子,其中15个是蓝的,6个是红的。如果随机地从盒子中取出两个碟子,取出两个蓝色碟子的概率是P(BB) = (15/21)×(14/20) = 1/2。
下一组使得取出两个蓝色盘子的概率恰好为50%的安排,是在盒子中装有85个蓝色碟子和35个红色碟子。
当盒子中装有超过1012 = 1,000,000,000,000个碟子时,找出第一组满足上述要求的安排,并求此时盒子中蓝色碟子的数量。
解题
表示暴力破解不可以。
昨天晚上10点开始跑,到今天15.30还没有出来结果,跑到了下面的结果:

然而正确答案时候的碟子总数是:
1070379110497
Python
# coding=gbk import time as time
import re
import math
def run():
n = 10**12
MAX = 10**16
index = 0
while n<MAX:
x = solvex(n)
if x!=0:
print x
break
n+=1
index+=1
if index%1000000==0:
print n def solvex(n):
dlt = delt(n)
if judge(dlt):
x = 1 + int(dlt**0.5)
if x%2==0:
return x/2
return 0 def judge(dlt):
sq = int(dlt**0.5)
return sq**2 == dlt def delt(n):
dlt = 2*n*(n-1)
return dlt t0 = time.time()
run()
t1 = time.time()
print "running time=",(t1-t0),"s"
设总的碟子数是x,蓝色碟子数是y

化简为:
第二个链接中给了求解方法

对于一般的二次方程的解是:

对这一题而言:
A=1
B=0
C=-2
D=-1
E=2
F=0
r=3 s =2 带入求解
def run():
MAX = 10**12
x = 21
y = 15
while x<MAX:
print x,y
tmpx = 17*x + 24*y -20
tmpy = 12*x + 17*y -14
x = tmpx
y = tmpy print x, y
但是这里的输出结果只是部分答案,但是我们要的答案还在里面。
21 15
697 493
23661 16731
803761 568345
27304197 19306983
927538921 655869061
31509019101 22280241075
1070379110497 756872327473
上面第一个链接好像应该是这样来的:

r = 3 s =2计算PEQS

上面的r s 应该是r1 s1 这样反过来再去 r s 好像就和上面博客中的一样了。。。。。
def run():
MAX = 10**12
x = 21
y = 15
while x<MAX:
print x,y
tmpy = 3*y+2*x-2
tmpx = 4*y+3*x-3
x = tmpx
y = tmpy
print x,y
输出结果是
21 15
120 85
697 493
4060 2871
23661 16731
137904 97513
803761 568345
4684660 3312555
27304197 19306983
159140520 112529341
927538921 655869061
5406093004 3822685023
31509019101 22280241075
183648021600 129858761425
1070379110497 756872327473
然而在这个中文博客中直接没有考虑常数项,xy初始值变成了1
JAVA
package Level3;
public class PE0100{
public static void run(){
long MAX = 1000000;
long x = 21;
long y = 15;
while(x/MAX<MAX){
long tmpx = 4*y+3*x -3;
long tmpy = 3*y+2*x -2;
x = tmpx;
y = tmpy;
}
System.out.println(y);
}
public static void main(String[] args){
long t0 = System.currentTimeMillis();
run();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms");
}
}
Project Euler 100 : Arranged probability 安排概率的更多相关文章
- Project Euler 613 Pythagorean Ant(概率+积分)
题目链接:点击我打开题目链接 题目大意: 给你一只蚂蚁,它在一个 边长为 \(30-40-50\) 的直角三角形\((x,y)\)上,并且它在直角三角形中选择的位置和移动方向的概率都是相等的.问你这只 ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 047:Project Euler 020:阶乘结果各数字之和
本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...
- Python练习题 045:Project Euler 017:数字英文表达的字符数累加
本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...
- Python练习题 041:Project Euler 013:求和、取前10位数值
本题来自 Project Euler 第13题:https://projecteuler.net/problem=13 # Project Euler: Problem 13: Large sum # ...
- Python练习题 032:Project Euler 004:最大的回文积
本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- 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 ...
随机推荐
- Android 官网提供的Custom-view 编译出错--error: No resource identifier found for attribute
error: No resource identifier found for attribute in custom-views from http://developer.android.com ...
- linux 定时执行任务 crontab
欲编写定时任务访问网页和打开图片 原来写法为 #!/bin/bash #可以执行 echoecho "Hello" > dir/file.txt #不可以执行 xdg-ope ...
- sql中更新数据库用到declare @a in
declare @a in update TB_Class set @a=1,name='李小龙' where ID=1 这样就可以像更新哪个就更新哪个了 例如ibatisnet中需要更新的时候: & ...
- webBrowser执行js的方法,并返回值,c#后台取值
private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate(Application.StartupPath + ...
- Mongodb Java Driver 参数配置解析
要正确使用Mongodb Java Driver,MongoClientOptions参数配置对数据库访问的并发性能影响极大. connectionsPerHost:与目标数据库能够建立的最大conn ...
- linux查看硬件信息的命令(图文)
发布:脚本学堂/Linux命令 编辑:JB02 2013-12-23 21:48:18 [大 中 小] 转自:http://www.jbxue.com/LINUXjishu/14996.htm ...
- 修改 apache http server 默认站点目录
1.打开apache中的 httpd.conf 文件,将DocumentRoot "D:/Program Files/Apache Software Foundation/Apache2.2 ...
- CLR via C# 混合线程同步构造
1. 自旋,线程所有权和递归 2. 混合构造 a.ManualResetEventSlim b.SemaphoreSlim c.Monitor d.ReaderWriterLockSlim 3.条件变 ...
- JAVA的字节码技术
1.什么是字节码? 字节码 byteCode JVM能够解释执行的.java程序的归宿,但是从规范上来讲和Java已没有任何关系了.一些动态语言也可以编译成字节码在JVM上运行.字节码就相当于JVM上 ...
- VS2013中设置大小写的快捷键
1.我们在定义头文件时,通常需要定义: #ifndef _MainMenu_H_#define _MainMenu_H_ your code... #endif 我们需要将头文件名设置为大写的: ...