标签:
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3866 Accepted Submission(s): 1241
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #include <stdlib.h> #include <string> #include <iostream> using namespace std; int n,c,m; bool mod[5005]; char num[20]; struct Node{ int mod; string ans; int len; }; void bfs(){ memset(mod,0,sizeof(mod)); queue<Node> q; Node s; for(int i=1;i<=m;i++){ if(num[i]!=‘0‘) { s.ans= num[i]; if(isdigit(num[i])) s.mod = (num[i]-‘0‘)%n; else s.mod = (num[i]-‘A‘+10)%n; s.len = 1; q.push(s); } } while(!q.empty()){ Node now = q.front(); q.pop(); //cout<<now.ans<<endl; if(now.len>500) { printf("give me the bomb please\n"); return; } if(now.mod==0){ cout<<now.ans<<endl; return ; } Node next; for(int i=1;i<=m;i++){ next.ans = now.ans+num[i]; if(isdigit(num[i])) next.mod = (now.mod*c+(num[i]-‘0‘))%n; else next.mod = (now.mod*c+(num[i]-‘A‘+10))%n; //cout<<next.ans<<endl; next.len=now.len+1; if(mod[next.mod]) continue; mod[next.mod]=true; q.push(next); } } printf("give me the bomb please\n"); return; } int main(){ int tcase; scanf("%d",&tcase); while(tcase--){ scanf("%d%d",&n,&c); scanf("%d",&m); bool flag = false; getchar(); for(int i=1;i<m;i++){ scanf("%c ",&num[i]); if(n==0&&num[i]==‘0‘) flag = true; } scanf("%c",&num[m]); if(n==0&&num[m]==‘0‘) flag = true; sort(num+1,num+1+m); if(n==0){ if(flag){ printf("0\n"); continue; }else{ printf("give me the bomb please\n"); continue; } } bfs(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5744164.html