HDU 1226 Super Password BFS very good problem ah!

Source: Internet
Author: User

Super PasswordTime limit:20000/10000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2883 Accepted Submission (s): 928


Problem Descriptionignatius took one weeks to finally find the legendary treasure, the treasure was put in a room, the door of the room was locked with a password, on the wall next to the door there are some information about the password:
The password is a C-ary number, and can only be made up of a given m number, and the password is a positive integer multiple of a given decimal integer N (0<=n<=5000) (if there are more than one number that satisfies the condition, then the smallest one is the password), if such a password exists, Then when you enter it with the backdoor will open, if there is no such password ... Then blow the door.

Note: Due to the long history of the treasure, the system can only store 500-bit passwords at the time. Therefore, if the resulting password length greater than 500 can not 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) that represents the number of test data. The first line of the test data for each group is two integers N (0<=n<=5000) and C (2<=c<=16), where n represents a given decimal integer in the title description, and C is the number of the password. The second line of the test data is an integer M (1<=m<=16), which represents the number of digits that comprise the password, followed by the number of m digits used to represent the password. Two test data will be separated by a blank line .

Note: In the given m number, if there are more than 10 of the number, we have agreed to use a to denote 10,b to represent the 11,c to represent the 12,d to represent the 13,e to represent the 14,f to represent the 15. I guarantee that the input data is legal.

Output for each set of test data, if a required password exists, the password is printed, and if the password does not exist, the output is "Give me the Bomb."

Note: The numbers that make up the password are not necessarily all used; The password may be very long, do not attempt to use an integer variable to save the password; I guarantee that the maximum password is not 0 (unless the password itself is 0).

Sample Input
322 1037 0 101125 163A B C

Sample Output
110give me the bomb PLEASECCBHintHintHuge input, scanf is recommended.
The remainder of the sentence weight. 152%n= (100+ (50+2%n)%n)%n. Code:
#include <cstdio> #include <string> #include <queue> #include <cstring> #include <iostream >using namespace std; struct node{string ans;int mod;};  int n, c, M; bool T[20], visited[5500], flag = False, string ans, void BFS () {Node now; queue <Node> que; for (int i = 1; I < 16; ++i) {if (T[i]) {now.ans.clear (); if (i<10) Now.ans + = i+ ' 0 '; Elsenow.ans + = i-10+ ' A '; now.mod = i%n; Que.push (now); visite D[i%n] = = true;}} Flag = False, while (!que.empty ()) {now = Que.front (); Que.pop (); if (flag && now.ans.size () >ans.size ()) Continue; if (Now.mod = = 0) {if (!flag) {ans = Now.ans; flag = true;} if (Ans.size () >now.ans.size () | | (Ans.size () ==now.ans.size () &&ans>now.ans)) {ans = Now.ans;} Continue;} for (int i = 0; i <, ++i) {if (T[i]) {Node next = now; if (i<10) {Next.ans + = i+ ' 0 ';} Else{next.ans + = i-10+ ' A ';} Next.mod = (next.mod*c+i)%n; if (Next.ans.size () <=500 &&!visited[next.mod]) | | next.mod==0) {Que.push (next); Visited[next.MoD] = True;}}}} int main () {int T; scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&c), scanf ("%d", &m); GetChar (); Memset (T , false,sizeof (t)); for (int i = 0; i < m; ++i) {char ch; scanf ("%c", &ch); if (ch>= ' 0 ' &&ch<= ' 9 ') t[ch-' 0 '] = true; elset[ch-' A ' +10] = true; GetChar ();} memset (visited,false,sizeof (visited)); Ans.clear (); if (n = = 0) {if (t[0]) Cout<<0<<endl;elsecout<< " Give me the bomb please "&LT;&LT;ENDL; continue;} Flag = false; BFS (); if (!flag) cout<< "Give Me the Bomb" &LT;&LT;ENDL; Elsecout<<ans<<endl;} return 0;}

with June

HDU 1226 Super Password BFS very good problem ah!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.