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 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

Source: Southeastern Europe 2000

Submit Status

主要是剪枝:数论的剪枝,直接用例子解释,比如36%24==12==60%24,那么36*****%24==60******%24

比如 368%24==608%24,所以在搜索的时候,如果已经搜索到36%24==12,那么遇到60时可以直接跳过


#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<map>
#include<stack>
#include<algorithm>
#include<queue>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define LL long long
const int maxn=100002;
const int inf=9999999;
int n,m;
int a[maxn];
int mod[maxn];
int getMode(string s){
    int len=s.size();
    int ans=0;
    for(int i=0;i<len;i++){
        ans=(ans*10+s[i]-‘0‘)%n;
    }
    return ans;
}
string bfs(){
    cl(mod,0);
    queue<string> q;
    for(int i=0;i<m;i++){//初始化队列的起点
        q.push(string(1,a[i]+‘0‘));//string(number,char)构造函数
    }
    while(!q.empty()){
        string s=q.front();q.pop();
        if(s!="0"&&getMode(s)==0){
            return s;
        }
        for(int i=0;i<m;i++){
            string tmp=s;
            if(tmp=="0"&&a[i]==0)continue;//特判,防止出现00000...的情况
            tmp+=string(1,a[i]+‘0‘);
            int d=getMode(tmp);
            if(mod[d]++)continue;//出现同余,直接跳过
            q.push(tmp);
        }
    }
    return "0";
}

int main(){

    while(~scanf("%d",&n)){
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            scanf("%d",&a[i]);
        }
        if(n==0){
            printf("0\n");continue;
        }
        sort(a,a+m);
        printf("%s\n",bfs().c_str());
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 06:51:11

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)

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

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

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

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

zoj 1649 Rescue (bfs+队列)

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want

数论 --- 同余定理

声明:以下文章是借鉴了别人的再加上自己补充后的,转载请注明! 一.同余 对于整数除以某个正整数的问题,如果只关心余数的情况,就产生同余的概念. 定义1 用给定的正整数m分别除整数a.b,如果所得的余数相等,则称a.b对模m同余,记作a≡b(mod m),如 56≡0 (mod 8). 举个例子: 3%2=1 5%2=1 则有: (5-3)%=0 [同余性质] 1) 自反性 2) 传递性 3) 对称性 4) 若 a ≡ b (mod m), c ≡ d (mod m) 则 a+b ≡ b+ d(m

Codeforces 542D Superhero&#39;s Job 数论 哈希表 搜索

原文链接https://www.cnblogs.com/zhouzhendong/p/CF542D.html 题目传送门 - CF542D 题目传送门 - 51Nod1477 题意 定义公式 $J(x) = \sum_{1 \leq k \leq x 且 k|x 且 \gcd (k,x/k) = 1} k$ . 现在给定一个整数 $A$ ,要求有多少正整数 $x$ ,满足 $J(x)=A$ . $x|n$ 表示 $x$ 是 $n$ 的因子. $\gcd(a,b) 表示 $a$ 和 $b$ 的最大

ZOJ 3644 Kitty&#39;s Game dfs,记忆化搜索,map映射 难度:2

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的因数,所以不会形成环,只需从1直接走,走到n即可. 但是如果这样的话时空复杂度就都是nk,明显不满足题意,而这个时候我们可以想到,每个状态都必然是k的约数,(点数不是k的约数的节点不在路上,可以无视),而约数的个数也就k^0.5个,可以直接用map映射,这样时空复杂度都是n*k^0.5,可以解出答案