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 = 1000000count = 0M = 1while count < limit:for a in range(1,M+1):for b in range(a,M+1):c = M path = (a+b)**2 + c**2tmp = int((path)**0.5)if tmp**2 == path:count +=1M += 1print M-1 # 1818
# running time= 1062.27400017 s
run()
t1 = time.time()
print "running time=",(t1-t0),"s"

转载于:https://www.cnblogs.com/theskulls/p/5001306.html

Project Euler 86:Cuboid route 长方体路径相关推荐

  1. Project Euler 126 - Cuboid layers

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

  2. 欧拉计划(project euler)最详细中文题解

    欧拉计划是一个在线解题网站,题目以各类数学问题为主,通常需要结合一定的数学与编程知识,写出适当的程序求解问题(详细介绍可以参见我的文章).相比于力扣等刷题网站,欧拉计划上的题目有着更丰富的知识背景,在 ...

  3. Project Euler.59.yyt

    Project Euler.59.yyt XOR 题目链接:http://pe-cn.github.io/59/ 解题思路: 题目中提到秘钥是由三个小写字母组成,因此可以选取一定长度的密文进行爆破. ...

  4. 硬币游戏 Project Euler 232

    原帖:http://hi.baidu.com/atyuwen/blog/item/160bd024531e3034c995591d.html Project Euler上最近的题目都还比较意思,来看看 ...

  5. [Project Euler] 来做欧拉项目练习题吧: 题目004

        [Project Euler] 来做欧拉项目练习题吧: 题目004 周银辉 问题描述: A palindromic number reads the same both ways. The l ...

  6. [Project Euler] 来做欧拉项目练习题吧: 题目017

    [Project Euler] 来做欧拉项目练习题吧: 题目017 周银辉 题目描述: If the numbers 1 to 5 are written out in words: one, two ...

  7. [Project Euler] 来做欧拉项目练习题吧: 题目012

      [Project Euler] 来做欧拉项目练习题吧: 题目012 周银辉 问题描述: The sequence of triangle numbers is generated by addin ...

  8. Project Euler

    最近发现了一个很有趣的网站,Project Euler 上面全是数学题,不过大多需要用编程解决 Problem 3: 求:600851475143的最大素因子. 解:编了个程序,迅速水过,看官方的题解 ...

  9. Project Euler Problem 27小结

    Project Euler上有很多有意思的问题,刚做到第27题,对这个问题做个小结. Problem 27: Euler有一个著名的方程n^2+n+41,当n=0到39时,方程结果均为质数.如今人们用 ...

最新文章

  1. PHP 作为SocketClient发送字节数组
  2. PC远程控制 NetSupport Manager
  3. ECShop 模板库项目功能详解
  4. Java中异常的分类
  5. mysql在线模拟器_SQL在线模拟器
  6. 下一代微服务(service Mesh)
  7. java数组比较的头文件_设给定一组整型数组,求它们的平均值及最大值 Java程序设计...
  8. 绝对好文:嵌入式系统的软件架构设计!
  9. 总结CSS3新特性(媒体查询篇)
  10. 怎么钢枪_和平精英有战术钢枪和无脑冲有何区别?你们怎么看待这个问题
  11. vmix安装无法连接远程服务器_Windows server 2012 云服务器建站教程 (1):远程连接桌面+IIS服务器安装...
  12. CMake 学习笔记 02 - 更复杂的项目
  13. 官方Caffe—Microsoft编译安装
  14. 试图在loongarch64上编译JNA失败
  15. 高等数学张宇18讲 第一讲 高等数学常用基础知识
  16. 6阶子群同构于s3或者z6_顾沛《抽象代数》1.6变换群与置换群习题解答
  17. mysql退出安全模式_MySQL数据库之mysql 解除安全模式
  18. VB程序设计教程(第四版)龚沛曾 实验8-2
  19. android多级列表
  20. 《人间告白.金鱼酱》摘录和读后感

热门文章

  1. 微信登录界面安卓代码_【雷电说明书】安卓模拟器微信登录闪退,转圈,停止运行解决方法...
  2. Python循环练习-货币兑换
  3. UltraISO怎么用之使用软碟通UltraISO如何烧录windows 2012r2和Centos系统到U盘
  4. Go const和iota 使用实战
  5. U3D_关于UI中的锚点
  6. 关于IE8无法上传附件的解决办法
  7. oracle 表的er图,用PowerDesingner把oracle中的表导成er图
  8. Java从指定URL下载文件并保存到指定目录
  9. 馒头,不!月饼引起的惨案
  10. win7 64位 java_1.win7 64位 java安装