ZOJ - 1136 Multiple (同余+BFS)

Description

a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the smallest strictly positive multiple of N that has no other digits besides X1,X2..XM (if such a multiple exists).

The input file has several data sets separated by an empty line, each data set having the following format:

On the first line - the number N

On the second line - the number M

On the following M lines - the digits X1,X2..XM.

For each data set, the program should write to standard output on a single line the multiple, if such a multiple exists, and 0 otherwise.

An example of input and output:

Input

22

3

7

0

1

2

1

1

Output

110

0

题意:让你用m个数组成n的倍数,要求最小

思路:首先我们排序这m个数,然后从高位开始搜,这样才能保证最先BFS的是最小的,这里用到了一个剪枝就是对于:余数相同的,我们可以拿来判重,小的比较优,也就是最先放进队列的比较优

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 10005;

struct Node {
	int res;
	int pre;
	int digit;
} q[maxn];
int a[maxn], vis[maxn];
int n, m;

void print(int u) {
	if (q[u].pre == -1)
		return;
	print(q[u].pre);
	printf("%d", q[u].digit);
}

int bfs() {
	q[0].digit = 0;
	q[0].pre = -1;
	q[0].res = 0;
	memset(vis, 0, sizeof(vis));
	int front = 0, rear = 1;
	while (front < rear) {
		Node tmp = q[front];
		for (int i = 0; i < m; i++) {
			int t = (tmp.res * 10 + a[i]) % n;
			if (a[i] == 0 && tmp.pre == -1)
				continue;
			if (!vis[t]) {
				vis[t] = 1;
				Node tmp;
				tmp.digit = a[i];
				tmp.pre = front;
				tmp.res = t;
				q[rear++] = tmp;
			}
			if (t == 0) {
				print(rear-1);
				printf("\n");
				return 1;
			}
		}
		front++;
	}
	return 0;
}

int main() {
	while (scanf("%d%d", &n, &m) != EOF) {
		for (int i = 0; i < m; i++)
			scanf("%d", &a[i]);
		if (n == 0) {
			printf("0\n");
			continue;
		}
		sort(a, a+m);
		if (!bfs())
			printf("0\n");
	}
	return 0;
}
时间: 2024-12-22 04:35:59

ZOJ - 1136 Multiple (同余+BFS)的相关文章

poj 1465 &amp; zoj 1136 Multiple (BFS+余数重判)

Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6177   Accepted: 1346 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the small

ZOJ 1136 Multiple(BFS + 数论 同余剪枝 搜索数字的倍数 )

ZOJ Problem Set - 1136 Multiple Time Limit: 10 Seconds Memory Limit: 32768 KB a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the smallest strictly positive mult

ZOJ 1136 Multiple (BFS)

Multiple Time Limit: 10 Seconds      Memory Limit: 32768 KB a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the smallest strictly positive multiple of N that has

zoj 2913 Bus Pass (BFS)

Bus Pass Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The way the bus syst

zoj 3811 Untrusted Patrol(bfs或dfs)

Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safety of drinks, Edward hir

hdu4294 Multiple 数学结论 bfs

#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f3f3f3f #define ll __in

ZOJ 1136 Mutiple _BFS(同余定理判重)

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #define sc(x) scanf("%d",&x) 7 #define pf(x) printf("%d\n",x) 8 #define P printf("\n"

ZOJ 1671:Walking Ant【bfs】

Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometimes build their nests beneath flagstones. Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to h

ZOJ 3420 Double Maze (BFS)

链接 :  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3420 普通的BFS 两个图的状态表示成一个状态.记录答案直接用string保存操作. #include <iostream> #include <sstream> #include <cstring> #include <cstdio> #include <vector> #include <stac