Super Password
Time Limit: 20000/10000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1584 accepted submission (s): 478 problem descriptionignatius spent a week finally finding the legendary treasure where the treasure was placed in a room, the door of the room is locked with a password. There are some prompts about the password on the wall next to the door:
The password is a C-digit number and can only consist of a given m numbers. The password is a given decimal integer N (0 <= n <= 5000) (if there are multiple numbers that meet the conditions, the smallest one is the password). If such a password exists, it will be opened after you enter it, if this password does not exist ...... let's close the door.
Note: Because the treasure has a long history, the system can only store a maximum of 500 passwords at that time. Therefore, if the obtained password length is greater than 500, it cannot be used to open the door. This situation is also considered that the password does not exist.
The first line of input data is an integer T (1 <=t <= 300), indicating the number of test data. the first row of each group of test data is two integers, n (0 <= n <= 5000) and C (2 <= C <= 16 ), N indicates the given decimal integer in the Title Description, and C indicates the base number of the password. the second row of the test data is an integer m (1 <= m <= 16), which indicates the number of digits that constitute the password, and then M digits are used to represent the digits that constitute the password. there is a blank line between the two test data.
Note: In the given m numbers, if there are more than 10 numbers, we agree to use a to represent 10, B to represent 11, C to represent 12, d to represent 13, e Indicates 14, F indicates 15. I ensure that the input data is valid.
Output for each group of test data, if there is a required password, the output password, if the password does not exist, the output "give me the bomb please ".
Note: not all the numbers that make up the password must be used. The password may be very long. Do not use an integer variable to save the password; I guarantee that the maximum password bit is not 0 (unless the password itself is 0 ).
Sample Input
322 1037 0 12 101125 163A B C
Sample output
110give me the bomb pleaseCCBHintHint Huge input, scanf is recommended.
It's almost the same as thinking and poj1465. You can check my poj 1465 first if you don't understand it.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # define maxn 5005 using namespace STD; int n, m, C, ans; int A [20]; char DX [] = "0123456789 abcdef"; bool vis [maxn]; struct node {char D; int Val, pre, step;} cur, now, Q [maxn]; bool BFS () {int I, j, nval, tval, nstep; int head = 0, tail =-1; memset (VIS, 0, sizeof (VIS); cur. step = 0; cur. pre =-1; for (I = 1; I <= m; I ++) {if (a [I] = 0) continue; cur. D = DX [A [I]; Cur. val = A [I] % N; vis [A [I] % N] = 1; Q [++ tail] = cur;} while (Head <= tail) {now = Q [head]; nval = now. val; nstep = now. step; If (nstep> = 500) return false; If (nval = 0) {ans = head; return true ;}for (I = 1; I <= m; I ++) {tval = (nval * C + A [I]) % N; If (! Vis [tval]) {vis [tval] = 1; cur. D = DX [A [I]; cur. pre = head; cur. step = nstep + 1; cur. val = tval; Q [++ tail] = cur ;}} head ++;} return false;} void output (int K) // recursively output the answer {If (k =-1) return; else {output (Q [K]. PRE); printf ("% C", Q [K]. d) ;}} int main () {int I, j, T, flag; char s [10]; scanf ("% d", & T ); while (t --) {scanf ("% d", & N, & C, & M); for (I = 1; I <= m; I ++) {scanf ("% s", S); If (s [0]> = '0' & S [0] <= '9 ') A [I] = s [0]-'0'; El Se A [I] = s [0]-'A' + 10;} Sort (a + 1, A + m + 1); If (n = 0) // {if (a [1]! = 0) printf ("give me the bomb please \ n"); else printf ("0 \ n"); continue;} flag = 0; for (I = 1; I <= m; I ++) // special sentence {if (a [I] & A [I] % N = 0) {printf ("% C \ n", DX [A [I]); flag = 1; break ;}} if (FLAG) continue; If (BFS ()) output (ANS); else printf ("give me the bomb please"); printf ("\ n");} return 0 ;}