题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1220
解法一:
/*
题解:因为公共点的数目只可能有:0,1,2,4.
很明显我们用总的对数减掉有四个公共点的对数就可以了。
总的公共点对数:C(2,n^3)= n^3*(n^3-1)/2(一共有n^3块小方块,从中选出2块)。
因为(只有两个小方块之间才存在公共点,我们从所有的小方块中任意选出两个,自然就确定了这两个小方块的公共点的对数,从所有小方块中任意选取两个,所以总的选取方法数就是所有种类对数数目的总和!)?
公共点为4的对数:一列有n-1对(n个小方块,相邻的两个为一对符合要求),
一个面的共有 n^2列,选
上面和左面,前面三个方向,同理可得,故总数为:3*n^2(n-1)。
综上:
所以公数学 式为:(n^3*(n^3-1))/2 - 3*n^2(n-1)。
*/
#include <iostream>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
int n;
while (cin >> n) {
cout << (n * n * n * (n * n * n - 1)) / 2 - 3 * (n * n) * (n - 1)
<< endl;
}
return 0;
}
解法二:
/**
联想高中数学题,边长n的正方体涂满6个面油漆,然后切成n*n*n个边长1的小正方体,
有一面油漆的小正方体 == 有5面与其他小正方体共用4点,以此类推2面 3面等
*/
#include <stdio.h>
int main() {
int n; //正方体切成n*n*n个小正方体
while (scanf("%d", &n) != EOF) {
if (n == 1) {
printf("0\n");
} else if (n == 2) {
printf("16\n");
} else {
int total = n * n * n;
int n3 = 8; // 3面与其他小正方体相邻
int n4 = (n - 2) * 12; // 4面与其他小正方体相邻
int n5 = (n - 2) * (n - 2) * 6; // 5面与其他小正方体相邻
int n6 = (n - 2) * (n - 2) * (n - 2); // 6面与其他小正方体相邻
int res = n3 * (total - 3 - 1) + n4 * (total - 4 - 1) +
n5 * (total - 5 - 1) + n6 * (total - 6 - 1);
printf("%d\n", res / 2);
}
}
}