- 注册时间
- 2008-2-6
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 51573
- 在线时间
- 小时
|
发表于 2013-3-19 19:29:27
|
显示全部楼层
优化了一下
数字较大时候,缩短的时间很可观-
-
- #include <iostream>
-
- using namespace std;
- #define MAX 99
-
- struct point3
- {
- int x, y, z, dp;
- };
-
- point3 point[(MAX+1)*(MAX+1)*(MAX+1)];
-
- int dp(int n,int x, int y,int z)
- {
- return (x + (n+1)*y + (n+1)*(n+1)*z);
- }
-
- void init(int n)
- {
- int x, y, z;
- for (z=0;z<=n;z++)
- for (y=0;y<=n; y++)
- for (x=0;x<=n; x++)
- {
- int t= x+(n+1)*y+(n+1)*(n+1)*z;
- point[t].x = x;
- point[t].y = y;
- point[t].z = z;
- point[t].dp = dp(n, x, y, z);
- }
- }
-
- int orth(point3 p0, point3 p1,point3 p2)
- {
- return ((p1.x-p0.x)*(p2.x-p0.x) + (p1.y-p0.y)*(p2.y-p0.y) + (p1.z-p0.z)*(p2.z-p0.z))==0;
- }
-
- void addp2(point3& r, point3 p0, point3 p1, point3 p2)
- {
- r.x=p1.x+p2.x-p0.x;
- r.y=p1.y+p2.y-p0.y;
- r.z=p1.z+p2.z-p0.z;
- }
-
- void addp3(point3& r, point3 p0, point3 p1, point3 p2, point3 p3)
- {
- r.x=p1.x+p2.x+p3.x-2*p0.x;
- r.y=p1.y+p2.y+p3.y-2*p0.y;
- r.z=p1.z+p2.z+p3.z-2*p0.z;
- }
-
- int inside(int n, point3 p)
- {
- return (p.x<=n)&&(p.x>=0)&&(p.y<=n)&&(p.y>=0)&&(p.z<=n)&&(p.z>=0);
- }
-
- long long calc(int n)
- {
- if (n < 1) return 0;
- if (n == 1) return 1;
- point3 t;
- long long c=0;
- init(n);
- int max=(n+1)*(n+1)*(n+1)-1;
- for (int i=0; i<=max-(n+1)*(n+1)-3; i ++)
- for (int j=i+1; j<=max-(n+1)*(n+1)-2; j ++)
- for (int k=j+1; k<=max-(n+1)*(n+1)-1; k ++)
- if (orth(point[i], point[j], point[k]))
- for (int m=k+1; m<=max; m ++)
- {
- if (orth(point[i], point[j], point[m]))
- if (orth(point[i], point[m], point[k]))
- {
- addp2(t, point[i], point[j], point[k]);
- if (inside(n, t))
- {
- addp2(t, point[i], point[m], point[k]);
- if (inside(n, t))
- {
- addp2(t, point[i], point[j], point[m]);
- if (inside(n, t))
- {
- addp3(t, point[i], point[j], point[k], point[m]);
- if (inside(n, t)) c++;
- }
- }
- }
- }
- }
- return c;
- }
-
- int main( void )
- {
- int n;
- cout << "input: ";
- cin >> n;
- cout << endl;
- if (n > MAX) cout << "input is to BIG" << endl;
- cout << "total: " << calc(n);
- return 0;
- }
复制代码 |
|