QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#152435 | #6425. Harmonious Rectangle | qzez# | RE | 2ms | 3876kb | C++14 | 1.6kb | 2023-08-28 08:29:20 | 2023-08-28 08:29:21 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;using LL=__int128;
const int N=10+5,M=3e5+5,K=(1<<15)+5,mod=1e9+7,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(time(0));
int n,m,A[N][N],f[N][3];ll ans;
ll mpow(ll x,int y=mod-2){ll ans=1;while(y) y&1&&(ans=ans*x%mod),y>>=1,x=x*x%mod;return ans;}
int CK(int x,int y){
if(f[x][0]>3||f[x][1]>3||f[x][2]>3) return 0;
for(int i=1;i<x;i++) for(int j=1;j<y;j++){
if(A[i][j]==A[i][y]&&A[x][j]==A[x][y]) return 0;
if(A[i][j]==A[x][j]&&A[i][y]==A[x][y]) return 0;
}
return 1;
}
void dfs(int x,int y){
if(y==m+1) return dfs(x+1,1);
if(x==n+1) {ans--;return;}
A[x][y]=0;f[x][A[x][y]]++;if(CK(x,y)) dfs(x,y+1);f[x][A[x][y]]--;
A[x][y]=1;f[x][A[x][y]]++;if(CK(x,y)) dfs(x,y+1);f[x][A[x][y]]--;
A[x][y]=2;f[x][A[x][y]]++;if(CK(x,y)) dfs(x,y+1);f[x][A[x][y]]--;
}
ll g[N][N];
void Solve(){
int i,j;
scanf("%d%d",&n,&m);
if(n>m) swap(n,m);
if(g[n][m]){printf("%lld\n",g[n][m]);return;}
if(n==1||m==1){puts("0");return;}
ans=mpow(3,n*m);
if(n<=9&&m<=9) dfs(1,1);
g[n][m]=(ans%mod+mod)%mod;
printf("%lld\n",(ans%mod+mod)%mod);
}
int main(){
int t;
scanf("%d",&t);
// t=1;
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3876kb
input:
3 1 4 2 2 3 3
output:
0 15 16485
result:
ok 3 number(s): "0 15 16485"
Test #2:
score: -100
Runtime Error
input:
10000 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 6...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 339 4761 52929 517761 4767849 43046721 387420489 486784380 381059392 429534507 865810542 792294...