Cuboid route

A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the opposite corner. By travelling on the surfaces of the room the shortest “straight line” distance from S to F is 10 and the path is shown on the diagram.

However, there are up to three “shortest” path candidates for any given cuboid and the shortest route doesn’t always have integer length.

It can be shown that there are exactly 2060 distinct cuboids, ignoring rotations, with integer dimensions, up to a maximum size of M by M by M, for which the shortest route has integer length when M = 100. This is the least value of M for which the number of solutions first exceeds two thousand; the number of solutions when M = 99 is 1975.

Find the least value of M such that the number of solutions first exceeds one million.


长方体路径

蜘蛛S位于一个6乘5乘3大小的长方体屋子的一角,而苍蝇F则恰好位于其对角。沿着屋子的表面,从S到F的最短“直线”距离是10,路径如下图所示:

但是,每个立方体都有三条可能的最短路径,而且最终的最短路径并不一定是整数。

考虑所有整数边长的立方体屋子,最大不超过M×M×M,当M=100时一共有2060个立方体的最短路径是整数,而且这也是解超过2000的最小的M;M=99时又1975个立方体的最短路径是整数。

找出解超过一百万的最小的M。

解题

可以直接求的

为了防止在计算的过程中,出现立方体重复的显现,可以假设 a<=b <=c

最短路径有三种:

path1 = (a+b)^2 + c^2

path2 = (a+c)^2 + b^2

path3 = (c+b)^2 + a^2

上面三个值展开后可以发现都含有a b c的平方项,不同项以此是:2ab 2 ac 2bc

显然的发现2ab是最小值,也就是说path1就是最小路径值,判断是不是整数就很简单了。

Java关键程序

    static void run(){
int limit = 1000000;
int count =0;
int M = 1;
for(M = 1;;M++){
// 当 a<= b <= c 最小路径就是 (a+b)*(a+b) + c*c 开根号
for(int a = 1;a<= M ;a++){
for(int b =a ;b<= M;b++){
int c = M ;
int path = (a+b)*(a+b) + c*c;
int tmp = (int)Math.sqrt(path);
if(tmp*tmp == path){
count ++;
}
}
}
if(count> limit){
System.out.println(M);
break;
}
}
}

另外一种方法,参考链接

同样假设:a<=b<=c

最小值是:(a+b)^2 + c^2

可以把 a+b看成一个值ab

显然ab的范围就是[2,2M]

后面就看不懂了。

上面两种放大都是固定c的值,c也是最大值,找出对应c满足条件的 立方体数量,c+1的时候显然是包括c的情况的解。

package Level3;

public class PE086{
static void run(){
int limit = 1000000;
int count =0;
int M = 1;
for(M = 1;;M++){
// 当 a<= b <= c 最小路径就是 (a+b)*(a+b) + c*c 开根号
for(int a = 1;a<= M ;a++){
for(int b =a ;b<= M;b++){
int c = M ;
int path = (a+b)*(a+b) + c*c;
int tmp = (int)Math.sqrt(path);
if(tmp*tmp == path){
count ++;
}
}
}
if(count> limit){
System.out.println(M);
break;
}
}
}
static void run2() {
int limit = 1000000; int c = 1;
int count = 0;
while(count < limit){
c++;
for(int ab = 2;ab<= 2*c;ab++){
int path = ab*ab + c*c;
int tmp = (int)Math.sqrt(path);
if(tmp*tmp== path){
count += (ab>=c)?1+(c-(ab+1)/2):ab/2;
}
}
// if(c ==100)
// System.out.println(count);
}
System.out.println(c);
} public static void main(String[] args){
long t0 = System.currentTimeMillis();
run2();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms"); }
}

1818
running time=0s39ms

 

Python 时间有点长

# coding=gbk
import time as time t0 = time.time()
print 3**2
print int(8**0.5)
def run():
limit = 1000000
count = 0
M = 1
while count < limit:
for a in range(1,M+1):
for b in range(a,M+1):
c = M
path = (a+b)**2 + c**2
tmp = int((path)**0.5)
if tmp**2 == path:
count +=1
M += 1 print M-1 # 1818
# running time= 1062.27400017 s run()
t1 = time.time()
print "running time=",(t1-t0),"s"

Project Euler 86:Cuboid route 长方体路径的更多相关文章

  1. Project Euler:Problem 86 Cuboid route

    A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o ...

  2. Project Euler 126 - Cuboid layers

    这题先是推公式… 狂用不完全归纳+二次回归,最后推出这么一个奇怪的公式 \[f(t,x,y,z)=4(t-1)(x+y+z+t-2)+2(xy+yz+xz)\] 表示长宽高为\(x\).\(y\).\ ...

  3. Python练习题 043:Project Euler 015:方格路径

    本题来自 Project Euler 第15题:https://projecteuler.net/problem=15 ''' Project Euler: Problem 15: Lattice p ...

  4. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  5. [project euler] program 4

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

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

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

  7. Project Euler 9

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

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

  9. project euler 169

    project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...

随机推荐

  1. 使用 PHP 验证表单数据

    使用 PHP 验证表单数据 首先我们对用户所有提交的数据都通过 PHP 的 htmlspecialchars() 函数处理. 当我们使用 htmlspecialchars() 函数时,在用户尝试提交以 ...

  2. 浅析Mysql 数据回滚错误的解决方法

    介绍一下关于Mysql数据回滚错误的解决方法.需要的朋友可以过来参考下 MYSQL的事务处理主要有两种方法.1.用begin,rollback,commit来实现begin 开始一个事务rollbac ...

  3. PHP流程控制语句下

    ok,继续搞. while循环: while(expr){ 程序块: } 条件expr成立执行程序块,否则结束. do while循环:do while循环比while循环要多循环一次,它要先执行程序 ...

  4. .NET开发Windows Service程序 - Topshelf

    在实际项目开发过程中,会经常写一些类似定时检查,应用监控的应用.这类应用在windows平台通常都会写成window service程序. 在百度上搜索一下'c#开发windows service', ...

  5. 【转】Microsoft® SQL Server® 2012 Performance Dashboard Reports

    http://www.cnblogs.com/shanyou/archive/2013/02/12/2910232.html SQL Server Performance Dashboard Repo ...

  6. linux驱动路径

    1. 按键驱动 \drivers\input\keyboard\utu2440_buttons.c 2. LED驱动 \drivers\char\utu2440-led.c 3. DM9000网卡驱动 ...

  7. redis 界面软件使用

    ubuntu 下下载安装包 sudo dpkg -i redis-desktop-manager_0.8.3-120_amd64.deb//安装 redis-desktop-manager //启动

  8. XCODE快捷键和功能汇总篇(不断更新)

    快捷键 command+b(build) 编译 command+r(run) 运行编译后程序鼠标放在代码元素上,按command然后单击,可以看到元素的属性

  9. quartus ii 中文注释乱码解决办法

    转载自:http://bbs.ednchina.com/BLOG_ARTICLE_3027549.HTM 有些时候我们用Quartus ii 打开不同版本创建的工程文件时,往往会出现下列提示 点yes ...

  10. 实现iOS长时间后台的两种方法:Audiosession和VOIP(转)

    分类: Iphone2013-01-24 14:03 986人阅读 评论(0) 收藏 举报 我们知道iOS开启后台任务后可以获得最多600秒的执行时间,而一些需要在后台下载或者与服务器保持连接的App ...