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 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 whereS2 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
一直习惯主函数void结果runtime error n次,用了long long 类型不能void....
正方个数(维数w):num=1^w+2^w+3^w+...+n^w
长方个数(维数w): NUM=(n*(n+1)/2)^w-num
靠自己的力量,只发现了正方形个数的公式,而且只会平方和和立方和的化简公式,四次化简的没教过不会Y-Y。
以下是看别人的:
矩形的个数就是就是(n+1)中取2的组合数的平方,也就是(n+1)* n/2 的平方,那么3维也就是3次方,四维就是4次方。。。。矩形搞定了。那么再求出方形的,减一下就能求出纯矩形了。
三维为啥是三次方没想通╮(╯▽╰)╭....代码也是复制来的╮(╯▽╰)╭....可耻的AC了
#include <stdio.h>
int main()
{long long s2,r2,s3,r3,s4,r4,N;
while (scanf("%lld",&N)!=EOF)
{
s2=N*(N+1)*(2*N+1)/6;
r2=(N+1)*N*N*(N+1)/4;
s3=r2;
r3=(N+1)*N*N*(N+1)*N*(N+1)/8;
s4=(6*N*N*N*N*N+15*N*N*N*N+10*N*N*N-N)/30;
r4=(N+1)*N*N*(N+1)*N*(N+1)*N*(N+1)/16;
printf("%lld %lld %lld %lld %lld %lld\n",s2,r2-s2,s3,r3-s3,s4,r4-s4);
}
return 0;
}