QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#620581 | #4514. Schrödinger and Pavlov | wangchunjie | 50 ✓ | 574ms | 4104kb | C++14 | 1.6kb | 2024-10-07 19:26:48 | 2024-10-07 19:26:49 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e3+5,P=1e9+7;
int n;
int fa[N],p[N];
char s[N];
int Fa[N][2];
int getfa(int x,int y)
{
while(x!=Fa[x][y])
x=Fa[x][y]=Fa[Fa[x][y]][y];
return x;
}
int get(int x)
{
return (1-x+P)%P;
}
void solve()
{
scanf("%lld%s",&n,s+1);
for(int i=1;i<=n;i++)
scanf("%lld",&fa[i]);
for(int i=1;i<=n;i++)
Fa[i][0]=Fa[i][1]=i;
for(int i=1;i<=n;i++){
int x=getfa(i,0),y=getfa(fa[i],0);
if(x!=y)
Fa[y][0]=x;
}
int Ring;
for(int i=n;i>=1;i--){
int x=getfa(i,0),y=getfa(n,0);
if(x==y){
x=getfa(i,1),y=getfa(fa[i],1);
if(x!=y)
Fa[y][1]=x;
else
Ring=i;
}
}
int ans=0;
for(int l=0;l<=1;l++){
for(int r=0;r<=1;r++){
for(int i=1;i<=n;i++){
if(s[i]=='C')
p[i]=1;
else if(s[i]=='.')
p[i]=0;
else
p[i]=(1+P)/2;
}
int ring;
for(int i=1;i<=n;i++){
if(i==Ring){
ring=(l?p[i]:get(p[i]))*(r?p[fa[i]]:get(p[fa[i]]))%P;
p[i]=l,p[fa[i]]=r;
}
int x=p[i],y=p[fa[i]];
p[i]=x*y%P;
p[fa[i]]=(x+y*get(x)%P)%P;
}
(ans+=ring*p[n]%P)%=P;
}
}
ans=(ans+P)%P;
for(int i=1;i<=n;i++)
if(s[i]=='?')
ans=ans*2%P;
printf("%lld\n",(ans+P)%P);
}
// #define IO IO
signed main()
{
#ifdef IO
freopen("experiment.in", "r", stdin);
freopen("experiment.out", "w", stdout);
#endif
int T;
scanf("%lld",&T);
for(int i=1;i<=T;i++){
cout<<"Case #"<<i<<": ";
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 8
Accepted
time: 12ms
memory: 3900kb
input:
1234 92 ???????????????????????????????????????????????????????????????????????????????????????????? 2 1 2 3 4 7 5 7 8 9 15 9 15 12 14 15 16 19 16 17 17 20 22 23 24 25 26 27 28 29 30 31 32 33 34 38 34 39 35 39 45 40 42 43 44 45 46 47 48 49 50 51 52 57 53 55 56 56 57 59 60 61 62 63 64 64 66 67 68 69 ...
output:
Case #1: 75340207 Case #2: 248320570 Case #3: 1 Case #4: 501005577 Case #5: 0 Case #6: 0 Case #7: 0 Case #8: 988185614 Case #9: 0 Case #10: 840220993 Case #11: 140130951 Case #12: 8 Case #13: 0 Case #14: 0 Case #15: 570065479 Case #16: 128 Case #17: 67108864 Case #18: 0 Case #19: 0 Case #20: 5605238...
result:
ok 1234 lines
Test #2:
score: 42
Accepted
time: 574ms
memory: 4104kb
input:
1234 4693 ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...
output:
Case #1: 13595505 Case #2: 0 Case #3: 370024028 Case #4: 332957307 Case #5: 0 Case #6: 401898087 Case #7: 588719939 Case #8: 0 Case #9: 0 Case #10: 0 Case #11: 893344250 Case #12: 816205426 Case #13: 902710840 Case #14: 735342227 Case #15: 0 Case #16: 623834768 Case #17: 195615577 Case #18: 44692699...
result:
ok 1234 lines