Two Knights(数学归纳)
题目描述
Your task is to count for k=1,2,...,n the number of ways two knights can be placed on a k * k chessboard so that they do not attack each other.
输入
The only input line contains an integer n(1 ≤ n ≤ 10000).
输出
Print n integers: the results.
样例输入 Copy
8样例输出 Copy
0 6 28 96 252 550 1056 1848
不考虑攻击,可选的位置有:
k*k的格子内,两个马可以放 (k*k)*(k*k-1)/2 个(去掉重复)
考虑攻击,两个马攻击的时候他们是对称的,所以计算只考虑半边8/2=4个
对于其中一个方向右1上2(=左1下2),马可以有效攻击的格子范围:(行-2)*(列-1)
(如果此时再计算左1下2,就是把这一组多算了一遍)
每一个方向公式都一样,所以一个马在不同位置可能攻击到的格子总数为4*(k-2)*(k-1)
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
int main(){cin>>n;for(ll i=1;i<=n;++i){cout<<i*i*(i*i-1)/2-4*(i-2)*(i-1)<<'\n';}return 0;
}