A knight jumps around an infinite chessboard. The chessboard is an unexplored territory. In the spirit of explorers, whoever stands on a square for the first time claims the ownership of this square. The knight initially owns the square he stands, and jumps
N times before he gets bored.
Recall that a knight can jump in 8 directions. Each direction consists of two squares forward and then one squaure sidways.
After N jumps, how many squares can possibly be claimed as territory of the knight? As N
Each test case contains only one number N, indicating how many times the knight jumps.
1≤T≤105
0≤N≤109
Output For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the number of squares that can possibly be claimed by the knight. Sample Input
Recall that a knight can jump in 8 directions. Each direction consists of two squares forward and then one squaure sidways.
After N jumps, how many squares can possibly be claimed as territory of the knight? As N
can be really large, this becomes a nightmare to the knight who is not very good at math. Can you help to answer this question?
Input The first line of the input gives the number of test cases,
T.
T test cases follow.
Each test case contains only one number N, indicating how many times the knight jumps.
1≤T≤105
0≤N≤109
Output For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the number of squares that can possibly be claimed by the knight. Sample Input
3 0 1 7Sample Output
Case #1: 1 Case #2: 9 Case #3: 649
打表代码
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
int fx[]={0,2,2,1,1,-1,-1,-2,-2};
int fy[]={0,1,-1,2,-2,2,-2,1,-1};
const int N=1000+5;
bool mat[2][N][N];
unsigned long long ans;
void color(bool f){
int g=(f+1)%2;
memset(mat[g],false,sizeof(mat[g]));
for(int x=0;x<N;x++)
for(int y=0;y<N;y++)
if(mat[f][x][y]){
for(int k=0;k<9;k++)
mat[g][x+fx[k]][y+fy[k]]=true;
}
for(int x=0;x<N;x++)
for(int y=0;y<N;y++)
if(mat[g][x][y])
ans++;
}
int main(){
mat[1][500][500]=true;
for(int i=1;i<=100;i++){
ans=0;
color(i%2);
printf("%d\n",ans);
}
}
1,9,41,109,205,325,473,649,853,1085,1345,1633,1949,2293,2665,3065,3493,3949,4433,4945,5485,6053,6649,7273,7925,8605,9313,10049,10813,11605,12425,13273,14149,15053,15985,16945,17933,18949,19993,21065,22165,23293,24449,25633,26845,28085,29353,30649,31973,33325,34705,36113,37549,39013,40505,42025,43573,45149,46753,48385,50045,51733,53449,55193,56965,58765,60593,62449,64333,66245,68185,70153,72149,74173,76225,78305,80413,82549,84713,86905,89125,91373,93649,95953,98285,100645,103033,105449,107893,110365,112865,115393,117949,120533,123145,125785,128453,131149,133873,136625,139405,
8,32,68,96,120,148,176,204,232,260,288,316,344,372,400,428,456,484,512,540,568,596,624,652,680,708,736,764,792,820,848,876,904,932,960,988,1016,1044,1072,1100,1128,1156,1184,1212,1240,1268,1296,1324,1352,1380,1408,1436,1464,1492,1520,1548,1576,1604,1632,1660,1688,1716,1744,1772,1800,1828,1856,1884,1912,1940,1968,1996,2024,2052,2080,2108,2136,2164,2192,2220,2248,2276,2304,2332,2360,2388,2416,2444,2472,2500,2528,2556,2584,2612,2640,2668,2696,2724,2752,2780,
24,36,28,24,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,
#include<iostream>
using namespace std;
typedef unsigned long long ull;
ull ans[]={1,9,41,109,205,325,473,649,853,1085};
int main(){
int z;
scanf("%d",&z);
ull n;
for(int i=1;i<=z;i++){
cin>>n;
if(n<10)printf("Case #%d: %llu\n",i,ans[n]);
else printf("Case #%d: %llu\n",i,14llu*n*n-6llu*n+5llu);
}
}