题目:求出边长为n的 2,3,4维的方体中有多少个方体和矩形体。
分析:数学题。观察可知,k维的方体中的方体个数为:sk = 1^k + 2^k + ... + n^k(枚举边长)
至于矩形体枚举每条边在[0,n]上的起始和终点,则边长有n(n+1)/2种,
则矩形体的数量为:rk = (1*2/2)^k + (2*3/2)^k + ... + (n(n+1)/2)^k - sk (去掉正方体)
说明:使用long long防止溢出。
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int main()
{
long long n,s2,s3,s4,r2,r3,r4,e;
while ( cin >> n ) {
s2 = s3 = s4 = r2 = r3 = r4 = 0LL;
for ( int i = 1 ; i <= n ; ++ i ) {
s2 += i*i;
s3 += i*i*i;
s4 += i*i*i*i;
}
e = n*(n+1)/2;
r2 = e*e-s2;
r3 = e*e*e-s3;
r4 = e*e*e*e-s4;
printf("%lld %lld %lld %lld %lld %lld\n",s2,r2,s3,r3,s4,r4);
}
return 0;
}