UOJ Logo poj_dwj的博客

博客

超级密码

2019-06-04 22:00:53 By poj_dwj

超级密码

*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

Huge input, scanf is recommended.

Hint

Hint

代码如下:

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
char ch[50];
bool b[5010];
int n,m,z,a[20];
struct node
{
    int x,y;
    int h[520]= {0};
};
void dfs()
{
    node nw,nt;
    queue<node>q;
    for(int i=1; i<16; i++)
    {
        if(a[i])
        {
            nw.x=0;
            nw.h[nw.x]=i;
            nw.y=i%n;
            nw.x++;
            b[i%n]=1;
            q.push(nw);
        }
    }
    while(!q.empty())
    {
        nw=q.front();
        q.pop();
        if(nw.y==0)
        {
            for(int i=0; i<nw.x; i++)
            {
                if(nw.h[i]>=10)
                    printf("%c",nw.h[i]+'A'-10);
                else if(nw.h[i]<=9&&nw.h[i]>=0)
                    printf("%d",nw.h[i]);
            }
            printf("\n");
            return ;
        }
        for(int i=0; i<16; i++)
        {
            if(a[i])
            {
                nt=nw;
                nt.h[nt.x]=i;
                nt.y=(nt.y*z+i)%n;
                nt.x++;
                if(nt.x<=500&&!b[nt.y])
                {
                    b[nt.y]=1;
                    q.push(nt);
                }
            }
        }
    }
    puts("give me the bomb please");
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>z>>m;
        getchar();
        gets(ch);
        int l=strlen(ch);
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i=0; i<l; i++)
        {
            if(ch[i]>='0'&&ch[i]<='9')
                a[ch[i]-'0']=1;
            if(ch[i]>='A'&&ch[i]<='F')
                a[ch[i]-'A'+10]=1;
        }
        if(!n&&a[0])
            puts("0");
        else if(!n)
            puts("give me the bomb please");
        else dfs();
    }
    return 0;
}
poj_dwj Avatar