题意:给你一个数字 然后每次改变一种数 问你最后数字MOD 10e9+7 是多少?
思路:我们只要知道某种数最后变成怎么样就好。所以我是从最后一次变化往上推,每一次要变的那种数都是向下一次取得数据进行改造。
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<math.h>
#include<string>
using namespace std;
#define LL long long
#define MOD 1000000007
#define N 100010
struct node
{
LL x;
LL l;
};
node r[N][11];
struct edge
{
int l;
string ch;
};
edge e[N];
int n;
char ch[N];
char str[N];
LL quick(LL x)
{
LL res=1,t=10;
while(x)
{
if(x%2)res=(res*t)%MOD;
t=(t*t)%MOD;
x/=2;
}
return res;
}
node get(edge t,int x)
{
node res;
res.l=0;
res.x=0;
if(t.ch.length()==0) return res;
for(int i=0;i<t.ch.length();i++)
{
int f=t.ch[i]-'0';
res.l=(res.l+r[x][f].l)%(MOD-1);
res.x=((res.x*quick(r[x][f].l))%MOD+r[x][f].x)%MOD;
}
return res;
}
void solve()
{
for(int i=0;i<=9;i++)
{
r[n+1][i].l=1;
r[n+1][i].x=i;
}
for(int i=n;i>=1;i--)
{
for(int j=0;j<=9;j++)
{
if(j==e[i].l)
{
r[i][j]=get(e[i],i+1);
}
else
{
r[i][j]=r[i+1][j];
}
}
}
}
int main()
{
scanf("%s",ch);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",str);
e[i].l=str[0]-'0';
int len=strlen(str);
for(int j=0;str[j+3];j++)
{
e[i].ch+=str[j+3];
}
}
LL ans=0;
solve();
for(int i=0;ch[i];i++)
{
int x=ch[i]-'0';
ans=(ans*quick(r[1][x].l)%MOD+r[1][x].x)%MOD;
}
printf("%lld\n",ans);
return 0;
}