uva 10177(数学)

来源:互联网 发布:win10系统网络感叹号 编辑:程序博客网 时间:2024/06/11 15:19

题目:

You can see a (4x4) grid below. Can you tell me how many squares and rectangles are hidden there? You can assume that squares are not rectangles. Perhaps one can count it by hand but can you count it for a (100x100) grid or a (10000x10000) grid. Can you do it for higher dimensions? That is can you count how many cubes or boxes of different size are there in a (10x10x10) sized cube or how many hyper-cubes or hyper-boxes of different size are there in a four-dimensional (5x5x5x5) sized hypercube. Remember that your program needs to be very efficient. You can assume that squares are not rectangles, cubes are not boxes and hyper-cubes are not hyper-boxes. 

 

Fig: A 4x4 Grid

Fig: A 4x4x4 Cube


Input

The input contains one integer N (0<=N<=100) in each line, which is the length of one side of the grid or cube or hypercube. As for the example above the value ofN is 4. There may be as many as 100 lines of input.

output

For each line of input, output six integers S2, R2, S3, R3, S4, R4 in a single line where S2 means no of squares of different size in( NxN) two-dimensional grid, R2 means no of rectangles of different size in(NxN) two-dimensional grid. S3, R3, S4, R4 means similar cases in higher dimensions as described before.   

sample input

1
2
3

sample output

1 0 1 0 1 0
5 4 9 18 17 64
14 22 36 180 98 1198

题解:二维边长为n的正方形有s  += i * i (i从1到n) , 可类推出三维中正方体有s += i * i * i(i从1到n), 类推四维。只推出二维边长为n的矩形的规律是 r += 2 * (i * i * (i - 1) / 2)(i从2到n) 。三维的长方体是在找不出式子了,于是就用所有立方体数量的减去正方体的得到了长方体的数量,(1 + ... + n)^3 是n * n的正方体所有的立方体的数量公式,用它减去之前求得的s3就能得到r3,类推得到r4......

#include <iostream>#include <cstdio>#include <cmath>using namespace std;int main() {int n;double s2, r2, s3, r3, s4, r4;while (scanf("%d", &n) != EOF) {s2 = r2 = s3 = r3 = s4 = r4 = 0;for (int i = n; i >= 1; i--) {s2 += pow(i, 2);s3 += pow(i, 3);s4 += pow(i, 4);}for (int i = 2; i <= n; i++)r2 += i * i * (i - 1) / 2;r2 = r2 * 2;r3 = pow((1 + n) * n / 2, 3) - s3;r4 = pow((1 + n) * n / 2, 4) - s4;printf("%.0lf %.0lf %.0lf %.0lf %.0lf %.0lf\n", s2, r2, s3, r3, s4, r4);}return 0;}


0 0
原创粉丝点击