#include<iostream>
#include<queue>
#include<string>
using namespace std;
int n,c,m;
int a[20];
int flag;
bool visited[5100];
string ans;
struct node{
string ans;
int mod;
}now;
void bfs(){
queue<node> q;
int i;
for(i=1;i<16;i++)
{
if(a[i])
{
visited[i%n]=1;
if(i<10)
{
now.ans=""; now.ans=i+'0'; now.mod=i%n; q.push(now);
}
else
{
now.ans=""; now.ans='A'+i-10 ; now.mod=i%n; q.push(now);
}
}
}
while(!q.empty())
{
now=q.front(); q.pop();
if(flag && now.ans.size() > ans.size()) continue;
if(now.mod==0)
{
if(!flag) ans=now.ans;
flag=1;
if(ans.size()>now.ans.size() || (ans.size()==now.ans.size() && ans>now.ans))
{
ans=now.ans;
}
}
for(i=0;i<16;i++)
{
if(a[i])
{
if(i<10)
{
node next=now;
next.ans+=(i+'0');
next.mod=(now.mod*c+i)%n;
if((next.ans.size()<=500&&!visited[next.mod])||next.mod==0)
{
visited[next.mod]=1;
q.push(next);
}
}
else
{
node next=now;
next.ans+=('A'+i-10);
next.mod=(now.mod*c+i)%n;
if((next.ans.size()<=500&&!visited[next.mod])||next.mod==0)
{
visited[next.mod]=1;
q.push(next);
}
}
}
}
}
}
int main()
{
int t,i;
cin>>t;
while(t--){
char str;
scanf("%d%d%d",&n,&c,&m);
getchar();
memset(a,0,sizeof(a));
memset(visited,0,sizeof(visited));
for(i=0;i<m;i++) {
scanf("%c",&str);
if(str>='0'&&str<='9'){
a[str-'0']=1;
}
else{
a[str-'A'+10]=1;
}
getchar();
}
flag=0; ans="";
if(n==0){
if(a[0]==1)
cout<<0<<endl;
else
cout<<"give me the bomb please"<<endl;
continue;
}
bfs();
if(flag){
cout<<ans<<endl;
}
else
cout<<"give me the bomb please"<<endl;
}
return 0;
}
HDU 1226 超级密码
最新推荐文章于 2022-02-25 19:38:17 发布