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.

高中做过的题目。把立方体各面展开,这个路径实际上是一个直角三角形的斜边。

要使得的这个路径最小。如果矩阵个边长分别为a<=b <=c

最短路径为sqrt((a+b)^2+c^2)

把a,b视为总体,记做ab

则ab范围是[2,2M]

在寻找到开方后结果为整数的ab和c后

假设ab<c:a,b是能够平均分ab的

假设ab>=c:b的取值到大于ab/2而且满足b<=c,ab-b<=c 得到b的取值个数为(c-(ab+1)/2)+1

#include <iostream>
#include <string>
#include <cmath>
using namespace std; int main()
{
int c = 1;
int count = 0;
while (count < 1000000)
{
c++;
for (int ab = 2; ab <= 2 * c; ab++)
{
int path=ab*ab + c*c;
int tmp = int(sqrt(path));
if (tmp*tmp == path)
{
count += (ab >= c) ? 1+(c-(ab+1)/2) : ab / 2;
}
}
}
cout << c << endl;
system("pause");
return 0;
}

Project Euler:Problem 86 Cuboid route的更多相关文章

  1. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  2. Project Euler:Problem 63 Powerful digit counts

    The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...

  3. Project Euler:Problem 76 Counting summations

    It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...

  4. Project Euler:Problem 87 Prime power triples

    The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...

  5. Project Euler:Problem 89 Roman numerals

    For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...

  6. Project Euler:Problem 93 Arithmetic expressions

    By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four ari ...

  7. Project Euler:Problem 39 Integer right triangles

    If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exact ...

  8. Project Euler:Problem 28 Number spiral diagonals

    Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...

  9. Project Euler:Problem 47 Distinct primes factors

    The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The ...

随机推荐

  1. JS中数组和对象的区别

  2. 关于Bootstrap的悬浮窗口(popover)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. Angular进阶教程三

    7 总结 angular上手比较难,初学者(特别是习惯了使用JQuery的人)可能不太适应其语法以及思想.随着对ng探索的一步步深入,也确实感觉到了这一点,尤其是框架内部的某些执行机制. 7.1页面效 ...

  4. Spring mvc与Struts2的比较

    1. 机制:spring mvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同. 2. 性能:spring会稍微比struts快.spring mvc是基于方法的设 ...

  5. linux shell实现守护进程 看门狗 脚本

    嵌入式初学者,第一次上传代码.昨天做了一个udhcpd与udhcpc的守护,目前只会用shell模仿编写,还有什么方法可以做守护呢? ? 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...

  6. 关于eclipse连接外置android模拟器

    1.win+R,输入cmd,打开命令提示符 2.cd D:\Java(安卓应用开发)\adt-bundle-windows-64\sdk\platform-toolsadb connect 127.0 ...

  7. linux 服务器 keras 深度学习环境搭建

    感慨: 程序跑不起来,都是环境问题. 1. 安装Anaconda https://blog.csdn.net/gdkyxy2013/article/details/79463859 2. 在 Anac ...

  8. Java基础之基本数据类型的包装类型

    Java的基本数据类型的包装数据类型有多种: int Integer,short Short,boolean Boolean,float Float,double Double等等. Java包装类数 ...

  9. spring+quarts常见问题

    javax/transaction/UserTransactionCaused by: java.lang.NoClassDefFoundError: javax/transaction/UserTr ...

  10. HTML后续

    列表标签 <ul>.<ol>.<dl> 列表标签 无序列表 属性: type="属性值".属性值可以选: disc(实心原点,默认),squar ...