QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#458297 | #8836. Highway Hoax | ucup-team1525# | WA | 240ms | 29420kb | C++20 | 4.2kb | 2024-06-29 16:35:31 | 2024-06-29 16:35:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1<<18;
const int mod=998244353;
using arr=int[N+5];
int add(int x,int y){ return (x+=y)>=mod?x-mod:x; }
int sub(int x,int y){ return (x-=y)<0?x+mod:x; }
int ksm(ll x,int tp,int s=1){
for(;tp;x=x*x%mod,tp>>=1) if(tp&1) s=x*s%mod;
return s;
}
namespace poly{
int L,iv;
arr w,rev;
arr pa,pb;
#define szf sizeof(int)
void cl(arr a){ memset(a,0,L*szf); }
void r_prep(){
int L=N>>1;
int val=ksm(3,(mod-1)/N);
w[L]=1;
for(int i=1;i<L;i++) w[i+L]=1ll*w[i+L-1]*val%mod;
for(int i=L-1;i;i--) w[i]=w[i<<1];
}
void pre_n(int n){
L=1; while(L<n) L<<=1;
iv=mod-(mod-1)/L;
for(int i=1;i<L;i++) rev[i]=(rev[i>>1]>>1)|((i&1)?(L>>1):0);
}
void FFT(arr t,bool ok=1){
int x,y;
for(int i=1;i<L;i++)
if(i<rev[i]) swap(t[i],t[rev[i]]);
for(int i=1;i<L;i<<=1)
for(int j=0;j<L;j+=i<<1)
for(int l=0;l<i;l++){
x=t[j+l]; y=1ll*t[i+j+l]*w[i+l]%mod;
t[i+j+l]=sub(x,y); t[j+l]=add(x,y);
}
if(!ok){
reverse(t+1,t+L);
for(int i=0;i<L;i++) t[i]=1ll*t[i]*iv%mod;
}
}
void NTT(arr a){ FFT(a); }
void INTT(arr a){ FFT(a,0); }
void NTT(arr a,arr b){ memcpy(b,a,L*szf); FFT(b); }
void INTT(arr a,arr b){ memcpy(b,a,L*szf); FFT(b); }
void Mult(arr a,arr b,arr c,int n){
pre_n(n);
NTT(a,pa); NTT(b,pb);
for(int i=0;i<L;i++) c[i]=1ll*pa[i]*pb[i]%mod;
INTT(c);
fill(c+n,c+L,0);
cl(pa); cl(pb);
}
}
void solve(int l,int r,arr a,arr b,arr f){
if(l==r){
f[0]=a[l]; f[1]=b[l];
return;
}
int mid=l+r>>1,l1=mid-l+2,l2=r-mid+1;
solve(l,mid,a,b,f); solve(mid+1,r,a,b,f+l1);
static arr ta,tb,tc;
int L=l1+l2-1;
for(int i=0;i<l1;i++) ta[i]=f[i];
for(int i=0;i<l2;i++) tb[i]=f[l1+i];
poly::Mult(ta,tb,tc,L);
for(int i=0;i<L;i++) f[i]=tc[i];
f[L]=0;
}
int n;
char s[N+5];
struct E{
int v,d;
E(int v=0,int d=0):v(v),d(d){}
};
vector<E> e[N+5];
mt19937 rnd(time(0));
int f[N+5][3];
arr g,h;
void dfs(int u,int tf){
for(auto it:e[u]){
if(it.v!=tf) dfs(it.v,u);
}
static arr a,b,g,h;
int tot=0;
for(auto it:e[u]){
int v=it.v;
if(v==tf) continue;
if(it.d==1){
tot++;
a[tot]=f[v][0];
b[tot]=f[v][1];
}
}
if(tot>0)
solve(1,tot,a,b,g);
else
g[0]=1;
tot=0;
for(auto it:e[u]){
int v=it.v;
if(v==tf) continue;
if(it.d==0){
tot++;
a[tot]=f[v][0];
b[tot]=f[v][2];
}
}
if(tot>0)
solve(1,tot,a,b,h);
else
h[0]=1;
static int val[10];
memset(val,0,sizeof val);
int d=e[u].size();
for(int i=0;i<=d;i++)
for(int j=max(i-2,0);j<=min(i+2,d);j++)
val[5+j-i]=(val[5+j-i]+1ll*h[j]*g[i])%mod;
if(s[u]=='S'){
// 'F'
f[u][0]=add(f[u][0],val[5-1]);
f[u][1]=add(f[u][1],val[5-2]);
f[u][2]=add(f[u][2],val[5]);
// 'S'
f[u][0]=add(f[u][0],val[5]);
f[u][1]=add(f[u][1],val[5-1]);
f[u][2]=add(f[u][2],val[5+1]);
}
else{
// 'F'
f[u][0]=add(f[u][0],val[5]);
f[u][1]=add(f[u][1],val[5-1]);
f[u][2]=add(f[u][2],val[5+1]);
// 'S'
f[u][1]=add(f[u][1],val[5]);
f[u][0]=add(f[u][0],val[5+1]);
f[u][2]=add(f[u][2],val[5+2]);
}
fill(g,g+d+1,0);
fill(h,h+d+1,0);
// printf("u %d: %d %d %d\n",u,f[u][0],f[u][1],f[u][2]);
}
int main(){
poly::r_prep();
scanf("%d",&n);
scanf("%s",s+1);
for(int i=1;i<n;i++){
int u,v;
scanf("%d %d",&u,&v);
e[u].push_back(E(v,1));
e[v].push_back(E(u,0));
}
for(int i=1;i<=n;i++){
shuffle(e[i].begin(),e[i].end(),rnd);
}
dfs(1,0);
printf("%d\n",f[1][0]);
// printf("%d\n",mx);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 16132kb
input:
5 SFSFS 2 1 2 3 4 3 4 5
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 18340kb
input:
4 SFFF 1 2 1 3 1 4
output:
4
result:
ok 1 number(s): "4"
Test #3:
score: 0
Accepted
time: 3ms
memory: 16164kb
input:
7 SSSSFFF 1 2 3 2 4 3 4 5 4 6 2 7
output:
13
result:
ok 1 number(s): "13"
Test #4:
score: 0
Accepted
time: 0ms
memory: 14284kb
input:
2 FS 1 2
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 16332kb
input:
3 FFF 3 1 3 2
output:
1
result:
ok 1 number(s): "1"
Test #6:
score: 0
Accepted
time: 3ms
memory: 14240kb
input:
4 FFFF 1 2 4 2 2 3
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 0ms
memory: 16156kb
input:
5 FFFFF 4 2 2 1 3 1 3 5
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: 0
Accepted
time: 3ms
memory: 18312kb
input:
6 FSSSSF 5 3 2 5 1 2 2 6 4 2
output:
3
result:
ok 1 number(s): "3"
Test #9:
score: -100
Wrong Answer
time: 240ms
memory: 29420kb
input:
199995 FSSFFFSSSFSFFSSSFSSSSSFSFSSSFSFFSSFSFSFFFSSSFSSFSFFSFFFFSSFFSSSFFSFSFFFFFFFFFFFFFSSSFSSFFFSSSFSFSSSFFFSFFSFSSSSFSFSFSFSSFFSFSFFSFFSSFSSSFSFFSSSFFSFFFSFFSFSFSFSFSSSFSSSSFFSSFFFFFSFSSSFFSSSSSSSSFFFSFSFFSSSFSFFFFFSFFFFSSSSSSSFFFSFFSFSSSFSFSFFFFSFSSFSSFFFSSFFFSSFSFFFSSSFSFSSFFSFSFFSFFSSFSFSSSSFFF...
output:
226304474
result:
wrong answer 1st numbers differ - expected: '233157276', found: '226304474'