[HDU] [1220] Cube

Problem Description

Loading...

Problem Analysis

It can be seen
that the blocks sharing fewer than 4 points with a corner block are all except the three neighboring blocks and itself
that the blocks sharing fewer than 4 points with an edge block are all except the four neighboring blocks and itself
that the blocks sharing fewer than 4 points with a face block are all except the five neighboring blocks and itself
the rest share fewer than 4 with all except the six neighboring blocks and itself

Then add them up and divide the result by 2 (each pair is counted twice)
and the answer is obtained

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include<stdio.h>
#include<algorithm>
using namespace std;
int main() {
	int n;
	while(scanf("%d",&n)!=EOF) {
		int res=0;
		int l=n*n*n;
		res+=8*(l-4);
		res+=(n-2)*12*(l-5);
		res+=((n-2)*(n-2)*6)*(l-6);
		res+=(l-8-(n-2)*12-(n-2)*(n-2)*6)*(l-7);
		printf("%d\n",res/2);
	}
	return 0;
}

Problem link: 【杭电】[1220]Cube

Loading...