3 22 10 3 7 0 1 2 10 1 1 25 16 3 A B C
110 give me the bomb please CCBHuge input, scanf is recommended.HintHint
#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' ; else now.ans += i-10+'A' ; now.mod = i%n ; que.push(now) ; visited[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 < 16 ; ++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 ; else t[ch-'A'+10] = true ; getchar() ; } memset(visited,false,sizeof(visited)) ; ans.clear() ; if(n == 0) { if(t[0]) cout<<0<<endl ; else cout<<"give me the bomb please"<<endl ; continue ; } flag = false ; BFS() ; if(!flag) cout<<"give me the bomb please"<<endl ; else cout<<ans<<endl ; } return 0 ; }
原文地址:http://blog.csdn.net/lionel_d/article/details/44924989