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 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的倍数。

思路:bfs 输出的时候相当于遍历路径一边,所以有pre.

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);
#define maxn 5000

struct Node
{
    int num, pre, id, yu;
};
Node node[maxn];
int vis[maxn];
int n, m;
int a[maxn];
void output(int id)
{
    if(node[id].pre == -1)
        return;
    output(node[id].pre);
    printf("%d", node[id].num);
}
void bfs()
{
    memset(vis, 0, sizeof vis);
    node[0].id = 0;
    node[0].pre = -1;
    node[0].yu = 0;
    int cnt = 1;
    queue<int> q;
    q.push(0);
    while(!q.empty())
    {
        int id = q.front();
        q.pop();
        for(int i = 0; i < m; i++)
        {
            if(node[id].yu == 0 && a[i] == 0)
                continue;
            int yu = (node[id].yu*10+ a[i])%n;
            if(!vis[yu])
            {
                if(yu == 0)
                {
                    output(id);
                    printf("%d\n", a[i]);
                    return;
                }
                vis[yu] = 1;
                node[cnt].pre = id;
                node[cnt].num = a[i];
                node[cnt].yu = yu;
                node[cnt].id = cnt;
                q.push(cnt++);
            }
        }
    }
    printf("%d\n", 0);
}
int main()
{
    while(~scanf("%d",&n))
    {
        scanf("%d", &m);
        for(int i = 0; i < m; i++) scanf("%d", &a[i]);
        sort(a, a + m);
        if(n == 0)
            printf("%d\n", 0);
        else
            bfs();
    }
    return 0;
}
时间: 2024-12-25 00:28:19

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)

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 mu

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

POJ Multiple (BFS,同余定理)

http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6164   Accepted: 1339 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM

HDU 1026 Ignatius and the Princess I (基本算法-BFS)

Ignatius and the Princess I Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem si

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

[ACM] hdu 1253 胜利大逃亡 (三维BFS)

胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出