QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#108064#6513. Expression 3tricyzhkxTL 3112ms90488kbC++143.1kb2023-05-23 15:32:112023-05-23 15:32:15

Judging History

你现在查看的是最新测评结果

  • [2024-02-14 13:23:19]
  • hack成功,自动添加数据
  • (/hack/531)
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-23 15:32:15]
  • 评测
  • 测评结果:TL
  • 用时:3112ms
  • 内存:90488kb
  • [2023-05-23 15:32:11]
  • 提交

answer

# include <bits/stdc++.h>
using namespace std;
const int mod=998244353,g=3;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> Poly;
int ans,a[200010],rev[600010],w[600010],maxn;
ll inv[200010];
char s[200010];
Poly P1[600010],P2[600010],P3[600010];
template<typename T>
void upd(int &a,T b){a=(a+b)%mod;}
ll power(ll a,int b)
{
	ll ans=1;
	for(;b;b>>=1,a=a*a%mod)
		if(b&1) ans=ans*a%mod;
	return ans;
}
void init(int len)
{
	int l=0;maxn=1;
	while(maxn<=len) maxn<<=1,l++;
	for(int i=0;i<maxn;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(l-1));
	for(int i=1;i<maxn;i<<=1)
	{
		ll wm=power(g,(mod-1)/(i<<1));w[i]=1;
		for(int j=1;j<i;j++) w[i+j]=w[i+j-1]*wm%mod;
	}
}
void NTT(Poly &p,bool flag)
{
	static ull a[600010];
	p.resize(maxn);
	for(int i=0;i<maxn;i++) a[i]=p[rev[i]];
	for(int i=1;i<maxn;i<<=1)
		for(int j=0;j<maxn;j+=i<<1)
			for(int k=j;k<j+i;k++)
			{
				ull x=a[k],y=a[k+i]*w[i+k-j]%mod;
				a[k]=x+y;a[k+i]=x+mod-y;
			}
	if(flag) for(int i=0;i<maxn;i++) p[i]=a[i]%mod;
	else
	{
		reverse(a+1,a+maxn);
		int inv=power(maxn,mod-2);
		for(int i=0;i<maxn;i++) p[i]=a[i]%mod*inv%mod;
	}
}
Poly operator-(const Poly &a,const Poly &b)
{
	int n=a.size(),m=b.size();
	Poly ans(max(n,m));
	for(int i=0;i<n;i++) ans[i]=a[i];
	for(int i=0;i<m;i++) upd(ans[i],mod-b[i]);
	return ans;
}
Poly mul(Poly a,Poly b,int sz=-1)
{
	int n=a.size(),m=b.size();
	if(sz<0) sz=n+m-1;
	init(n+m);
	NTT(a,1);NTT(b,1);
	for(int i=0;i<maxn;i++) a[i]=(ll)a[i]*b[i]%mod;
	NTT(a,0);a.resize(sz);
	return a;
}
Poly INV(const Poly &a,int n)
{
	assert(n<=(int)a.size());
	if(n==1) return {(int)power(a[0],mod-2)};
	Poly b=INV(a,(n+1)/2),c(a.begin(),a.begin()+n),d=b;
	init(n);
	NTT(b,1);NTT(c,1);
	for(int i=0;i<maxn;i++) c[i]=(ll)b[i]*c[i]%mod;
	NTT(c,0);c.resize(n);fill(c.begin(),c.begin()+(n+1)/2,0);
	NTT(c,1);
	for(int i=0;i<maxn;i++) b[i]=(ll)b[i]*(mod-c[i])%mod;
	NTT(b,0);b.resize(n);move(d.begin(),d.end(),b.begin());
	return b;
}
Poly MOD(Poly a,Poly b)
{
	int n=a.size(),m=b.size();
	if(n<m) return a.resize(m-1),a;
	Poly ra(a.rbegin(),a.rend()),rb(b.rbegin(),b.rend());
	rb.resize(n-m+1);
	Poly Q=mul(ra,INV(rb,n-m+1),n-m+1);
	reverse(Q.begin(),Q.end());
	Poly R=a-mul(b,Q);R.resize(m-1);
	return R;
}
void build(int rt,int l,int r)
{
	if(l==r)
	{
		if(s[l]=='-') P1[rt]={mod-l-2,1},P2[rt]={mod-l,1};
		else P1[rt]=P2[rt]={1};
		P3[rt]={mod-l,1};
		return;
	}
	int mid=(l+r)/2;
	build(rt*2,l,mid);build(rt*2+1,mid+1,r);
	P1[rt]=mul(P1[rt*2],P1[rt*2+1]);
	P2[rt]=mul(P2[rt*2],P2[rt*2+1]);
	P3[rt]=mul(P3[rt*2],P3[rt*2+1]);
}
void solve(int rt,int l,int r,Poly F1,Poly F2)
{
	if(l==r) return upd(ans,F1[0]*power(F2[0],mod-2)%mod*a[l]);
	int mid=(l+r)/2;
	solve(rt*2,l,mid,MOD(F1,P3[rt*2]),MOD(F2,P3[rt*2]));
	solve(rt*2+1,mid+1,r,MOD(mul(F1,P1[rt*2]),P3[rt*2+1]),MOD(mul(F2,P2[rt*2]),P3[rt*2+1]));
}
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	scanf("%s",s+1);
	build(1,1,n);solve(1,1,n,{1},{1});
	for(int i=1;i<n;i++) ans=(ll)ans*i%mod;
	cout<<ans<<endl;
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 11ms
memory: 45648kb

input:

4
9 1 4 1
-+-

output:

46

result:

ok 1 number(s): "46"

Test #2:

score: 0
Accepted
time: 3ms
memory: 45592kb

input:

5
1 2 3 4 5
+-+-

output:

998244313

result:

ok 1 number(s): "998244313"

Test #3:

score: 0
Accepted
time: 3112ms
memory: 90488kb

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...

output:

178167352

result:

ok 1 number(s): "178167352"

Test #4:

score: -100
Time Limit Exceeded

input:

200000
109044620 745578941 396599814 756923982 940933214 875346257 378089839 792684563 491924893 782192923 208569108 421583135 814903710 690275542 15773609 364566266 12890134 661702679 640270667 615999192 13352194 325560419 385152885 265008089 570536451 282429805 331946208 255056541 813809151 150995...

output:


result: