QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#115030 | #5810. Doubly-sorted Grid | zhouhuanyi | 30 ✓ | 5647ms | 45320kb | C++11 | 1.8kb | 2023-06-24 14:30:54 | 2023-06-24 14:30:56 |
Judging History
answer
#include<iostream>
#include<cstdio>
#define N 26
#define M 184756
#define K 20
#define mod 10007
using namespace std;
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
void Adder(int &x,int d)
{
x+=d;
if (x>=mod) x-=mod;
return;
}
int T,n,m,length,l[N+1][N+1],r[N+1][N+1],dp[N+1][M+1],DP[N+1][M+1],tong[M+1],num[1<<K];
char c[N+1][N+1];
int main()
{
int res;
bool op;
T=read();
for (int i=1;i<=T;++i)
{
n=read(),m=read(),length=0;
for (int j=0;j<(1<<(n+m));++j)
if (__builtin_popcount(j)==m)
tong[++length]=j,num[j]=length;
for (int j=0;j<=26;++j)
{
for (int k=0;k<=n+m;++k) l[j][k]=0,r[j][k]=k;
for (int k=1;k<=length;++k) dp[j][k]=0;
}
for (int j=1;j<=n;++j)
for (int k=1;k<=m;++k)
{
cin>>c[j][k];
if (c[j][k]!='.')
{
r[c[j][k]-'a'][n-j+k]=min(r[c[j][k]-'a'][n-j+k],k-1);
if (c[j][k]!='z') l[c[j][k]-'a'+1][n-j+k]=max(l[c[j][k]-'a'+1][n-j+k],k);
}
}
dp[0][length]=1;
for (int j=1;j<=26;++j)
{
for (int k=1;k<=n+m;++k)
for (int t=1;t<=length;++t)
DP[k][t]=0;
for (int k=1;k<=length;++k) DP[1][k]=dp[j-1][k];
for (int k=2;k<=n+m;++k)
for (int t=1;t<=length;++t)
{
Adder(DP[k][t],DP[k-1][t]);
if (tong[t]&(1<<(k-1)))
{
for (int w=k-1;w>=1;--w)
{
if (!(tong[t]&(1<<(w-1)))) Adder(DP[k][num[tong[t]^(1<<(k-1))^(1<<(w-1))]],DP[k-1][t]);
else break;
}
}
}
for (int k=1;k<=length;++k)
{
res=0,op=1;
for (int t=1;t<=n+m;++t)
{
res+=((tong[k]>>(t-1))&1);
if (res<l[j][t]||res>r[j][t]) op=0;
}
if (op) dp[j][k]=DP[n+m][k];
}
}
printf("Case #%d: %d\n",i,dp[26][1]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 1ms
memory: 30228kb
input:
40 4 4 .... .... .... .... 4 4 abcd bcde cdef defg 4 4 bbcc ccde cdef deef 4 4 .... .... .r.. ..e. 3 4 .... .... ...g 1 3 f.. 2 2 .b bb 2 3 ... ..f 4 4 .... .... ..t. ..d. 3 3 .gn ... ... 4 4 ...a ...b ...c ...d 4 4 ...b ..bb .bbb bbbb 4 4 a... .b.. ..c. ...d 3 3 c.. glw ..w 1 4 .... 1 3 d.v 4 2 .. ...
output:
Case #1: 7285 Case #2: 1 Case #3: 1 Case #4: 0 Case #5: 718 Case #6: 231 Case #7: 2 Case #8: 686 Case #9: 0 Case #10: 3500 Case #11: 330 Case #12: 14 Case #13: 4096 Case #14: 2756 Case #15: 3737 Case #16: 19 Case #17: 8175 Case #18: 3737 Case #19: 7279 Case #20: 256 Case #21: 23 Case #22: 7569 Case ...
result:
ok 40 lines
Subtask #2:
score: 20
Accepted
Test #2:
score: 20
Accepted
time: 5647ms
memory: 45320kb
input:
40 5 10 .......... .......... .......... .......... .......... 10 10 ....a..... ....c..... ....g..... ....j..... ....l..... ....m..... ....n..... ....q..... ....t..... ....v..... 10 10 .......... .......... .......... .......... .......... .......... .......... .......... ...ok..... .......... 9 10 ...
output:
Case #1: 8791 Case #2: 7220 Case #3: 0 Case #4: 2510 Case #5: 4299 Case #6: 1663 Case #7: 981 Case #8: 1430 Case #9: 8814 Case #10: 2877 Case #11: 2124 Case #12: 0 Case #13: 4311 Case #14: 4458 Case #15: 8699 Case #16: 3727 Case #17: 14 Case #18: 5478 Case #19: 7922 Case #20: 6789 Case #21: 1252 Cas...
result:
ok 40 lines
Extra Test:
score: 0
Extra Test Passed