God thought, for the first time saw this operation ... The
constructs a matrix to transfer state.
A+b∗c A+b*c, we note that the 3-tuple (s,m,t) (s,m,t) represents the current state, where S=a, m=b, t=b∗c s=a,\ the m=b,\.
Such records, each more than one character, the transfer can be represented as multiplying the previous matrix.
Number k K: (s,m,t) → (s,m,t∗10+m∗k) (s,m,t) \to (s,m,t*10+m*k)
+ +: (s,m,t) → (s+t,1,0) (s,m,t) \to (s+t,1,0)
− -: (s,m,t) → (s,t,0) (s,m,t) \to (s,t,0)
Matrix fast Power optimization.
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
typedef long Long LL;
const int maxn=100005,mod=998244353;
struct matrix{int n,m,a[5][5];
Matrix (int x=0) {memset (a,0,sizeof (a)); a[0][0]=a[1][1]=a[2][2]=a[3][3]=x;}
Matrix operator * (const matrix &B) const{matrix C; c.n=n; c.m=b.m; for (int i=0;i<=c.n-1;i++) for (int j=0;j<=c.m-1;j++) for (int k=0;k<=m-1;k++) (c.a[i)
[j]+= (LL) a[i][k]*b.a[k][j]%mod)%=mod;
return C;
} ans;
int n;
Matrix Getm (char ch) {matrix C; c.n=c.m=4; if (ch== ' + ') c.a[0][0]=c.a[2][0]=c.a[3][1]=c.a[3][3]=1; else if (ch== ' * ') c.a[0][0]=c.a[2][1]=c.a[3][3]=1;
Else C.a[0][0]=c.a[1][1]=c.a[3][3]=1, c.a[2][2]=10, c.a[1][2]=ch-' 0 ';
return C;
} Char st[20];
Matrix Pow (Matrix A,int b) {matrix res=1; res.n=res.m=4;
for (; b;b>>=1,a=a*a) if (b&1) res=res*a;
return res; int main () {freopen ("zroi10a.in", "R", sTdin);
Freopen ("Zroi10a.out", "w", stdout);
scanf ("%d", &n); ans=0; Ans.n=1; ans.m=4;
Ans.a[0][1]=ans.a[0][3]=1;
for (int i=1;i<=n;i++) {int x; scanf ("%d%s", &x,st+1); int Len=strlen (st+1); Matrix Res=1;
res.n=res.m=4;
for (int i=1;i<=len;i++) Res=res*getm (St[i]);
Ans=ans*pow (RES,X);
} Ans=ans*getm (' + ');
printf ("%d\n", ans.a[0][0]);
return 0; };