代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXK = 65;
const int MAXM = 25;
string n, ans;
int k, len, cnt;
bool vis[MAXM];
int x[MAXK], y[MAXK];
void DFS(int a)
{
if(vis[a]) return;
vis[a] = true;
cnt++;
for(int i = 1; i <= k; i++)
if(x[i] == a)
DFS(y[i]);
return;
}
string C(string A, int B)
{
int len = A.size();
int AA[MAXK];
for(int i = 1; i <= len; i++)
{
AA[i] = int(A[len - i]) - 48;
AA[i] *= B;
}
for(int i = 1; i <= len; i++)
{
if(AA[i] >= 10)
{
AA[i + 1] += AA[i] / 10;
AA[i] %= 10;
if(i == len) len++, A += '0';
}
}
string num = A;
for(int i = 1; i <= len; i++)
num[len - i] = char(AA[i] + 48);
return num;
}
int main()
{
cin >> n >> k;
len = n.size();
for(int i = 1; i <= k; i++)
cin >> x[i] >> y[i];
ans = "1";
for(int i = 0; i < len; i++)
{
memset(vis, 0, sizeof(vis));
cnt = 0;
DFS(int(n[i]) - 48);
ans = C(ans, cnt);
}
cout << ans;
return 0;
}
WA+RE评测记录:
https://www.luogu.com.cn/record/33698736