Description
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:
12
3
Sample Output:
1 0 1 0 1 05 4 9 18 17 64
14 22 36 180 98 1198
Shahriar Manzoor
#include <stdio.h> int main() { long long s2,r2,s3,r3,s4,r4; long long ssum,rsum; int n; int l1,l2,l3,l4; int i,k,j,h; while (~scanf ("%d",&n)) { ssum = 0; for (i = 1;i <= n;i++) { l1 = n - i + 1; ssum += l1 * l1; } rsum = 0; for (i = 1; i <= n; i++) for (k = 1; k <= n; k++) { l1 = n - i + 1; l2 = n - k + 1; rsum += l1 * l2; } s2 = ssum; r2 = rsum - ssum; ssum = 0; for (i = 1;i <= n;i++) { l1 = n - i + 1; ssum += l1 * l1 * l1; } rsum = 0; for (i = 1;i <=n;i++) for (k = 1;k <= n;k++) for (j = 1;j <= n;j++) { l1 = n - i + 1; l2 = n - k + 1; l3 = n - j + 1; rsum += l1 * l2 * l3; } s3 = ssum; r3 = rsum - ssum; ssum = 0; for (i = 1;i <= n;i++) { l1 = n - i + 1; ssum += l1 * l1 * l1 * l1; } rsum = 0; for (i = 1;i <= n;i++) for (k = 1;k <= n;k++) for (j = 1;j <= n;j++) for (h = 1;h <= n;h++) { l1 = n - i + 1; l2 = n - k + 1; l3 = n - j + 1; l4 = n - h + 1; rsum += l1 * l2 * l3 *l4; } s4 = ssum; r4 = rsum - ssum; printf ("%lld %lld %lld %lld %lld %lld\n",s2,r2,s3,r3,s4,r4); } return 0; }
在写的时候就担心TLE,果然,不粗所料,TLE,见红了。
#include <stdio.h> int an[102]; int main() { long long s2,r2,s3,r3,s4,r4; long long ssum,rsum; int n; int l1,l2,l3,l4; int i,k,j,h; freopen ("1.txt","r",stdin); while (~scanf ("%d",&n)) { for (i = 1;i <= n;i++) an[i] = n - i + 1; ssum = 0; for (i = 1;i <= n;i++) { l1 = an[i]; ssum += l1 * l1; } rsum = 0; for (i = 1; i <= n; i++) for (k = 1; k <= n; k++) { l1 = an[i]; l2 = an[k]; rsum += l1 * l2; } s2 = ssum; r2 = rsum - ssum; ssum = 0; for (i = 1;i <= n;i++) { l1 = an[i]; ssum += l1 * l1 * l1; } rsum = 0; for (i = 1;i <=n;i++) for (k = 1;k <= n;k++) for (j = 1;j <= n;j++) { l1 = an[i]; l2 = an[k]; l3 = an[j]; rsum += l1 * l2 * l3; } s3 = ssum; r3 = rsum - ssum; ssum = 0; for (i = 1;i <= n;i++) { l1 = an[i]; ssum += l1 * l1 * l1 * l1; } rsum = 0; for (i = 1;i <= n;i++) for (k = 1;k <= n;k++) for (j = 1;j <= n;j++) for (h = 1;h <= n;h++) { l1 = an[i]; l2 = an[k]; l3 = an[j]; l4 = an[h]; rsum += l1 * l2 * l3 *l4; } s4 = ssum; r4 = rsum - ssum; printf ("%lld %lld %lld %lld %lld %lld\n",s2,r2,s3,r3,s4,r4); } return 0; }
居然AC了,其实时间和内存还是很恐怖的
事实证明,这样时间还是很悬的,但是也同样证明,这样的确可以缩短时间。