我的c++代码如下:
#include<bits/stdc++.h>
using namespace std;
#define re register
int read(){
re int x=0,cf=1;
re char ch=getchar();
while(!isdigit(ch)){
if(ch=='-'){
cf=-1;
}
ch=getchar();
}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*cf;
}
int const M=20;
struct node{
int c1;
int c2;
}ch[M];
int const N=11;
int head[N],nex[M],to[M];
int idx;
void add(int u,int v){
to[++idx]=v;
nex[idx]=head[u];
head[u]=idx;
}
bool mp[N][N];
bool vis[N];
int ce[10000];
string mul(string str1,string str2){
int len1,len2;
len1=str1.size();
len2=str2.size();
reverse(str1.begin(),str1.end());
reverse(str2.begin(),str2.end());
int len=len1+len2;
for(int i=0;i<len1;i++){
for(int j=0;j<len2;j++){
ce[i+j]+=(str1[i]-'0')*(str2[j]-'0');
ce[i+j+1]+=ce[i+j]/10;
ce[i+j]%=10;
}
}
while(!ce[len-1]&&len>1) len--;
string ans="";
for(int i=0;i<len;i++){
char now=(char)(ce[i]+'0');
ans=now+ans;
}
return ans;
}
string tur(int x){
string s="";
while(x){
int a=x%10;
a+=48;
x/=10;
s=(char)a+s;
}
return s;
}
int main(){
freopen("generate.in","r",stdin);
freopen("generate.out","w",stdout);
string n;
cin>>n;
int k=read();
for(re int i=1;i<=k;++i){
ch[i].c1=read();
ch[i].c2=read();
add(ch[i].c1,ch[i].c2);
}
for(re int i=0;i<=9;++i){
memset(vis,false,sizeof(vis));
queue<int> q;
q.push(i);
vis[i]=true;
while(!q.empty()){
int x=q.front();
q.pop();
for(re int j=head[x];j;j=nex[j]){
if(vis[to[j]]) continue;
vis[to[j]]=true;
q.push(to[j]);
}
}
for(re int j=0;j<=9;++j){
mp[i][j]=vis[j];
}
mp[i][i]=true;
}
int len=n.size();
string ans="1";
for(re int i=0;i<len;++i){
int anan=0;
int now=n[i]-'0';
if(i==0){
for(re int j=1;j<=9;++j){
if(mp[now][j]) anan++;
}
ans=mul(ans,tur(anan));
continue;
}
for(re int j=0;j<=9;++j){
if(mp[now][j]) anan++;
}
anan--;
ans=mul(ans,tur(anan));
}
cout<<ans<<endl;
return 0;
}
/*
234 2
2 5
3 6
*/