QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#107445 | #6513. Expression 3 | psc233 | TL | 463ms | 49876kb | C++14 | 2.4kb | 2023-05-21 15:11:37 | 2023-05-21 15:11:38 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define ll long long
#define mk(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define cs const
using namespace std;
cs int g=3;
const int N=6e5+10;
const int mod=998244353;
const int lim=1e6;
typedef vector<int> poly;
int rev[N],inv[N],w[N];
unordered_map<int,int>mp;
char s[N];
int n,m,len,d,x,k;
int a[N];
int mul(int x,int y){return (ll)x*y%mod;}
int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
int dec(int x,int y){return x-y>=0?x-y:x-y+mod;}
int ksm(int x,int y){
ll ans=1;
for (;y;y>>=1,x=mul(x,x)) if (y&1) ans=mul(ans,x);
return ans;
}
int read(){
int f=1,x=0; char c=getchar();
while (c<'0'||c>'9'){if (c=='-')f=-1;c=getchar();}
while (c>='0'&&c<='9'){x=add(mul(x,10),c-'0');c=getchar();}
return x;
}
void init(int x){
len=1,d=0;
while (len<x) {len<<=1;d++;}
for (int i=0;i<len;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(d-1));
}
inline poly NTT(poly a,int t) {
for (int i=0;i<a.size();i++) if (i<rev[i]) swap(a[i],a[rev[i]]);
for (int i=1;i<a.size();i<<=1) {
int s=(i<<1);
int wn=ksm(g,(mod-1)/s);
if (t==-1) wn=ksm(wn,mod-2);
for (int j=0;j<a.size();j+=s) {
int w=1;
for (int k=j;k<j+i;k++) {
int x=a[k],y=mul(a[k+i],w);
a[k]=add(x,y); a[k+i]=dec(x,y);
w=mul(w,wn);
}
}
}
if (t==-1){
ll w=ksm(a.size(),mod-2);
for (int i=0;i<a.size();i++) a[i]=mul(a[i],w);
}
return a;
}
inline poly operator *(poly a,poly b){
int n=a.size(),m=b.size();
init(n+m-1);
a.resize(len);
b.resize(len);
a=NTT(a,1); b=NTT(b,1);
for (int i=0;i<a.size();i++) a[i]=mul(a[i],b[i]);
a=NTT(a,-1);
a.resize(n+m-1);
return a;
}
const int sq=ksm(ksm(g,lim),mod-2);
int get(int x){
if (x==0) return 0;
for (int i=0,t=1;;t=mul(t,sq),i++){
if (mp[mul(t,x)]){
return i*lim+mp[mul(t,x)]-1;
}
}
}
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=0,t=1;i<lim;i++,t=mul(t,g)){
mp[t]=i+1;
}
scanf("%s",s+1);
poly b,c;
for (int i=1;i<n;i++) if (s[i]=='-') b.pb(1); else b.pb(0);
for (int i=1;i<=n;i++) w[i]=get(i);
c.pb(dec(get(mod-1),w[1]));
for (int i=2;i<=n;i++) c.pb(dec(get((i-2+mod)%mod),w[i]));
b=b*c;
int ans=a[1];
bool p=0;
for (int i=2;i<=n;i++) if (s[i-2]!='-') {
ans=add(ans,mul(a[i],ksm(g,b[i-2])));
}
for (int i=1;i<n;i++) ans=mul(ans,i);
printf("%d\n",ans);
}
详细
Test #1:
score: 100
Accepted
time: 348ms
memory: 49752kb
input:
4 9 1 4 1 -+-
output:
46
result:
ok 1 number(s): "46"
Test #2:
score: 0
Accepted
time: 463ms
memory: 49876kb
input:
5 1 2 3 4 5 +-+-
output:
998244313
result:
ok 1 number(s): "998244313"
Test #3:
score: -100
Time Limit Exceeded
input:
100000 664815434 205025136 871445392 797947979 379688564 336946672 231295524 401655676 526374414 670533644 156882283 372427821 700299596 166140732 677498490 44858761 185182210 559696133 813911251 842364231 681916958 114039865 222372111 784286397 437994571 152137641 650875922 613727135 209302742 5321...