QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#152431#6425. Harmonious Rectangleqzez#TL 2ms3984kbC++141.6kb2023-08-28 08:24:372023-08-28 08:24:38

Judging History

你现在查看的是最新测评结果

  • [2023-08-28 08:24:38]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:3984kb
  • [2023-08-28 08:24:37]
  • 提交

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=2e3+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(f[n][m]){printf("%lld\n",g[n][m]);return;}
	if(n==1||m==1){puts("0");return;}
	ans=mpow(3,n*m);
	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';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3984kb

input:

3
1 4
2 2
3 3

output:

0
15
16485

result:

ok 3 number(s): "0 15 16485"

Test #2:

score: -100
Time Limit Exceeded

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...

result: