【HDU - 1220】Cube (组合数学,简单)
題干:
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 3Sample Output
0 16 297The results will not exceed int type.Hint
Hint解題報(bào)告:
兩個(gè)小方塊之間的交點(diǎn)個(gè)數(shù)為0,1,2,4;為了方便計(jì)算我們可以用減法,先求出兩個(gè)方塊組合的全部可能情況再減去兩個(gè)方塊相鄰(即交點(diǎn)為四個(gè)的情況)?
兩個(gè)方塊相鄰(交點(diǎn)個(gè)數(shù)為4):每列有n-1對(duì),每面有n列 每面的個(gè)數(shù)即為n*(n-1)?
用正方體左,上,前三面計(jì)算,也就是兩個(gè)方塊相鄰總情況數(shù)為3*n*(n-1)?
公式:C(2, n^3) - 3*n*(n-1) = n*n*n*(n*n*n-1)/2 - 3*n*n*n+3*n*n題解源
AC代碼:
#include<bits/stdc++.h>using namespace std;int main(){int n;while(~scanf("%d", &n)) {printf("%d\n", n*n*n*(n*n*n-1)/2 - 3*n*n*n+3*n*n);}return 0; }在找四個(gè)交點(diǎn)(共面)的時(shí)候,還有一種理解,相當(dāng)于是枚舉了所共的每一個(gè)面
? ? ? ? ? ? ?4個(gè)交點(diǎn)的立方體對(duì)是兩個(gè)立方體共面的情況,求出大的立方體一共有多少個(gè)單位面積的公共面;
? ? ? ? ? ? ?即所有單位立方體的面數(shù)6*n^3減去在大立方體表面的面數(shù)6*n^2就可以了
AC代碼:
#include<stdio.h> int main() {int n,m;while(~scanf("%d",&n)){m=(n*n*n*(n*n*n-1)/2)-(6*n*n*n-6*n*n)/2;printf("%d\n",m);} }?
總結(jié)
以上是生活随笔為你收集整理的【HDU - 1220】Cube (组合数学,简单)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 用上骁龙旗舰芯:曝华为MatePad P
- 下一篇: 【HDU - 1702 】ACboy n