注意密码位数<=500 输出注意十六进制改成字母

要点题目都已说明 ac就好

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue> #define N 5100
#define L(x) (x<<1)
#define R(x) (x<<1|1)
using namespace std; inline int Max(int a,int b){return a>b?a:b;}
inline int Min(int a,int b){return a<b?a:b;}
int a[20],k,n,c;
int vis[N]; //取模后出现的可能只会是 [0, n-1]
int pre[N];
int num[N];
void BFS(){
memset(vis, 0, sizeof(vis));
memset(pre, -1, sizeof(pre));
queue<int>q;
int mod; for(int i=0;i<k;i++)
{
if(!a[i])continue;
mod = a[i]%n;
if(!vis[mod])
{
vis[mod] = 1;
num[mod] = a[i];
q.push(mod);
} if(a[i]%n == 0)
{if(a[i]<=10)printf("%d\n",a[i]); else printf("%c\n",a[i]-10+'A');return ;}
}
bool su =false;
while(!q.empty() && su == false){
mod = q.front(); q.pop();
for(int i=0;i<k;i++)
{
int now = (mod*c + a[i])%n;
if(now == 0){ pre[now] = mod;num[now]=a[i]; su = true; break; }
if(!vis[now])
{
q.push(now);
vis[now]=1;
pre[now] = mod;
num[now] = a[i];
}
}
} if(su == false){printf("give me the bomb please\n");return ;}
int top=0,ans[N];
for(int i=0; i!=-1;i=pre[i])
ans[top++]=num[i];
if(top>500){printf("give me the bomb please\n");return ;}
for(int i=top-1;i>=0;i--)
if(ans[i]>=10)
printf("%c",ans[i] - 10 +'A');
else
printf("%d",ans[i]);
printf("\n");
} int main(){
int T;scanf("%d",&T); while(T--){
scanf("%d %d %d",&n,&c,&k); for(int i=0;i<k;i++){
char c=getchar();
while( !( '0'<=c && c<='9') && !( 'A'<=c && c<='F')) c=getchar(); if( '0'<=c && c<='9' ) a[i] = c - '0';
else a[i] = c - 'A' +10;
} sort(a,a+k); if(n == 0){
if(a[0])printf("give me the bomb please\n");
else printf("0\n");
continue;
} BFS();
}
return 0;
}
/*
3
22 10
3
7 0 1 2 10
1
1 25 16
3
A B C */

HDU 1226 BFS的更多相关文章

  1. hdu 1226 BFS + bfs记录路径

    http://acm.hdu.edu.cn/showproblem.php? pid=1226 为了节省空间.您可以使用vis初始化数组初始化-1. 发现BFSeasy错了地方 始一直WA在这里:就是 ...

  2. hdu 1226 bfs+余数判重+大数取余

    题目: 超级密码 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  4. HDU 1226 超级密码(数学 bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1226 超级密码 Time Limit: 20000/10000 MS (Java/Others)    ...

  5. hdu.1226.超级密码(bfs)

    超级密码 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  6. hdu 1226 超级密码(bfs+余数判重)

    题意:略过 分析:用m个数字组成一个能够被n整除的c进制数的最小值,实际上本题的关键就在于这个最小值上.  首先确定我们的思路是从小到大寻找.先查看一位数,即查看着m个数字是否能被n整除:若不能,就查 ...

  7. HDU 1226 超级密码(BFS) (还需研究)

    Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Desc ...

  8. HDU 1226 超级密码 (搜素)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1226 题意简单,本来是一道很简单的搜素题目. 但是有两个bug: 1.M个整数可能有重复的. 2.N可 ...

  9. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

随机推荐

  1. cocos2d-x 多分辨率适配详解(转载),以前北京团队设计的游戏,也是用这套方案

    http://blog.csdn.net/kyo7552/article/details/17163487 多种分辨率的适配一直都是一个蛋疼的问题,各家公司可能都有自己的一套方案.今天我为大家介绍的是 ...

  2. Android:使用ViewPager实现左右滑动切换图片(图上有点点)

    在以下实例的基础上加上点点 Android:使用ViewPager实现左右滑动切换图片 (简单版) 效果预览: 因为要把点点放图片上,所以修改布局为相对布局: <?xml version=&qu ...

  3. window下gvim中文界面改变成英文界面

    中文环境下设置GVIM的界面.菜单.提示为英文语言 修改你的_vimrc,通常为类似C:\Program Files\Vim 加入以下语句至末尾 " set the menu & t ...

  4. The document has been modified outside of Code Composer. Would you like to reload the file

    2013-06-20 10:03:32 烧写过程是合众达给出的文档 problem: I'm new to using Code Composer Studio 3.3 and am having a ...

  5. *gravity的取值详表

    android有 android:layout_gravity 和 android:gravity,前者设置相对父控件布局,后者是设置自己内部的控件的布局. Value Description top ...

  6. Linux下安装Wine运行windows程序

    资料 首页 https://www.winehq.org/ 安装 https://www.winehq.org/download/ 教程 https://www.winehq.org/document ...

  7. 手机通过WIFI连上ZXV10 H618B路由器但不能上网问题的解决

    前几天朋友帮忙拿到一个ZXV10 H618B路由器,一看需要12V供电,还好以前留下一个12V输出的DC充电器,关键时刻用上了,先大概下载了此路由器的用户手册,发现原来是08年的产品,都5年了. 开始 ...

  8. Excel 内容粘贴到DataGridView, DataGridView 粘贴到 Excel

    void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Ke ...

  9. Ruby类,模块1

    类的扩展和继承 class Fixnum def dosome(str) puts str end def abs puts "覆盖了原有的方法" end end puts 1.c ...

  10. CSS3 Transitions, Transforms和Animation使用简介与应用展示

    CSS3 Transitions, Transforms和Animation使用简介与应用展示 by zhangxinxu from http://www.zhangxinxu.com本文地址:htt ...