hdu 1226(同余搜索)

超级密码

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

Problem Description

Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息:

码是一个C进制的数,并且只能由给定的M个数字构成,同时密码是一个给定十进制整数N(0<=N<=5000)的正整数倍(如果存在多个满足
条件的数,那么最小的那个就是密码),如果这样的密码存在,那么当你输入它以后门将打开,如果不存在这样的密码......那就把门炸了吧.

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

Input


入数据的第一行是一个整数T(1<=T<=300),表示测试数据的数量.每组测试数据的第一行是两个整数N(0<=N&
lt;=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

Hint

Hint

Huge input, scanf is recommended.

Author

Ignatius.L

Source

杭州电子科技大学第三届程序设计大赛

题解:强大的同余剪枝,如果某个数 a%n == b%n (a<b) 如果目标状态为 (a + c)%n == 0,而(a+c)%n = a%n + c%n = b%n + c%n ,所以搜索 a b是等价的,但是我们要的是最小的解,所以我们可以不再去找b,所以我们标记余数,这样的话可以剪掉非常多的状态,这样的话就不会超时了.

所以具体解法为排序后进行同余搜索,0要特判。

#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;
}
时间: 2024-08-27 15:38:03

hdu 1226(同余搜索)的相关文章

HDU 4937 Lucky Number 搜索

题意: 给你一个数,求在多少种不同的进制下这个数每一位都是3.4.5.6中的一个. 思路: 搜索.枚举这个数在任意进制下的表示,判断是否合法.当数字只有3.4.5.6时,必定有无穷种. 因为数字太大,所以直接枚举必定会超时. 下面有两种剪枝的方法: 1. 先枚举最后一位的情况. 假设数字n在base进制下表示为 a[n]...a[0],即 n = a[0] + a[1]*base^1 + ... + a[n]*base^n. 则 n - a[0] = a[1]*base^1 + ... + a[

HDU 1078 记忆化搜索

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4575 Accepted Submission(s): 1829 Problem Description FatMouse has stored some cheese in a city. The city can be considered as a

hdu 4960 记忆化搜索 DP

Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 490    Accepted Submission(s): 180 Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morni

hdu 1226 超级密码 bfs+取余判重

超级密码 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2808    Accepted Submission(s): 897 Problem Description Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息: 密码是一个C进制的

hdu 1664(数论+同余搜索+记录路径)

Different Digits Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1430    Accepted Submission(s): 392 Problem Description Given a positive integer n, your task is to find a positive integer m, w

hdu 1226 BFS + bfs记录路径

http://acm.hdu.edu.cn/showproblem.php?pid=1226 为了省空间,可以用vis数组初始化的时候初始化为-1, 发现一个BFS容易错的地方 开始一直WA在这里:就是我int tp=q.front();之后马上q.pop():了,然后才去判断是不是符合条件以break,这样就不能根据q.empty()==1认为没有找到ans 因为这里WA了 其实也可以vis[0] == -1来判断 比较不理解的是 当n==0的时候 %n==0的时候怎么处理 //#pragma

hdu - 1226 超级密码 (bfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1226 难以想到怎么去bfs,还是对状态的划分不明确,知道了之后感觉还是挺简单的. 这题关键是密码可能很长,然后判断是否整除用到了一点技巧,确保不会溢出,输出的时候是用递归回溯输出. 因为同一个数可以取多次,而最终取的是数值最小的,故输入之后从小到大排序,然后从第一个数到最后一个数每次添加一遍,直到找到合适的为止. 为了便于输出用了数组模拟队列. 1 #include <cstdio> 2 #include

hdu 1560 迭代加深搜索

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 只能说bin神太给力了.. 又学到不少新知识.. 迭代加深搜索,貌似 又叫IDA*, 就是给搜索深度一个限制,搜索到一个满足条件就结束. 注意剪枝~ 代码: #include <iostream> #include <cstdio> #include <cstring> using namespace std; char g[10][10]; int size[10];

poj 1426(同余搜索)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26926   Accepted: 11174   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains