luogu#P4950. 完美数字

完美数字

Problem Description

You are given two sets of digits SS and TT, where all elements are integers between 00 and 99.

A “perfect number” is a number whose digits contain all digits in SS and contain none of the digits in TT.

For example, if S={1,3,4}S=\{1,3,4\} and T={7,8}T=\{7,8\}, then 13451345, 341166341166, and 41331294133129 are perfect numbers, while 1313, 84318431, and 3417134171 are not (because 1313 does not contain digit 44, and although 84318431 and 3417134171 contain digits 11, 33, and 44, they also contain 88 and 77).

Find the sum of all perfect numbers in [l,r][l,r].

Input Format

The first line contains a positive integer TT, indicating the number of test cases.

Then follow TT test cases:

The first line contains two positive integers l,rl,r.

The second line: first input a positive integer nSn_S representing the number of elements in SS, followed by nSn_S integers representing the elements of SS.

The third line: first input a positive integer nTn_T representing the number of elements in TT, followed by nTn_T integers representing the elements of TT.

Output Format

For each test case, output one integer: the sum of all perfect numbers in [l,r][l,r].

3
11 40
1 3
2 4 7
2018 20170901
1 4
1 7
19260817 998244353
3 2 5 6
4 0 7 8 9

310
52885750312822
2039707997741122

Hint

Sample Explanation

For the first sample test case, the perfect numbers are:

1313, 2323, 3030, 3131, 3232, 3333, 3535, 3636, 3838, 3939.

So the total sum is 310310.

Constraints

For 30%30\% of the testdata, 1lr1041 \le l \le r \le 10^4.

For another 10%10\% of the testdata, nS=nT=0n_S=n_T=0.

For 100%100\% of the testdata, 1T20001 \le T \le 2000, 1lr1091 \le l \le r \le 10^9, 0nS,nT100 \le n_S,n_T \le 10. It is guaranteed that all elements in SS and TT are integers in [0,9][0,9].

Translated by ChatGPT 5