(2/3/4)-D Sqr/Rects/Cubes/Boxes?

来源:互联网 发布:trackpad windows 编辑:程序博客网 时间:2024/05/21 10:01

(2/3/4)-D Sqr/Rects/Cubes/Boxes?

Input: standard input

Output: standard output

Time Limit: 2 seconds

 

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 of N 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


解题报告

就是找规律的题目。。。

求空间上2、3、4维空间上的正方形个数和纯矩形(不包括正方形)

我是先找正方形的个数,用个函数来表示。。。

N=2,2维是2*2+1*1,3维是3*3+2*2+1*1,4维是4*4+3*3+2*2+1*1

长方形就不好找规律。。。

你会发现N=2,2维的长方形加正方形=9,3维的长方形加正方形=27。。。

N=3,2维的长方形加正方形=36,3维的长方形加正方形=216。。。

规律就在眼前。。。


#include<stdio.h>#include<math.h>long long s(int x,int y){    long long sum=0;    for(int i=1;i<=x;i++)    sum+=pow(i,y);    return sum;}long long r(int x,int y){    return pow((x-1)*(2+x)/2+1,y)-s(x,y);}int main (){    int n;    while(scanf("%d",&n)!=EOF){    printf("%lld %lld %lld %lld %lld %lld\n",s(n,2),r(n,2),s(n,3),r(n,3),s(n,4),r(n,4));    }    return 0;}


0 0
原创粉丝点击