# P1037 production (high precision & DFS)

2021-08-10 08:04:59

## P1037 Generating numbers ( Hyperfine &DFS)

Subject portal

Given as a large integer ,k The conversion of middle numbers . Find out how many different numbers can be converted into .

Ideas ： utilize DFS Find every bit that can be converted , According to the principle of multiplication, multiplying the situation of each bit is the answer .

``````#include<bits/stdc++.h>
using namespace std;
int k,ans[100]={1},vis[10],tmp;//vis[i] Mark whether the number has been accessed
pair<int,int>p[20];
void dfs(int x){ // Search every possible situation
for(int i=1;i<=k;i++)
if(!vis[p[i].second]&&p[i].first==x)
vis[p[i].second]=1,tmp++,dfs(p[i].second);
}
void jc(int *a,int b){ // High precision multiplication and low precision  ( Analog high-precision factorial )
for(int i=0,p=0;i<100;i++)
{
a[i]=a[i]*b+p;
p=a[i]/10;
a[i]%=10;
}
}
int main(){
string a;
cin>>a>>k;
for(int i=1;i<=k;i++) cin>>p[i].first>>p[i].second;
for(int i=0;i<a.size();i++){
int num=a[i]-'0';
memset(vis,0,sizeof vis);
vis[num]=1,tmp=1;
dfs(num);
jc(ans,tmp);// Multiplication principle
}
for(int i=99,f=0;i>=0;i--)
{
if(ans[i]) f=1;
if(f) printf("%d",ans[i]);
}
puts("");
return 0;
}
```

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
```

https://chowdera.com/2021/08/20210810080205604q.html