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

本文探讨了如何高效地计算四维空间中不同尺寸的立方体和超立方体的数量,通过数学公式推导和编程实现,解决了二维到四维空间的几何体计数问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


数学公式推导的题目,话说最近一直在做这类题目。



四维情况的推导和三维情况的推导是类似的。
很容易得到:
S4 = n^4+(n-1)^4+...+1^4
R4 = n*(S3+R3-n^3)+(n-1)*(S3+R3-(n-1)^3)+....+1*(S3+R3-1^3).
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {

    //freopen("aa.in", "r", stdin);
    //freopen("bb.out", "w", stdout);

    int n;
    long long S2, R2, S3, R3, S4, R4;
    while(cin >> n) {
        S2 = n * (2 * n + 1) * (n + 1) / 6;
        R2 = 0;
        for(int i = n; i >= 1; --i) {
            R2 += i * i * (i - 1);
        }
        S3 = R2 + S2;
        R3 = ((n + 1) * n / 2 - 1) * S3;
        S4 = 0;
        for(int i = n; i >= 1; --i) {
            S4 += i * i * i * i;
        }
        R4 = 0;
        for(int i = n; i >= 1; --i) {
            R4 += i * (S3 + R3 - i * i * i);
        }
        cout << S2 << " " << R2 << " " << S3 << " " << R3 << " " << S4 << " " << R4 << endl;
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值