Harmonious Rectangle 南京ICPC2020 暴力枚举
题目大意:
定义一种和谐矩阵,如果存在一个矩阵的四个角的位置满足两个角的颜色相同,并且另外两个角的颜色也是一样的,这样的矩阵称为和谐矩阵,给你一个 (n*m) 的矩阵,你有三种颜色对矩阵的每一个位置进行填充,问最后这个矩阵里存在至少一个和谐矩阵的方案数是多少?
题解:
因为只有三种颜色,这三种颜色可以组成 3 * 3 = 9 种不同的方案,所以如果 n > 9,那么一定会出现重复,同理 m > 9 也是如此。 (0 0,0 1,0 2,1 0,1 1,1 2,2 0,2 1,2 2)
对于 (n<=9&&m<=9) 的情况,暴力打表即可。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
#define lson (id<<1)
#define rson (id<<1|1)
using namespace std;
const int maxn = 2e5+10;
const int mod = 1e9+7;
typedef long long ll;
ll binpow(ll x,ll k){
x%=mod;
ll ans = 1;
while(k){
if(k&1) ans = ans * x % mod;
x = x*x%mod;
k>>=1;
}
return ans;
}
ll ans[10][10]={
0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,
0,1,15,339,4761,52929,517761,4767849,43046721,387420489,
0,1,339,16485,518265,14321907,387406809,460338013,429534507,597431612,
0,1,4761,518265,43022385,486780060,429534507,792294829,175880701,246336683,
0,1,52929,14321907,486780060,288599194,130653412,748778899,953271190,644897553,
0,1,517761,387406809,429534507,130653412,246336683,579440654,412233812,518446848,
0,1,4767849,460338013,792294829,748778899,579440654,236701429,666021604,589237756,
0,1,43046721,429534507,175880701,953271190,412233812,666021604,767713261,966670169,
0,1,387420489,597431612,246336683,644897553,518446848,589237756,966670169,968803245
};
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
if(n==1||m==1) printf("0
");
else if(n>9||m>9) printf("%lld
",binpow(3,n*m));
else printf("%lld
",ans[n][m]);
}
}