|
沙发
楼主 |
发表于 2018-10-3 19:03:53
|
只看该作者
- #include<iostream>
- #include<cstring>
- #define FOR(i,a,b) for(register int i=a;i<=b;i++)
- #define ROF(i,a,b) for(register int i=a;i>=b;i--)
- using namespace std;
- string n;
- int len,k,m[11][3],c[200],ans[200];
- bool num[11][11];
- void add(int *a)
- {
- a[1]++;a[0]=1;
- if(a[1]>=10){a[2]++;a[1]=0;a[0]=2;}
- return;
- }
- void mul(int *a,int *b)
- {
- memset(c,0,sizeof(c));
- int ex;
- FOR(i,1,a[0])
- {
- ex=0;
- FOR(j,1,b[0])
- {
- c[i+j-1]+=a[i]*b[j]+ex;
- ex=c[i+j-1]/10;
- c[i+j-1]%=10;
- }
- c[i+b[0]]+=ex;
- }
- c[0]=a[0]+b[0]+1;
- while(c[0]>1 && !c[c[0]])c[0]--;
- FOR(i,0,c[0])a[i]=c[i];
- }
- void print(int *a){ROF(i,a[0],1)cout<<a[i];cout<<endl;return;}
- void Floyd()
- {
- FOR(k,0,9)FOR(i,0,9)FOR(j,0,9)if(num[i][k] && num[k][j])num[i][j]=1;
- }
- int main()
- {
- cin>>n>>k;FOR(i,0,9)num[i][i]=1;len=n.size();
- FOR(i,1,k){int a,b;cin>>a>>b;num[a][b]=1;}
- Floyd();///FOR(i,0,9){FOR(j,0,9)cout<<num[i][j]<<" ";cout<<endl;}
- FOR(i,0,9)FOR(j,0,9)if(num[i][j])add(m[i]);//FOR(i,0,9)print(m[i]);
- ans[0]=1;ans[1]=1;
- FOR(i,0,len-1)mul(ans,m[n[i]-'0']);
- print(ans);
- return 0;
- }
复制代码 |
|