QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#61341 | #4411. Equipment Upgrade | Appleblue17 | AC ✓ | 1941ms | 15224kb | C++14 | 2.9kb | 2022-11-12 14:16:17 | 2022-11-12 14:16:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=6e5+5,mod=998244353,inv2=499122177;
int inv[N];
int ksm(int f,int x){
int tot=1;
while(x){
if(x & 1ll) tot=1ll*tot*f%mod;
f=1ll*f*f%mod;
x>>=1ll;
}
return tot;
}
int pr[N];
void init(int lim){
for(int l=1;l<N;l<<=1ll)
pr[l]=ksm(3,(mod-1)/(l*2));
}
int qmod(int x){
return x+(x>>31 & mod);
}
namespace NTT{
int A[N],B[N],S[N],rev[N];
int init(int n,int m){
memset(rev,0,sizeof(rev));
int lim=0;
while((1ll<<lim)<=n+m) lim++;
for(int i=0;i<=(1<<lim)-1;i++)
rev[i]=(rev[i>>1ll]>>1ll) | ((i & 1ll)<<(lim-1));
return lim;
}
void ntt(int *f,int n,int opt){
for(int i=0;i<=n-1;i++)
if(i<rev[i]) swap(f[i],f[rev[i]]);
for(int l=1;l<n;l<<=1ll){
int tmp=pr[l];
if(opt==-1) tmp=ksm(tmp,mod-2);
for(int i=0;i<=n-1;i+=l<<1ll){
int omegf=1;
for(int j=0;j<l;j++){
int x=f[i+j],y=1ll*omegf*f[i+j+l]%mod;
f[i+j]=qmod(x+y-mod),f[i+j+l]=qmod(x-y);
omegf=1ll*omegf*tmp%mod;
}
}
}
if(opt==-1){
int t=ksm(n,mod-2);
for(int i=0;i<=n-1;i++)
f[i]=1ll*f[i]*t%mod;
}
}
void solve(int *s,int* f,int* g,int n,int m){
if(n+m<=100){
for(int i=0;i<=n+m;i++) S[i]=0;
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
S[i+j]=qmod(S[i+j]+1ll*f[i]*g[j]%mod-mod);
}
}
for(int i=0;i<=n+m;i++) s[i]=S[i];
return ;
}
int lim=init(0,m);
for(int i=0;i<=n;i++) A[i]=f[i];
for(int i=0;i<=m;i++) B[i]=g[i];
for(int i=n+1;i<=(1ll<<lim);i++) A[i]=0;
for(int i=m+1;i<=(1ll<<lim);i++) B[i]=0;
ntt(A,(1<<lim),1);
ntt(B,(1<<lim),1);
for(int i=0;i<(1<<lim);i++) S[i]=1ll*A[i]*B[i]%mod;
ntt(S,(1<<lim),-1);
for(int i=0;i<=n+m;i++) s[i]=S[i];
}
}
int T,n;
int p[N],c[N],w[N],s[N];
int f[N],ans[N],sum;
int A[N],B[N];
void Devide_solve(int l,int r){
if(l==r){
int i=l;
ans[i]=1ll*(c[i]+1ll*sum*(1+mod-p[i])%mod+mod-1ll*f[i-1]*(1+mod-p[i])%mod*ksm(s[i],mod-2)%mod)%mod*ksm(p[i],mod-2)%mod;
sum=(sum+ans[i])%mod;
// cout<<"ANS "<<i<<": "<<ans[i]<<'\n';
return ;
}
int mid=(l+r)>>1ll;
Devide_solve(l,mid);
int lim=r-l+1,len=lim>>1ll;
for(int i=0;i<len;i++) A[i]=ans[l+i];
for(int i=1;i<=lim;i++) B[i]=s[i];
NTT::solve(A,A,B,len-1,lim-1);
for(int i=len+1;i<=lim;i++) f[i-len+mid]=(f[i-len+mid]+A[i-1])%mod;
Devide_solve(mid+1,r);
}
void Devide(int n){
int lim=NTT::init(n,0);
int m=1ll<<lim;
for(int i=0;i<m;i++) f[i]=ans[i]=0;
sum=0;
Devide_solve(0,m-1);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
init(N-5);
cin>>T;
while(T--){
cin>>n;
for(int i=0;i<n;i++) cin>>p[i]>>c[i],p[i]=1ll*p[i]*ksm(100,mod-2)%mod;
for(int i=1;i<n;i++) cin>>w[i],s[i]=(s[i-1]+w[i])%mod;
Devide(n);
// for(int i=0;i<=n;i++) cout<<ans[i]<<" ";
int anss=0;
for(int i=0;i<n;i++) anss=(anss+ans[i])%mod;
cout<<anss<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1941ms
memory: 15224kb
input:
208 2 100 41 28 64 28 3 100 48 91 13 73 3 78 92 4 100 22 15 85 26 50 41 15 90 85 77 5 100 39 51 97 83 41 4 86 36 70 49 24 17 33 6 100 53 53 45 92 2 36 40 61 61 76 52 18 37 75 49 96 7 100 5 21 47 39 58 78 1 82 93 59 82 56 90 1 41 76 64 84 27 8 100 14 38 77 66 20 1 63 47 47 3 12 87 16 99 62 14 81 75 2...
output:
375 243619761 141260443 516768753 850749960 897481401 602765935 510391586 689398435 784190268 697129546 505176530 687991734 16121189 684750916 616413796 324645467 60836964 997265902 829124402 135215114 115586183 566051860 45973142 577302112 438599189 808712026 903587073 180745041 931933480 429669755...
result:
ok 208 lines