Project Euler 126 - Cuboid layers

这题先是推公式…

狂用不完全归纳+二次回归,最后推出这么一个奇怪的公式

\[f(t,x,y,z)=4(t-1)(x+y+z+t-2)+2(xy+yz+xz)\]

表示长宽高为\(x\)、\(y\)、\(z\)的立方体第\(t\)层放的立方体的个数。

接下来就是算答案了…

方法很简单:暴力

但是暴力还是有技巧的,开始我是直接从1到1000枚举\(t\)、\(x\)、\(y\)、\(z\),但这样出不来结果。

换成下面代码里的方法就行了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn=200000;
 7 int a[210000];
 8 inline int f(int t,int x,int y,int z)
 9 {
10     return 4*(t-1)*(x+y+z+t-2)+2*(x*y+y*z+x*z);
11 }
12 int main(int argc, char *argv[])
13 {
14     freopen("1.out","w",stdout);
15     for(int x=1;f(1,x,x,x)<=maxn;x++)
16         for(int y=x;f(1,x,y,y)<=maxn;y++)
17             for(int z=y;f(1,x,y,z)<=maxn;z++)
18                 for(int t=1;f(t,x,y,z)<=maxn;t++)
19                 a[f(t,x,y,z)]++;
20     for(int i=1;i<=200000;i++)
21         printf("%d %d\n",i,a[i]);
22     return 0;
23 }

 

posted @ 2014-04-29 10:54  zhuohan123  阅读(455)  评论(0)    收藏  举报