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 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
#include<stdio.h> int main() { long long s2, r2, s3, r3, s4, r4, n; int i; while(scanf("%lld", &n) != EOF) { s2 = n*(n+1)*(2*n+1)/6; for(r2=0,i=n; i>=1; --i) r2 += i*i*(i-1); s3 = s2 + r2; r3 = ((n+1)*n/2-1)*s3; for(s4=0,i=n; i>=1; --i) s4 += i*i*i*i; for(r4=0,i=n; i>=1; --i) r4 += i*(s3+r3-i*i*i); printf("%lld %lld %lld %lld %lld %lld\n", s2, r2, s3, r3, s4, r4); } return 0; }
解题思路:
四维N*N*N*N的情况和三维的情况差不多:
S4 = n^4 + (n-1)^4 + ... ... + 1^4
R4 = n*(S3+R3-n^3) + (n-1)*(S3+R3-(n-1)^3) + ... ... + 1*(R3+S3-1^3);
推了两个小时,可耻地AC了(1y)

更多内容请关注个人微信公众号 物役记 (微信号:materialchains)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?