LightOj 1332
题目链接:
http://www.bnuoj.com/v3/problem_show.php?pid=13251
题意:
给一个10*n(n<=1e9)的矩阵,现在每列需要放两个国王。
已知国王会在他所在的九宫格范围内实施攻击,问有多少种放置方法使得国王之间不相互攻击。
思路:
刚开始拿到手是一点思路没有。
强行打表找规律,失败。
因为n很大,不是公式就是有递推关系。公式的话肯定由前几项可以算的,那么体现在打的表上就会呈现一定规律。然而失败了,说明一定是递推的关系,因为n很大所以猜用快速幂优化。
然后强行往快速幂那边靠,发现每次取的点进行编号,下一次可以取得点只能由上一次取得一些点得到,所以可以构成一个关系矩阵。
然而我并不相信这是题解,随便写一发没过样例,看了别人代码后悔莫及啊……
源码:
赶时间版本
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
#define LL unsigned long long
typedef pair<int,int> pii;
const LL mod = ((LL)1) << 32;
int vis[15][15];
LL g[40][40];
LL a[40];
map<pii, int>M;
int cnt;
int cal_pos(int x, int y){return (19 - x) * (x - 1) / 2 ;}
bool valid(int row)
{
for(int i = 1 ; i <= 10 ; i++){
if(vis[row][i] && vis[row][i - 1]) return false;
if(vis[row][i] && (vis[row - 1][i - 1] || vis[row - 1][i] || vis[row - 1][i + 1])) return false;
}
return true;
}
void dfs1(int row)
{
// if(row == 2){
// int x1, y1;
// x1 = -1;
// for(int i = 1 ; i <= 10 ; i++){
// if(vis[1][i]){
// if(x1 == -1) x1 = i;
// else y1 = i;
// }
// }
pii temp = make_pair(x1, y1);
if(M[temp] == 0)
// }
if(row == 3){
int x1, y1;
x1 = -1;
for(int i = 1 ; i <= 10 ; i++){
if(vis[1][i]){
if(x1 == -1) x1 = i;
else y1 = i;
}
}
int x2, y2;
x2 = -1;
for(int i = 1 ; i <= 10 ; i++){
if(vis[2][i]){
if(x2 == -1) x2 = i;
else y2 = i;
}
}
// printf("x1 = %d, y1 = %d, x2 = %d, y2 = %d\n,cal1 = %d, cal2 = %d", x1, y1, x2, y2, cal_pos(x1, y1), cal_pos(x2, y2));
// system("pause");
g[M[make_pair(x1, y1)]][M[make_pair(x2, y2)]] = 1;
return;
}
for(int i = 1 ; i <= 10 ; i++){
vis[row][i] = 1;
for(int j = i + 1 ; j <= 10 ; j++){
vis[row][j] = 1;
if(valid(row)) dfs1(row + 1);
vis[row][j] = 0;
}
vis[row][i] = 0;
}
}
void init()
{
M.clear();
cnt = 0;
memset(vis, 0, sizeof(vis));
memset(g, 0, sizeof(g));
for(int i = 1 ; i <= 10 ; i++){
for(int j = i + 2 ; j <= 10 ; j++)
M[make_pair(i, j)] = ++cnt;
}
// printf("%d\n", cnt);
// memset(a, 0, sizeof(a));
dfs1(1);
}
struct Matrix
{
LL d[40][40];
void init(){for(int i = 1 ; i <= 36 ; i++) for(int j = 1 ; j <= 36 ; j++) if(i == j) d[i][j] = 1; else d[i][j] = 0;}
Matrix operator * (const Matrix& b)const{
Matrix ans;
for(int i = 1 ; i <= 36 ; i++){
for(int j = 1 ; j <= 36 ; j++){
ans.d[i][j] = 0;
for(int k = 1 ; k <= 36 ; k++)
ans.d[i][j] = (ans.d[i][j] + d[i][k] * b.d[k][j] % mod) % mod;
}
}
return ans;
}
}org;
Matrix ppow(Matrix a, int x)
{
Matrix ans;
ans.init();
while(x){
if(x & 1) ans = (ans * a);
a = (a * a);
x = x >> 1;
}
return ans;
}
int main()
{
init();
// for(int j = 1 ; j <= 36 ; j++) printf("%d ", g[i][j]);
// printf("\n");
// }
// for(int i = 1 ; i <= 36 ; i++) printf("%d ", a[i]);
int T;
scanf("%d", &T);
for(int cas = 1 ; cas <= T ; cas++){
int n;
scanf("%d", &n);
for(int i = 1 ; i <= 36 ; i++) for(int j = 1 ; j <= 36 ; j++) org.d[i][j] = g[i][j];
Matrix ans = ppow(org, n - 1);
// for(int i = 1 ; i <= 36 ; i++){
// for(int j = 1 ; j <= 36 ; j++) printf("%d ", g[i][j]);
// printf("\n");
// }
LL res = 0;
// printf("%I64d\n", mod);
for(int i = 1 ; i <= 36 ; i++) for(int j = 1 ; j <= 36 ; j++){
LL tres = res;
res = (res + ans.d[i][j]) % mod;
// if(res != tres){
// printf("res = %llu, ans.d[i][j] = %llu\n", res, ans.d[i][j]);
// system("pause");
// }
}
printf("Case %d: %llu\n", cas, res);
}
return 0;
}
好看的版本
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
#define LL unsigned long long
typedef pair<int,int> pii;
const LL mod = ((LL)1) << 32;
int vis[15][15];
LL g[40][40];
LL a[40];
map<pii, int>M;
int cnt;
int cal_pos(int x, int y){return (19 - x) * (x - 1) / 2 ;}
bool valid(int row)
{
for(int i = 1 ; i <= 10 ; i++){
if(vis[row][i] && vis[row][i - 1]) return false;
if(vis[row][i] && (vis[row - 1][i - 1] || vis[row - 1][i] || vis[row - 1][i + 1])) return false;
}
return true;
}
void dfs1(int row)
{
if(row == 3){
int x1, y1;
x1 = -1;
for(int i = 1 ; i <= 10 ; i++){
if(vis[1][i]){
if(x1 == -1) x1 = i;
else y1 = i;
}
}
int x2, y2;
x2 = -1;
for(int i = 1 ; i <= 10 ; i++){
if(vis[2][i]){
if(x2 == -1) x2 = i;
else y2 = i;
}
}
g[M[make_pair(x1, y1)]][M[make_pair(x2, y2)]] = 1;
return;
}
for(int i = 1 ; i <= 10 ; i++){
vis[row][i] = 1;
for(int j = i + 1 ; j <= 10 ; j++){
vis[row][j] = 1;
if(valid(row)) dfs1(row + 1);
vis[row][j] = 0;
}
vis[row][i] = 0;
}
}
void init()
{
M.clear();
cnt = 0;
memset(vis, 0, sizeof(vis));
memset(g, 0, sizeof(g));
for(int i = 1 ; i <= 10 ; i++){
for(int j = i + 2 ; j <= 10 ; j++)
M[make_pair(i, j)] = ++cnt;
}
dfs1(1);
}
struct Matrix
{
LL d[40][40];
void init(){for(int i = 1 ; i <= 36 ; i++) for(int j = 1 ; j <= 36 ; j++) if(i == j) d[i][j] = 1; else d[i][j] = 0;}
Matrix operator * (const Matrix& b)const{
Matrix ans;
for(int i = 1 ; i <= 36 ; i++){
for(int j = 1 ; j <= 36 ; j++){
ans.d[i][j] = 0;
for(int k = 1 ; k <= 36 ; k++)
ans.d[i][j] = (ans.d[i][j] + d[i][k] * b.d[k][j] % mod) % mod;
}
}
return ans;
}
}org;
Matrix ppow(Matrix a, int x)
{
Matrix ans;
ans.init();
while(x){
if(x & 1) ans = (ans * a);
a = (a * a);
x = x >> 1;
}
return ans;
}
int main()
{
init();
int T;
scanf("%d", &T);
for(int cas = 1 ; cas <= T ; cas++){
int n;
scanf("%d", &n);
for(int i = 1 ; i <= 36 ; i++) for(int j = 1 ; j <= 36 ; j++) org.d[i][j] = g[i][j];
Matrix ans = ppow(org, n - 1);
LL res = 0;
for(int i = 1 ; i <= 36 ; i++) for(int j = 1 ; j <= 36 ; j++){
LL tres = res;
res = (res + ans.d[i][j]) % mod;
}
printf("Case %d: %llu\n", cas, res);
}
return 0;
}