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

Problem J

(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

题意:

在N边的正方形,正方体,超正方体(4维平面),内分别有多少个正方形(体,超体),长方形(体,超体);

解析:
对于N边的正方形的个数:
正方形为:N*N+(N-1)*(N-1)+…………2*2+1*1;
正方体为N*N*N+(N-1)*(N-1)*(N-1)+…………2*2*2+1*1*1;
正方超体为N*N*N*N+(N-1)*(N-1)*(N-1)*(N-1)+…………+1*1*1*1;
对于N边的正方形,所含的矩形的数目为I*J(其中I的范围是1~N,J的范围也是1~N)之和,
即为(i*(i+1)/2)*(i*(i+1)/2),所以其所含的长方形的数目为矩形的数目减去正方形的数目。
同理求出,正方体,超正方体。所含的长方形的和。

#include <stdio.h>
#include <string.h>

const int N = 110;

int main() {
	int n;
	long long s2,s3,s4,r2,r3,r4;
	while(scanf("%d",&n) != EOF) {
		s2 = s3 = s4 = r2 = r3 =r4=0;
		for(int i=0;i<=n;i++) {
			s2 += i*i;	
			s3 += i*i*i;
			s4 += i*i*i*i;
		}
		long long tmp = n*(n+1)/2;
		r2 = tmp*tmp - s2;
		r3 = tmp*tmp*tmp - s3;
		r4 = tmp*tmp*tmp*tmp - s4;
		printf("%lld %lld %lld %lld %lld %lld\n",s2,r2,s3,r3,s4,r4);
	}
	return 0;
}


你可能感兴趣的:(uva,234-D,SqrRectsCubesB)