题目:
Cowl is good at solving math problems. One day a friend asked him such a question: You are given a cube whose edge length is N, it is cut by the planes that was paralleled to its side planes into N * N * N unit cubes. Two unit cubes may have no common points or two common points or four common points. Your job is to calculate how many pairs of unit cubes that have no more than two common points.
Process to the end of file.
Input
There will be many test cases. Each test case will only give the edge length N of a cube in one line. N is a positive integer(1<=N<=30).
Output
For each test case, you should output the number of pairs that was described above in one line.
Sample Input
1 2 3
Sample Output
0 16 297 The results will not exceed int type.
Hint
Hint
解题报告:给定一个长度为n的正方立体,用平面切割为n^3个大小相等的小正方体,为有多少对(两个正方体)的交点不超过4个的数目。上来居然想跑斐波那契,想想真的是呆了,这道题目六个月之前做过,很尴尬,总结了规律:你会发现任意两个正方体的相交的点的数目为0 1 2 4,求解不超过2的点的正方体对数,你会发现只要减去相交点数为4的就可以,总结公式,一共有n^3个正方体,选出两个C(n^2,2)为总数,相交点数大于4的对数为 (n-1)*n*n*3(n-1为一行/列/竖的对数,n*n是总行数,3的话是3个方向)二者做差就可以得到答案。
ac代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",n*n*n*(n*n*n-1)/2-3*n*n*(n-1));
}
return 0;
}