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

来源:互联网 发布:淘宝上的新奇玩意 编辑:程序博客网 时间:2024/06/02 05:08

(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

决解方案:先找出squares  rectangles cubes  boxes hyper-cubes  hyper-boxes的种类,这个应该很容易,然后看这些类型在各种维的cube中有多少,加起来即可,算法可能效率比较低,而且要用到64bit大的数。

代码:

#include<iostream>#include<cstdio>using namespace std;int main(){    long long n;    while(~scanf("%lld",&n))    {        long long s2=0;        for(long long i=1;i<=n;i++)            s2+=i*i;        cout<<s2<<" ";        long long r2=0;        for(long long i1=1;i1<=n;i1++)            for(long long i2=1;i2<=n;i2++)        {            if(i1!=i2)                r2+=(n-i1+1)*(n-i2+1);        }        cout<<r2<<" ";        long long s3=0;         for(long long i=1;i<=n;i++)            s3+=i*i*i;        cout<<s3<<" ";        long long r3=0;         for(long long i1=1;i1<=n;i1++)            for(long long i2=1;i2<=n;i2++)                for(long long i3=1;i3<=n;i3++)                 {            if(i1!=i2||i1!=i3||i2!=i3)                r3+=(n-i1+1)*(n-i2+1)*(n-i3+1);                  }                  cout<<r3<<" ";                  long long s4=0;                    for(long long i=1;i<=n;i++)            s4+=i*i*i*i;        cout<<s4<<" ";        long long r4=0;          for(long long i1=1;i1<=n;i1++)            for(long long i2=1;i2<=n;i2++)                for(long long i3=1;i3<=n;i3++)                    for(long long i4=1;i4<=n;i4++)                 {            if(i1!=i2||i1!=i3||i2!=i3||i1!=i4||i4!=i3||i2!=i4)                r4+=(n-i1+1)*(n-i2+1)*(n-i3+1)*(n-i4+1);                  }                  cout<<r4;        cout<<endl;    }    return 0;}


0 0
原创粉丝点击