QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#432534 | #8164. EX 中缀表达式 | Williamxzh | 100 ✓ | 1ms | 5136kb | C++23 | 5.1kb | 2024-06-07 10:38:18 | 2024-06-07 10:38:19 |
Judging History
answer
#include <bits/stdc++.h>
#define il inline
#define pii pair<int,int>
#define fi first
#define se second
#define RET {printf("error");exit(0);}
using namespace std;
typedef long long ll;
il ll qp(ll a,ll b,ll mod){
ll ans=1ll;
while(b){
if(b&1) ans=(ans*a)%mod;
a=(a*a)%mod,b>>=1;
}return ans;
}
il ll getphi(ll x){
ll ret=x,y,z;
for(ll i=2;i*i<=x;++i){
if(x%i) continue;
while(x%i==0) x/=i;
ret=(ret*(i-1ll))/i;
}
if(x>1) ret=(ret*(x-1ll))/x;
return ret;
}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
il bool isch(char c){return (c>='0' && c<='9') || c=='.' || c=='+' || c=='*' || c=='^' || c=='(' || c==')';}
il bool isop(char c){return c=='.' || c=='+' || c=='*' || c=='^';}
il void cmin(int &x,int y){x=x<y?x:y;}
il void cmax(int &x,int y){x=x>y?x:y;}
const int N=10005,M=15;const ll mod=998244353;
int n,m,k,to[N];char s[N],op[M];ll mo[N];
il void init(){
mo[0]=mod;
for(int i=1;;++i){
mo[i]=getphi(mo[i-1]);
if(mo[i]==1){k=i;break;}
}
for(int i=k+1;i<N;++i) mo[i]=1;
}
struct OP{
int l,r,c;
il OP(){l=r=c=0;}
il OP(int l,int r) : l(l),r(r) {}
};
struct node{
int l,r,p,ls,rs;ll v,w;char op;//t=0:a number, t=1: otherwise p:which mo[p] this number shall mod c:type in [1,m]
//v:value mod mo[p] ,w:if(value<=mod) w=value;else w=-1
il node(){l=r=ls=rs=p=0,v=w=0ll;}
il node(int l,int r) : l(l),r(r) {}
}a[N<<1];int tot,rt;
il int dfs(int l,int r){
if(l>r) return -1;
if(s[l]=='(' && s[r]==')' && to[l]==r) return dfs(l+1,r-1);
int len,x,y,z,p,q,pre;ll u,v,w;
vector<OP> f;f.clear();
x=l,pre=-1;
for(int i=l;i<=r;++i){
if(s[i]=='('){
if(pre==0) return -1;
i=to[i],pre=0;continue;
}
if(!isdigit(s[i])) return -1;
x=i;
while(x<r && isdigit(s[x+1])) ++x;
if(x>=r || !isop(s[x+1])) return -1;
if(s[x+1]=='.') p=0;
else if(isop(s[x+1])) p=1,f.push_back({i,x+1});
else return -1;
i=x+1;if(pre==p) return -1;
pre=p;
}
for(int i=0;i<f.size();++i){
if(f[i].r!=f[i].l+1 && s[f[i].l]!='0') return -1;
f[i].c=s[f[i].r-1]-'0';if(!f[i].c) return -1;
}
int mi=n+1;for(int i=0;i<f.size();++i) mi=min(mi,f[i].c);
if(op[mi]=='0'){p=-1;for(int i=0;i<f.size();++i)if(f[i].c==mi && i>p) p=i;}
else{p=-1;for(int i=0;i<f.size();++i)if(f[i].c==mi){p=i;break;}}
a[++tot]=node(l,r);int ID=tot;if(p!=-1) a[ID].op=s[f[p].r];else a[ID].op='a';
a[ID].ls=a[ID].rs=0;
if(p!=-1) a[ID].ls=dfs(l,f[p].l-1),a[ID].rs=dfs(f[p].r+1,r);
if(a[ID].ls==-1 || a[ID].rs==-1) return -1;
//printf("%d %d %d\n",ID,a[ID].ls,a[ID].rs);
//if(p!=-1) printf("%d %d %d %d\n",l,r,f[p].l,f[p].r);
return ID;
}
void solve(int x,int cur){
a[x].p=cur;//printf("%d %d %d %d\n",x,cur,a[x].ls,a[x].rs);
if(!a[x].ls && !a[x].rs){
ll u=0ll,v=0ll;
for(int i=a[x].l;i<a[x].r;++i) u=(u*10ll+s[i]-'0')%mo[cur];
for(int i=a[x].l;i<a[x].r;++i){v=(v*10ll+s[i]-'0');if(v>mod) {v=-1;break;}}
a[x].v=u,a[x].w=v;
//printf("*** %d %d %d %d %lld %lld\n",x,a[x].l,a[x].r,cur,a[x].v,a[x].w);
return ;
}
int y=a[x].ls,z=a[x].rs;
if(a[x].op=='+'){
solve(y,cur),solve(z,cur),a[x].v=(a[y].v+a[z].v)%mo[cur];
if(a[y].w==-1 || a[z].w==-1) a[x].w=-1;
else{
a[x].w=a[y].w+a[z].w;
if(a[x].w>mod) a[x].w=-1;
}
//printf("*** %d %d %d %d %lld %lld\n",x,a[x].l,a[x].r,cur,a[x].v,a[x].w);
return ;
}
if(a[x].op=='*'){
solve(y,cur),solve(z,cur),a[x].v=(a[y].v*a[z].v)%mo[cur];
if(a[y].w==-1 || a[z].w==-1) a[x].w=-1;
else{
a[x].w=a[y].w*a[z].w;
if(a[x].w>mod) a[x].w=-1;
}
//printf("*** %d %d %d %d %lld %lld\n",x,a[x].l,a[x].r,cur,a[x].v,a[x].w);
return ;
}
solve(y,cur),solve(z,cur+1);
if(a[z].w==-1){
a[x].v=qp(a[y].v,a[z].v+mo[cur+1],mo[cur]);
if(a[y].w!=-1 && (a[y].w>=0 && a[y].w<=1)) a[x].w=a[y].w;
else a[x].w=-1;
//printf("*** %d %d %d %d %lld %lld\n",x,a[x].l,a[x].r,cur,a[x].v,a[x].w);
return ;
}
a[x].v=qp(a[y].v,a[z].w,mo[cur]);
if(a[y].w==-1){a[x].w=-1;return ;}
if(a[y].w>=0 && a[y].w<=1){
if(!a[z].w) a[x].w=1;
else a[x].w=a[y].w;
return ;
}
a[x].w=1ll;
for(ll i=1;i<=a[z].w;++i){
a[x].w*=a[y].w;
if(a[x].w>mod){a[x].w=-1;break;}
}
//printf("*** %d %d %d %d %lld %lld\n",x,a[x].l,a[x].r,cur,a[x].v,a[x].w);
}
ll ans;int st[N],top;
int main(){
scanf("%d%s%s",&m,op+1,s+1);init();n=strlen(s+1);
for(int i=1;i<=n;++i){
if(s[i]=='(') st[++top]=i;
else if(s[i]==')'){
if(!top) RET
to[i]=st[top],to[st[top]]=i,--top;
}
}
rt=dfs(1,n);
if(rt==-1) RET
solve(rt,0);
printf("%lld",a[rt].v);
return 0;
}
/*
2
01
1.2+2.1^3.2*4.2^(5.2*6.)2+7.
2
01
1.2+2.1^3.2*4.2^(5.2*6.)2+7.
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 4
Accepted
time: 1ms
memory: 4716kb
input:
1 1 001.0+001.
output:
error
result:
ok single line: 'error'
Test #2:
score: 4
Accepted
time: 1ms
memory: 4800kb
input:
1 1 002.001+003.001*004.001+005.
output:
29
result:
ok single line: '29'
Test #3:
score: 4
Accepted
time: 1ms
memory: 4724kb
input:
1 1 (1.)()
output:
error
result:
ok single line: 'error'
Test #4:
score: 4
Accepted
time: 0ms
memory: 4724kb
input:
1 1 ((((002.001+003.)001*004.001+(((005.))))))
output:
45
result:
ok single line: '45'
Test #5:
score: 4
Accepted
time: 1ms
memory: 4724kb
input:
1 1 1
output:
error
result:
ok single line: 'error'
Test #6:
score: 4
Accepted
time: 1ms
memory: 4792kb
input:
1 0 000.001^000.001^001737356566464562245456526656565575.001^005985558448484954556451.
output:
1
result:
ok single line: '1'
Test #7:
score: 4
Accepted
time: 0ms
memory: 4724kb
input:
1 0 005656646456224545652665.001^00848495.001^00173736565575.001^005985558444556451.
output:
707709398
result:
ok single line: '707709398'
Test #8:
score: 4
Accepted
time: 1ms
memory: 4892kb
input:
1 1 001737356566464562245456526656565575.001^005985558448484954556451.001^000.001^000.
output:
64025176
result:
ok single line: '64025176'
Test #9:
score: 4
Accepted
time: 0ms
memory: 5088kb
input:
1 1 0017373575.001^005985558448484954556451.001^0054565266565.001^00656646456224655.
output:
563281323
result:
ok single line: '563281323'
Test #10:
score: 4
Accepted
time: 1ms
memory: 4820kb
input:
1 1 ((((1^))))
output:
error
result:
ok single line: 'error'
Test #11:
score: 4
Accepted
time: 1ms
memory: 4792kb
input:
1 1 ((((((((((((2.1^02.))01^3.01^02.))01^((03.01^03.))1^03.1^3.))01^((((02.1^3.))1^2.01^03.))01^((03.01^2.))1^02.1^03.))01^((((((3.01^02.))01^03.1^02.))1^((3.1^2.))1^2.1^03.))01^((((2.01^03.))01^2.01^2.))01^((02.1^2.))1^02.01^2.))1^((((((((2.1^2.))01^03.1^3.))1^((3.1^00.))1^2.1^2.))01^((((3.01^02.))...
output:
393739719
result:
ok single line: '393739719'
Test #12:
score: 4
Accepted
time: 1ms
memory: 4836kb
input:
1 1 ((((((((((((3.1^02.))1^03.1^03.))1^((1.1^3.))1^02.01^02.))01^((((2.01^1.))01^03.1^2.))1^((02.01^03.))01^03.1^02.))1^((((((02.1^03.))1^02.1^3.))01^((02.01^2.))01^2.1^02.))01^((((03.01^3.))1^3.1^3.))1^((3.1^02.))01^3.01^03.))01^((((((((01.1^2.))1^2.1^02.))01^((02.01^2.))01^3.1^3.))01^((((3.01^02.)...
output:
206923682
result:
ok single line: '206923682'
Test #13:
score: 4
Accepted
time: 1ms
memory: 5104kb
input:
1 0 02.1^02.1^((3.01^02.))01^((3.01^01.1^((3.01^02.))))1^((3.01^02.01^((3.01^3.))01^((03.1^02.1^((2.1^03.))))))1^((03.1^02.1^((03.01^00.))1^((01.01^3.1^((2.01^02.))))01^((1.01^03.1^((02.1^02.))01^((03.01^03.01^((0.01^2.))))))))1^((03.01^03.1^((3.1^02.))01^((2.01^2.1^((3.01^01.))))01^((2.1^02.1^((2.1...
output:
336183747
result:
ok single line: '336183747'
Test #14:
score: 4
Accepted
time: 1ms
memory: 4804kb
input:
1 1 ((((((((((((03.1^02.))01^03.1^3.))1^((3.1^03.))01^3.1^02.))1^((((2.1^02.))01^2.01^2.))1^((03.1^03.))1^2.01^0.))01^((((((02.01^2.))01^2.1^2.))01^((03.1^2.))01^3.1^02.))1^((((03.1^03.))01^02.1^3.))01^((02.1^03.))1^03.1^03.))1^((((((((03.1^02.))1^2.1^02.))01^((03.01^2.))01^03.1^03.))1^((((2.01^2.))...
output:
737081821
result:
ok single line: '737081821'
Test #15:
score: 4
Accepted
time: 1ms
memory: 4804kb
input:
1 0 1.1^2.1^((2.01^02.))1^((2.01^02.01^((2.1^02.))))01^((03.01^2.01^((3.01^02.))01^((02.1^03.1^((3.01^02.))))))01^((03.01^02.1^((01.1^03.))01^((2.01^3.01^((3.1^03.))))1^((03.01^2.1^((03.01^3.))1^((2.01^2.01^((03.1^2.))))))))01^((2.01^02.01^((03.01^03.))1^((3.1^3.1^((03.01^03.))))01^((3.01^3.01^((03....
output:
1
result:
ok single line: '1'
Test #16:
score: 4
Accepted
time: 1ms
memory: 5136kb
input:
9 100011111 ((((((((3.03*02.))9^3.09*03.4+((02.3+2.01*03.3+2.))2^((03.3^3.1+03.1+3.))8+((2.09*03.3+((02.03*3.))))))08*((((((3.04^03.))09*((02.2+03.))01+((3.4+03.))9*((3.7*03.))))6*((03.08^03.6*02.06^2.5*((01.05+2.03^03.09+02.))))))))08+((((03.5*2.02^03.06^2.01*2.09+03.6+((2.3^2.))))8^((((3.5^02.))6*...
output:
203844826
result:
ok single line: '203844826'
Test #17:
score: 4
Accepted
time: 1ms
memory: 5116kb
input:
9 010010100 ((((((((((02.06*2.03^((02.2+3.))))09^((((03.2^03.))03+02.05+03.))1+((02.08^00.3*2.4+03.1^((03.6^2.))9^((3.1+02.))))))9^((((((03.3+03.03*((03.1+01.))))7^((02.03^03.))09+((02.09+03.))))09*((((03.7^1.4+02.05+2.))9*((02.06*2.5*((03.03^3.))))))))03*((((((02.4+03.))7+((2.06^3.))))7^((01.01*02....
output:
473638651
result:
ok single line: '473638651'
Test #18:
score: 4
Accepted
time: 1ms
memory: 4836kb
input:
9 111101110 ((((((((((((2.2*03.))03*3.03+2.))08+((((02.07^02.))07+((3.04*02.))))07+((((2.1+3.))8+3.8+03.1^((03.01+03.))06*((03.3+02.))))1*((2.4+02.3+2.9+2.))08+((3.3*03.))9*((2.02^02.))7+((((02.02*3.))06^02.7^01.))07+((((2.01^02.))2+01.8+03.))))3*((((((3.5+2.))08^((02.6+03.))02^3.07^3.5^03.6^2.))3+(...
output:
1
result:
ok single line: '1'
Test #19:
score: 4
Accepted
time: 1ms
memory: 4828kb
input:
9 111011011 ((((((((3.08*2.07*((02.1*3.))07+((3.2*02.01^3.8^2.))))8^((((2.09*3.2^03.04+02.))4+((03.06*02.4+((00.01*02.))))))))8^((((3.06^2.))09^03.9^02.03^((0.4+03.02*2.05^00.))))9^((((03.1^02.))6+((02.01^3.))01+((02.03*02.))07+2.9+2.))01*((((02.6*02.4*02.8*2.))6*((03.2^02.))09^((3.7+2.))))08+((((03...
output:
788040634
result:
ok single line: '788040634'
Test #20:
score: 4
Accepted
time: 0ms
memory: 5112kb
input:
9 100110101 ((((((((3.5*3.))7^((2.3+3.))05+2.8^2.05^3.8*2.))7^03.08*02.8^((2.7+00.))08*((2.2^2.))09+((2.7*2.))06+((((((03.08*3.1*02.4+03.))1+02.03*2.2^3.05*02.))01*((2.7+02.01+02.02+02.))9*((03.02+03.1^0.03^3.))))05*((((((((02.01*02.))5*03.06+3.))5+((2.04*2.1*3.1+02.))02^((((03.7*03.01+2.01^3.))1+2....
output:
151078698
result:
ok single line: '151078698'
Test #21:
score: 4
Accepted
time: 1ms
memory: 4784kb
input:
9 010011011 ((((((((3.5^3.4+02.06+02.))09*((2.05^03.01*2.6*02.))03^((2.07+02.5*((03.04^0.))01+((02.4+03.))07+2.09^03.))))06+((((((02.1^2.))03^2.06^3.))7^((((2.01*02.))6*03.9*2.))04+((((3.05+2.01+3.7+2.))02+3.04*02.4^03.7+2.))))05+((((((02.07+3.))9+((3.08*2.))5*((2.8+02.02^((3.01*02.))))))08*((((02.0...
output:
926442773
result:
ok single line: '926442773'
Test #22:
score: 4
Accepted
time: 1ms
memory: 4820kb
input:
9 010111110 ((((((((2.09+2.7*((3.03^03.))02^((03.01*03.))3*03.6^03.))04+((((02.03*3.))06+((3.01^02.))))9*((((03.4*3.))09^((03.1+2.))))))06*((03.7+02.06^((2.5*03.))1+((00.09+3.1*2.9+2.))))7+((03.8^02.3^2.06^3.))08^((3.8^02.04+3.7*03.))04^((((2.07+3.5^((02.01^03.))))05*((03.01+03.))7^3.09^3.))09^((((2...
output:
482698727
result:
ok single line: '482698727'
Test #23:
score: 4
Accepted
time: 1ms
memory: 4852kb
input:
9 101011110 ((((((((3.9+01.09^((03.6^03.))05+((2.06^3.03+((00.1*03.))))))8^((02.6+02.02*03.6*02.02^((2.07+02.))7+((3.04+02.))))1*((((02.06+03.))8+((2.7+0.))01*((02.3*3.))9^((02.1^2.))))8^((((2.6^02.))8^((3.1+0.))7*((02.6*02.))8*((3.07+2.))))))05*((((03.4^2.03^((03.2*02.))))05*2.07+03.6+3.06+2.1^((2....
output:
665120433
result:
ok single line: '665120433'
Test #24:
score: 4
Accepted
time: 1ms
memory: 4916kb
input:
9 101010100 ((((((((((03.08+02.03+2.9^2.))9^((2.9+3.04+((02.2*02.))))2^((2.05+3.))6+3.7^2.3^2.7+03.3+3.9*3.))09*((((3.9*3.7*3.7*3.))9^((((03.04+03.))07^2.08*1.))3*03.8+02.04+((02.4+2.))04+((2.5+02.))6*((02.01+3.))))4*((((03.04+2.))08*((03.07^03.))03^2.07*3.05^((3.04*02.))2+((03.5^3.3*((03.2*3.))))03...
output:
487727059
result:
ok single line: '487727059'
Test #25:
score: 4
Accepted
time: 1ms
memory: 4768kb
input:
9 010000001 ((((((((2.05^03.4+((03.02^2.))))06^((((2.3+03.))06+((2.06*03.))))))8^((((((02.04^03.))07^((2.7+03.))))08+((3.06+03.))9^((2.02^3.))))3*((2.05^2.01^2.09^03.))07*((2.6*2.2+3.6+2.))04+((((02.05*2.02^02.02+02.))02^2.07^3.06+((3.6+3.))))01^((((((((2.01^02.))2^2.07^03.))8*((03.6*3.6^02.09^3.)))...
output:
529906102
result:
ok single line: '529906102'