luogu#P4950. 完美数字
完美数字
Problem Description
You are given two sets of digits and , where all elements are integers between and .
A “perfect number” is a number whose digits contain all digits in and contain none of the digits in .
For example, if and , then , , and are perfect numbers, while , , and are not (because does not contain digit , and although and contain digits , , and , they also contain and ).
Find the sum of all perfect numbers in .
Input Format
The first line contains a positive integer , indicating the number of test cases.
Then follow test cases:
The first line contains two positive integers .
The second line: first input a positive integer representing the number of elements in , followed by integers representing the elements of .
The third line: first input a positive integer representing the number of elements in , followed by integers representing the elements of .
Output Format
For each test case, output one integer: the sum of all perfect numbers in .
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:
, , , , , , , , , .
So the total sum is .
Constraints
For of the testdata, .
For another of the testdata, .
For of the testdata, , , . It is guaranteed that all elements in and are integers in .
Translated by ChatGPT 5