QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#352985 | #7989. 三染色 | 275307894a | AC ✓ | 193ms | 5588kb | C++14 | 4.0kb | 2024-03-13 19:20:43 | 2024-03-13 19:20:44 |
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
#define eb emplace_back
#define all(x) x.begin(),x.end()
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>;
const int N=50+5,M=(1<<20)+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const ll INF=1e18+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
#ifdef LOCAL
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
#else
#define gdb(...) void()
#endif
}using namespace Debug;
int n,m,k,A[6][N],vis[243][243],pw[10];
int check(array<array<int,2>,5> a){
for(int i=1;i<n;i++){
vector<int> b={a[i][0],a[i][1],a[i-1][0],a[i-1][1]};
sort(b.begin(),b.end());
if(!count(all(b),0)||!count(all(b),1)||!count(all(b),2)) continue;
for(int o:{0,1}) if(a[i-1][o]==a[i][o]||a[i-o][0]==a[i-o][1]) return 0;
}
return 1;
}
void add(ll &x,ll y){(x+=y)>=mod&&(x-=mod);}
namespace Solve1{
ll f[243],g[243];
void Solve(){
for(int i=0;i<k;i++){
int flag=0;
for(int j=0;j<n;j++)if(A[j][1]^3&&A[j][1]!=i/pw[j]%3) flag=1;
f[i]=!flag;
}
for(int i=2;i<=m;i++){
Mc(g,f);Me(f,0);
for(int j=0;j<k;j++){
int flag=0;
for(int h=0;h<n;h++) if(A[h][i]^3&&A[h][i]!=j/pw[h]%3) flag=1;
if(flag) continue;
for(int h=0;h<k;h++) if(vis[h][j]) add(f[j],g[h]);
}
}
ll tot=0;for(int i=0;i<k;i++) add(tot,f[i]);
printf("%lld ",tot);
}
}
namespace Solve2{
ll f[243][57][6],g[243][57][6];
int mi[243],is[243];
void Solve(){
for(int i=0;i<k;i++){
array<int,5> a;
for(int j=0;j<n;j++) a[j]=i/pw[j]%3;
int w=0;mi[i]=0;is[i]=0;
for(int j=1;j<n;j++){
if((a[j-1]+1)%3==a[j]) w++;
else if((a[j-1]+2)%3==a[j]) w--;
if(w<mi[i]) mi[i]=w,is[i]=j;
}
}
for(int i=0;i<k;i++){
int flag=0;
for(int j=0;j<n;j++)if(A[j][1]^3&&A[j][1]!=i/pw[j]%3) flag=1;
if(!flag){
for(int j=0;j<=m+n;j++) if(j+mi[i]>=0){
if(j+mi[i]) f[i][j][n]=1;
else f[i][j][is[i]]=1;
}
}
}
for(int i=0;i<k;i++) for(int j=0;j<m+n;j++) f[i][j][0]=0;
for(int i=2;i<=m;i++){
Mc(g,f);Me(f,0);
for(int j=0;j<k;j++){
int flag=0;
for(int h=0;h<n;h++) if(A[h][i]^3&&A[h][i]!=j/pw[h]%3) flag=1;
if(flag) continue;
for(int h=0;h<k;h++)if(vis[h][j]){
for(int d=0;d<=m+n;d++){
int ds=d;
if((h+2)%3==j%3) ds--;
else if((h+1)%3==j%3) ds++;
if(ds+mi[j]>0){
for(int o=0;o<=n;o++)if(g[h][d][o]){
if(!o) add(f[j][ds][0],g[h][d][0]);
else if(o==1) add(f[j][ds][0],g[h][d][1]*(i-1)%mod);
else if(o^n) add(f[j][ds][o-1],g[h][d][o]);
else add(f[j][ds][n],g[h][d][n]);
}
}else if(ds+mi[j]==0){
for(int o=0;o<=n;o++) if(g[h][d][o]){
if(!o) add(f[j][ds][0],g[h][d][0]);
else if(o==1||!is[j]) add(f[j][ds][0],g[h][d][o]*(i-1)%mod);
else add(f[j][ds][min(o-1,is[j])],g[h][d][o]);
}
}
}
}
}
}
ll tot=0;
for(int j=0;j<k;j++) for(int o=0;o<=n+m;o++){
tot+=f[j][o][0];
for(int h=1;h<n;h++) tot+=f[j][o][h]*(m-1+h)%mod;
}
printf("%lld\n",tot%mod);
}
}
void Solve(){
int i,j;scanf("%d%d",&n,&m);
for(pw[0]=i=1;i<=n;i++) pw[i]=pw[i-1]*3;k=pw[n];
for(i=0;i<n;i++) for(j=1;j<=m;j++) scanf("%d",&A[i][j]);
for(i=0;i<k;i++)for(j=0;j<k;j++){
array<array<int,2>,5> a;
for(int h=0;h<n;h++) a[h][0]=i/pw[h]%3,a[h][1]=j/pw[h]%3;
vis[i][j]=check(a);
}
Solve1::Solve();
Solve2::Solve();
}
int main(){
int t=1;
// scanf("%d",&t);
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: 1ms
memory: 5312kb
input:
2 2 1 0 3 2
output:
1 2
result:
ok single line: '1 2'
Test #2:
score: 0
Accepted
time: 10ms
memory: 5568kb
input:
5 5 3 3 3 3 2 2 3 3 3 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3
output:
50830224 170059345
result:
ok single line: '50830224 170059345'
Test #3:
score: 0
Accepted
time: 91ms
memory: 5568kb
input:
5 50 3 3 3 3 3 3 3 3 1 0 3 3 3 1 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 1 3 3 1 3 3 3 3 3 3 3 3 3 1 3 3 3 3 2 3 3 3 3 2 0 3 0 3 3 3 0 3 3 3 3 3 3 3 0 0 3 3 3 3 1 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 1 3 3 3 0 1 3 3 3 0 3 3 3 3 3 2 3 3 1 3 3 0 3 3 3...
output:
988900801 3995091
result:
ok single line: '988900801 3995091'
Test #4:
score: 0
Accepted
time: 193ms
memory: 5588kb
input:
5 50 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3...
output:
442413777 294837747
result:
ok single line: '442413777 294837747'
Test #5:
score: 0
Accepted
time: 153ms
memory: 5524kb
input:
5 50 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 3 3 3 3...
output:
225441044 884828241
result:
ok single line: '225441044 884828241'
Test #6:
score: 0
Accepted
time: 76ms
memory: 5568kb
input:
5 50 3 3 3 3 3 3 3 1 3 3 3 3 3 0 3 3 3 3 3 3 3 3 3 3 3 0 3 1 3 3 3 3 3 3 3 3 1 3 2 3 3 0 2 2 3 3 3 3 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0 3 3 3 1 3 3 1 3 0 3 3 3 3 3 3 0 2 3 3 3 3 0 1 3 3 3 0 3 3 3 0 3 3 3 3 2 3 3 3 3 3 3 3 1 0 3 3 3 3 3 3 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3...
output:
864217599 942336468
result:
ok single line: '864217599 942336468'
Test #7:
score: 0
Accepted
time: 97ms
memory: 5460kb
input:
5 50 3 3 3 3 3 3 1 3 3 3 3 2 3 3 3 3 3 3 3 1 3 3 0 3 3 3 3 3 0 3 3 3 3 3 2 3 3 3 3 3 3 2 3 3 3 1 3 3 3 3 2 3 3 3 3 0 3 3 3 3 3 3 3 3 3 1 3 3 3 3 1 3 3 3 2 3 3 3 1 3 1 2 0 3 3 3 3 3 0 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 2 3 3 1 3 3 3...
output:
436364669 329259414
result:
ok single line: '436364669 329259414'
Test #8:
score: 0
Accepted
time: 39ms
memory: 5524kb
input:
5 50 2 3 3 2 3 2 1 3 0 0 1 3 2 3 2 3 3 0 3 2 3 3 0 2 3 3 0 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 2 0 3 3 3 3 2 1 2 1 1 3 3 1 0 0 3 3 3 3 0 3 0 3 3 3 3 3 3 3 3 2 3 3 1 3 2 2 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 0 1 3 1 3 3 3 3 2 3 3 3 0 3 3 3 3 3 0 2 2 3 3 1 3 3 3 0 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3...
output:
0 0
result:
ok single line: '0 0'
Test #9:
score: 0
Accepted
time: 151ms
memory: 5536kb
input:
5 50 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3...
output:
810142538 871482893
result:
ok single line: '810142538 871482893'
Test #10:
score: 0
Accepted
time: 95ms
memory: 5584kb
input:
5 50 3 3 3 3 2 3 1 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 0 0 3 3 0 3 0 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 2 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 0 3 3 3 0 1 0 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 2 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 0 3 3 3 2 2 3 3 3 1 3 3 2 0 3 3 2 3 3...
output:
751568411 253164328
result:
ok single line: '751568411 253164328'
Test #11:
score: 0
Accepted
time: 36ms
memory: 5572kb
input:
5 50 3 3 3 0 3 1 3 3 3 3 3 2 1 3 3 3 1 3 0 3 3 3 3 2 3 0 3 3 3 3 3 0 3 3 3 2 3 3 3 1 0 0 3 3 3 3 3 2 1 2 0 3 0 3 0 3 3 2 2 3 3 2 3 1 3 0 3 3 3 2 3 3 0 3 3 3 3 1 2 3 2 0 0 0 2 3 0 3 3 3 3 1 3 3 1 3 2 0 3 3 3 1 3 3 0 3 1 1 3 3 1 1 3 3 3 3 1 1 2 3 3 2 3 3 1 3 2 3 1 3 2 3 3 2 0 3 1 3 1 3 3 0 3 0 3 3 0 0...
output:
0 0
result:
ok single line: '0 0'
Test #12:
score: 0
Accepted
time: 95ms
memory: 5536kb
input:
5 50 3 3 3 3 3 3 2 3 3 3 0 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 0 3 0 3 3 3 3 3 3 3 3 3 0 3 3 3 3 3 3 3 3 3 3 3 3 0 0 3 3 3 3 3 3 0 3 3 3 3 2 3 3 3 3 0 2 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 2 3 3 3 3 2 3 1 1 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 0 1 3 3 3 3 3 3 3 2 0...
output:
0 0
result:
ok single line: '0 0'
Test #13:
score: 0
Accepted
time: 38ms
memory: 5464kb
input:
5 50 3 1 0 3 2 3 3 3 1 3 0 2 2 2 1 2 0 3 3 0 1 3 3 3 2 2 3 3 0 3 3 3 3 2 3 3 3 1 2 3 3 2 3 3 3 1 3 3 3 0 3 3 2 3 3 3 3 0 3 1 3 3 0 3 3 3 3 3 3 3 2 0 2 3 3 3 3 3 0 1 3 0 2 3 1 3 2 1 3 3 3 3 3 3 3 3 3 3 3 3 3 2 0 2 3 3 0 3 3 3 2 2 3 3 3 3 1 3 1 3 3 3 3 1 3 3 3 3 3 3 0 3 0 3 0 3 2 3 3 3 3 3 3 0 2 3 3 1...
output:
200661729 258704668
result:
ok single line: '200661729 258704668'
Test #14:
score: 0
Accepted
time: 41ms
memory: 5568kb
input:
5 50 0 2 3 3 3 3 3 3 3 3 2 3 3 3 2 3 2 2 3 3 0 3 1 3 3 3 3 2 1 3 3 3 3 1 3 1 3 1 2 3 2 3 0 0 0 3 3 3 3 1 3 3 1 3 1 0 3 3 2 3 3 2 0 3 3 3 3 1 3 2 3 0 3 3 3 3 3 3 3 3 3 1 1 3 3 0 3 2 1 3 1 0 3 0 0 3 3 2 3 1 0 0 3 1 3 3 3 3 3 3 2 1 3 1 3 1 3 2 3 3 3 0 1 3 3 3 3 3 1 2 0 3 2 0 3 2 3 3 1 1 3 1 3 3 3 3 0 1...
output:
0 0
result:
ok single line: '0 0'
Test #15:
score: 0
Accepted
time: 4ms
memory: 5324kb
input:
2 50 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
output:
780628942 499420229
result:
ok single line: '780628942 499420229'
Test #16:
score: 0
Accepted
time: 145ms
memory: 5508kb
input:
5 50 3 0 3 3 3 3 3 3 3 3 3 3 3 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3...
output:
798118080 776193618
result:
ok single line: '798118080 776193618'
Test #17:
score: 0
Accepted
time: 175ms
memory: 5444kb
input:
5 50 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 3 3 3 3 3...
output:
622800277 243172909
result:
ok single line: '622800277 243172909'
Test #18:
score: 0
Accepted
time: 152ms
memory: 5456kb
input:
5 50 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 3 3 3 2 3 3 2 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 2 2 3 3 3 3 3 3 3 3 3 3 3 3 0 3 3 2 3 3 3 3 3 3 3...
output:
480284116 641719784
result:
ok single line: '480284116 641719784'
Test #19:
score: 0
Accepted
time: 187ms
memory: 5536kb
input:
5 50 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3...
output:
442413777 294837747
result:
ok single line: '442413777 294837747'
Test #20:
score: 0
Accepted
time: 186ms
memory: 5544kb
input:
5 50 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3...
output:
442413777 294837747
result:
ok single line: '442413777 294837747'
Test #21:
score: 0
Accepted
time: 7ms
memory: 5568kb
input:
5 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
output:
62340543 139608630
result:
ok single line: '62340543 139608630'
Test #22:
score: 0
Accepted
time: 67ms
memory: 5564kb
input:
5 28 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
output:
585830006 459695279
result:
ok single line: '585830006 459695279'