#P1037. [NOIP 2002 普及组] 产生数

    ID: 37 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>高精度2002NOIp 普及组深度优先搜索,DFS

[NOIP 2002 普及组] 产生数

Description

Given an integer nn and kk transformation rules.

Rules:

  • A single digit can be transformed into another single digit.
  • The right-hand side of a rule cannot be zero.

For example: n=234,k=2n = 234, k = 2. There are two rules:

  • 252\longrightarrow 5.
  • 363\longrightarrow 6.

After applying transformations, the integer 234234 can produce the following integers (including the original number):

  • 234234.
  • 534534.
  • 264264.
  • 564564.

There are 44 distinct generated numbers.

Now, given an integer nn and kk rules, determine how many different integers can be produced by applying any number of transformations (zero or more). Only output the count.

Input Format

The first line contains two integers n,kn, k, as described.

The next kk lines each contain two integers xi,yix_i, y_i, representing each rule.

Output Format

Output a single line containing the number of distinct integers that can be generated.

234 2
2 5
3 6

4

Hint

Constraints: For 100%100\% of the testdata, n<1030n < 10^{30}, k15k \le 15.

Problem Source: NOIP 2002 Junior, Problem 3.

Translated by ChatGPT 5