您的位置:首页 > 其它

HDU 1226 超级密码

2013-09-05 10:27 169 查看
bfs。用给定的数字,组成n的倍数(最小),在m进制下输出。先在10进制下处理,输出的时候判断一下就行。长度不能超过500.

先把可用的,小于m的数存下来备用,每次拿到一个余数,就乘以m再加一遍可以扩展的数,用余数判重,直至得到0或队空。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", n)
#define PC(n) printf("Case %d: ", n)
///OTHER
#define PB(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 9901;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 5555;
const int M = 22;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};

struct node
{
int v, cnt;
node(){};
node(int a, int b){v = a, cnt = b;};
}p;
int pre
, val
, a[M], b[M];
int n, num, m;

void print(int x)
{
if(pre[x])print(pre[x]);
printf("%c", val[x] > 9 ? char(val[x] - 10 + 'A') : char(val[x] + '0'));
}

void bfs()
{
int k;
queue<node> q;
FFF(i, 0, min(16, m))if(b[i])
{
if(i % n == 0 && i)
{
PI(i);
return;
}
if(i)
{
q.push(node(i, 1));
pre[i] = 0;
val[i] = i;
}
}
while(!q.empty())
{
p = q.front();
q.pop();
if(p.cnt > 500)break;
REP(i, num)
{
k = (p.v * m + a[i]) % n;
if(!k)
{
print(p.v);
printf("%c\n", a[i] > 9 ? char(a[i] - 10 + 'A') : char(a[i] + '0'));
return;
}
if(pre[k] == -1)
{
pre[k] = p.v;
val[k] = a[i];
q.push(node(k, p.cnt + 1));
}
}
}
puts("give me the bomb please");
}

int main()
{
//freopen("input.txt", "r", stdin);

int t, k;
char s[5];
RI(t);
while(t--)
{
RIII(n, m, k);
CLR(pre, -1);
CLR(b, 0);
REP(i, k)
{
RS(s);
if(s[0] >= '0' && s[0] <= '9')b[s[0] - '0'] = 1;
else b[s[0] - 'A' + 10] = 1;
}
if(!n)
{
if(b[0])PI(0);
else puts("give me the bomb please");
continue;
}
num = 0;
FFF(i, 0, min(16, m))if(b[i])a[num++] = i;
bfs();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: