uva 10177(数学)

题目:

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

题解:二维边长为n的正方形有s  += i * i (i从1到n) , 可类推出三维中正方体有s += i * i * i(i从1到n), 类推四维。只推出二维边长为n的矩形的规律是 r += 2 * (i * i * (i - 1) / 2)(i从2到n) 。三维的长方体是在找不出式子了,于是就用所有立方体数量的减去正方体的得到了长方体的数量,(1 + ... + n)^3 是n * n的正方体所有的立方体的数量公式,用它减去之前求得的s3就能得到r3,类推得到r4......

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main() {
	int n;
	double s2, r2, s3, r3, s4, r4;
	while (scanf("%d", &n) != EOF) {
		s2 = r2 = s3 = r3 = s4 = r4 = 0;
		for (int i = n; i >= 1; i--) {
			s2 += pow(i, 2);
			s3 += pow(i, 3);
			s4 += pow(i, 4);
		}
		for (int i = 2; i <= n; i++)
			r2 += i * i * (i - 1) / 2;
		r2 = r2 * 2;
		r3 = pow((1 + n) * n / 2, 3) - s3;
		r4 = pow((1 + n) * n / 2, 4) - s4;
		printf("%.0lf %.0lf %.0lf %.0lf %.0lf %.0lf\n", s2, r2, s3, r3, s4, r4);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值