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还没有出来结果,跑到了下面的结果:

然而正确答案时候的碟子总数是:

  1.  
  1. 1070379110497
  1. Python
  1. # coding=gbk
  2.  
  3. import time as time
  4. import re
  5. import math
  6. def run():
  7. n = 10**12
  8. MAX = 10**16
  9. index = 0
  10. while n<MAX:
  11. x = solvex(n)
  12. if x!=0:
  13. print x
  14. break
  15. n+=1
  16. index+=1
  17. if index%1000000==0:
  18. print n
  19.  
  20. def solvex(n):
  21. dlt = delt(n)
  22. if judge(dlt):
  23. x = 1 + int(dlt**0.5)
  24. if x%2==0:
  25. return x/2
  26. return 0
  27.  
  28. def judge(dlt):
  29. sq = int(dlt**0.5)
  30. return sq**2 == dlt
  31.  
  32. def delt(n):
  33. dlt = 2*n*(n-1)
  34. return dlt
  35.  
  36. t0 = time.time()
  37. run()
  38. t1 = time.time()
  39. print "running time=",(t1-t0),"s"

这个博客多长提到,里面提到一个链接

设总的碟子数是x,蓝色碟子数是y

化简为:

第二个链接中给了求解方法

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

对这一题而言:

A=1

B=0
C=-2
D=-1

E=2

F=0

r=3 s =2 带入求解

  1. def run():
  2. MAX = 10**12
  3. x = 21
  4. y = 15
  5. while x<MAX:
  6. print x,y
  7. tmpx = 17*x + 24*y -20
  8. tmpy = 12*x + 17*y -14
  9. x = tmpx
  10. y = tmpy
  11.  
  12. print x, y

但是这里的输出结果只是部分答案,但是我们要的答案还在里面。

  1. 21 15
  2. 697 493
  3. 23661 16731
  4. 803761 568345
  5. 27304197 19306983
  6. 927538921 655869061
  7. 31509019101 22280241075
  8. 1070379110497 756872327473

上面第一个链接好像应该是这样来的:

r = 3 s =2计算PEQS

上面的r s 应该是r1 s1 这样反过来再去 r s 好像就和上面博客中的一样了。。。。。

  1. def run():
  2. MAX = 10**12
  3. x = 21
  4. y = 15
  5. while x<MAX:
  6. print x,y
  7. tmpy = 3*y+2*x-2
  8. tmpx = 4*y+3*x-3
  9. x = tmpx
  10. y = tmpy
  11. print x,y

输出结果是

  1. 21 15
  2. 120 85
  3. 697 493
  4. 4060 2871
  5. 23661 16731
  6. 137904 97513
  7. 803761 568345
  8. 4684660 3312555
  9. 27304197 19306983
  10. 159140520 112529341
  11. 927538921 655869061
  12. 5406093004 3822685023
  13. 31509019101 22280241075
  14. 183648021600 129858761425
  15. 1070379110497 756872327473

然而在这个中文博客中直接没有考虑常数项,xy初始值变成了1

JAVA

  1. package Level3;
  2.  
  3. public class PE0100{
  4. public static void run(){
  5. long MAX = 1000000;
  6. long x = 21;
  7. long y = 15;
  8. while(x/MAX<MAX){
  9. long tmpx = 4*y+3*x -3;
  10. long tmpy = 3*y+2*x -2;
  11. x = tmpx;
  12. y = tmpy;
  13. }
  14. System.out.println(y);
  15.  
  16. }
  17. public static void main(String[] args){
  18. long t0 = System.currentTimeMillis();
  19. run();
  20. long t1 = System.currentTimeMillis();
  21. long t = t1 - t0;
  22. System.out.println("running time="+t/1000+"s"+t%1000+"ms");
  23.  
  24. }
  25. }

Project Euler 100 : Arranged probability 安排概率的更多相关文章

  1. Project Euler 613 Pythagorean Ant(概率+积分)

    题目链接:点击我打开题目链接 题目大意: 给你一只蚂蚁,它在一个 边长为 \(30-40-50\) 的直角三角形\((x,y)\)上,并且它在直角三角形中选择的位置和移动方向的概率都是相等的.问你这只 ...

  2. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  3. Python练习题 047:Project Euler 020:阶乘结果各数字之和

    本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...

  4. Python练习题 045:Project Euler 017:数字英文表达的字符数累加

    本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...

  5. Python练习题 041:Project Euler 013:求和、取前10位数值

    本题来自 Project Euler 第13题:https://projecteuler.net/problem=13 # Project Euler: Problem 13: Large sum # ...

  6. Python练习题 032:Project Euler 004:最大的回文积

    本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...

  7. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  8. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  9. 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 ...

随机推荐

  1. c#多层嵌套Json

    Newtonsoft.Json.Net20.dll 下载请访问http://files.cnblogs.com/hualei/Newtonsoft.Json.Net20.rar 在.net 2.0中提 ...

  2. linux下配置Apache基于加密的认证访问

    1.首先要确认安装了 mod_ssl模块 我的机器是centos是系统,执行下面命令 yum install -y mod_ssl 2.用openssl工具生成密钥,证书请求文件,证书 在/usr/l ...

  3. 正则PerlRegEx实现的批量替换指定文件中的标签

    示例: 一个朋友需要而编写的标签升级更新. 速度超快,1w个文件大概4,5秒,本想加个多线程显示进度,后来想想算了 主要代码: reg.RegEx := '<' + Edit_regular1. ...

  4. 一道关于比赛胜负的Sql查询题目

    以前做过一道题目,一直没有来得及总结下来.贴图: 记得以前曾经找到了两种方法,今天试了一下,还是可以的,贴出过程: 下面是具体的查询方法: 原来放的是图片,今天又练习了一下,附代码: create T ...

  5. C#不同页面之间通信的方法

    以前做项目的时候经常头疼两个页面之间的交互(汗),这几天看的MVVM项目,忽然感觉好简单的!我自己写了个简单的demo 可以简单实现2个页面之间的交互,新人第一次发博客,不喜勿喷 代码很简单,注释我就 ...

  6. 拥抱ARM妹纸第二季 之 第一次 点亮太阳

    上次做鱼缸LED灯时还有很多材料正好拿来用.穆等等哥- 俺去找材料. 材料列表     3W LED   x  3     散热片     x  1     恒流IC     x  1     其他零 ...

  7. 虚拟机Linux下找不到/dev/cdrom

    问题描述: 笔者欲更新一下VMwareTools,结果发现虚拟机Linux上找不到设备"/dev/cdrom",当然也就不能通过命令"mount /dev/cdrom / ...

  8. sysfs->sys简单介绍

    Sys节点 1:sysfs 是 Linux 内核中设计较新的一种虚拟的基于内存的文件系统, sysfs 的挂载点 /sys 目录结构. 2:/sys 文件系统下的目录结构 /sys 下的目录结构是经过 ...

  9. String面试题

    //a b c 分别是怎么存储的, a和b a和c分别有什么区别// c和d的区别是什么 String a= "hello";String b= "hello" ...

  10. per-project basis

    Of course, HSQLDB connection parameters should be stored on a per-project basis, instead of only onc ...