超级密码

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2108    Accepted Submission(s): 669


Problem Description
Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息:
密码是一个C进制的数,并且只能由给定的M个数字构成,同时密码是一个给定十进制整数N(0<=N<=5000)的正整数倍(如果存在多个满足条件的数,那么最小的那个就是密码),如果这样的密码存在,那么当你输入它以后门将打开,如果不存在这样的密码......那就把门炸了吧.

注意:由于宝藏的历史久远,当时的系统最多只能保存500位密码.因此如果得到的密码长度大于500也不能用来开启房门,这种情况也被认为密码不存在.
 

 

Input
输入数据的第一行是一个整数T(1<=T<=300),表示测试数据的数量.每组测试数据的第一行是两个整数N(0<=N<=5000)和C(2<=C<=16),其中N表示的是题目描述中的给定十进制整数,C是密码的进制数.测试数据的第二行是一个整数M(1<=M<=16),它表示构成密码的数字的数量,然后是M个数字用来表示构成密码的数字.两个测试数据之间会有一个空行隔开.

注意:在给出的M个数字中,如果存在超过10的数,我们约定用A来表示10,B来表示11,C来表示12,D来表示13,E来表示14,F来表示15.我保证输入数据都是合法的.
 

 

Output
对于每组测试数据,如果存在要求的密码,则输出该密码,如果密码不存在,则输出"give me the bomb please".

注意:构成密码的数字不一定全部都要用上;密码有可能非常长,不要试图用一个整型变量来保存密码;我保证密码最高位不为0(除非密码本身就是0).
 

 

Sample Input
3
22 10
3
7 0 1
2 10
1
1
25 16
3
A B C
 

 

Sample Output
110
give me the bomb please
CCB

 

 

 1 #include <iostream>
 2 #include <math.h>
 3 #include <algorithm>
 4 #include <stdio.h>
 5 #include <string.h>
 6 #include <queue>
 7 using namespace std;
 8 int n,c,m;
 9 char a[20];
10 int p[50000];
11 int funa(char s)
12 {
13     if(s>='0'&&s<='9')return s-'0';
14     else return s-'A'+10;
15 }
16 int fun(string d)
17 {
18     if(d.size()>=505)
19         return -1;
20     int i;
21     int yu=0;
22     for(i=0; i<d.size(); i++)
23     {
24         yu=(yu*c+funa(d[i]))%n;
25     }
26     return yu;
27 }
28 void bfs()
29 {
30     int i;
31     memset(p,0,sizeof(p));
32     queue<string> z;
33     while(!z.empty())z.pop();
34     string x;
35     for(i=0; i<m; i++)
36     {
37         if(a[i]=='0')continue;
38         x=a[i];
39         int r=fun(x);
40         if(!p[r])
41         {
42             z.push(x);
43             p[r]=1;
44         }
45         if(p[0])
46         {
47             cout<<a[i]<<endl;
48             return ;
49         }
50     }
51     while(!z.empty())
52     {
53         x=z.front();
54         for(i=0; i<m; i++)
55         {
56             int r=fun(x+a[i]);
57             if(r==-1)break;
58             if(!p[r])
59             {
60                 z.push(x+a[i]);
61                 p[r]=1;
62                 if(p[0])
63                 {
64                     cout<<x+a[i]<<endl;
65                     return ;
66                 }
67             }
68         }
69         z.pop();
70     }
71     cout<<"give me the bomb please"<<endl;
72 }
73 int main()
74 {
75     int t,i;
76     cin>>t;
77     while(t--)
78     {
79         cin>>n>>c>>m;
80         for(i=0; i<m; i++)cin>>a[i];
81         sort(a,a+m);
82         if(n==0)
83         {
84             if(a[0]=='0')
85             cout<<0<<endl;
86             else cout<<"give me the bomb please"<<endl;
87         }
88         else
89         bfs();
90     }
91 }
View Code

 

posted on 2014-03-18 23:33  ERKE  阅读(236)  评论(0编辑  收藏  举报