#P4163. [SCOI2007] 排列

    ID: 3096 远端评测题 500ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>搜索2007四川各省省选剪枝状态压缩,状压排列组合

[SCOI2007] 排列

Description

Given a digit string ss and a positive integer dd, count how many distinct permutations of ss are divisible by dd (leading 00 is allowed). For example, 123434123434 has 9090 permutations divisible by 22, among which 3030 end with 22 and 6060 end with 44.

Input Format

The first line contains an integer TT, the number of testdata. Each of the following lines contains ss and dd, separated by a space. String ss contains only digits 0,1,2,3,4,5,6,7,8,90,1,2,3,4,5,6,7,8,9.

Output Format

For each testdata, output one line containing the number of permutations divisible by dd.

7
000 1
001 1
1234567890 1
123434 2
1234 7
12345 17
12345678 29
1
3
3628800
90
3
6
1398

Hint

Constraints: For 100%100\% of the testdata, the length of ss does not exceed 1010, 1d10001 \le d \le 1000, 1T151 \le T \le 15.

In the first three examples, the numbers of permutations are 1,3,36288001, 3, 3628800, and they are all multiples of 11.

Translated by ChatGPT 5