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
题意:中文题面,就不解释了。
思路:BFS,由于数字太大了,所以每次记下取模的结果,然后找mod=0的最小值
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int a[20];
int vis[5555];
int n,c;
int m;
char str;
string tmp;
int flag;
struct Node
{
string ans;
int mod;
}f,t;
queue<Node>q;
void bfs()
{
for(int i=1;i<16;i++)
{
if(a[i])
{
int tt=i%n;
vis[tt]=1;
t.mod=tt;
t.ans="";
if(i<10) t.ans+=(i+'0');
else t.ans+=(i-10+'A');
q.push(t);
}
}
while(!q.empty())
{
t=q.front();q.pop();
if(flag && t.ans.size()>tmp.size())continue;
if(t.mod==0)
{
if(flag==0) tmp=t.ans;
flag=1;
if( tmp.size()>t.ans.size() || (tmp.size()==t.ans.size() && tmp>t.ans) )
tmp=t.ans;
}
for(int i=0;i<16;i++)
{
if(a[i]==0) continue;
if(i<10)
{
f=t;
f.ans+=(i+'0');
f.mod=(f.mod*c+i)%n;
if( (vis[f.mod]==0 && f.ans.size()<=500) || f.mod==0 )
{
vis[f.mod]=1;
q.push(f);
}
}
else
{
f=t;
f.ans+=(i-10+'A');
f.mod=(f.mod*c+i)%n;
if( (vis[f.mod]==0 && f.ans.size()<=500) || f.mod==0 )
{
vis[f.mod]=1;
q.push(f);
}
}
}
}
}
int main()
{
int T;
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d %d",&n,&c);
scanf("%d",&m);
getchar();
memset(a,0,sizeof a);
memset(vis,0,sizeof vis);
for(int i=0;i<m;i++)
{
scanf("%c",&str);
if(str>='0' && str<='9')
a[str-'0']=1;
else if(str>='A' && str<='F')
a[str-'A'+10]=1;
getchar();
}
if(n==0)
{
if(a[0])
printf("0\n");
else puts("give me the bomb please");
continue;
}
while(!q.empty()) q.pop();
flag=0;
bfs();
if(flag)
cout<<tmp<<endl;
else
puts("give me the bomb please");
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/wust_zjx/article/details/47190793