id=1465">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 (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).

Input

The input 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.

Output

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:

Sample Input

22
3
7
0
1 2
1
1

Sample Output

110
0

Source

题意:

给出一个整数N。和M个0~9的数。求N的一个最小倍数,且该数仅由这M个数构成,不存在则输出0。

分析:

如果存在终于的数,一定能够写成A1*10^(k-1)+A2*10^(k-2)+...+Ak,Ai属于给出的M个数的集合。k有可能非常大。64位整数也可能存不下。

注意到最后的结果是N的倍数,如果结果是X,则有X%N=0,注意到结果的多项式形式,显然能想到使用同余定理。

我们须要从1位扩展到k位(当然要一步步来)。能够用BFS,用余数来记录状态(仅仅须要第一个),这样状态不超过N个。一旦余数为0,我们须要的结果就出来了。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm
#define maxn using namespace std; int X[10];
int n,m;
int q[5555];
int st[5555];
bool __hash[5555]; struct __node
{
int x,mod,fir;
}node[5555]; void write(int x)
{
int top=-1; for (;~x;x=node[x].fir)
st[++top]=node[x].x; while (top>=0)
printf("%d",st[top--]); puts("");
} void bfs()
{
int f=0,r=-1,cnt=0;
if (!n)
{
printf("%d\n",0);
return ;
}
memset(__hash,0,sizeof __hash);
for (int i=0;i<m;i++)
{
if (!X[i]) continue;
int mod=X[i]%n;
if (!mod)
{
printf("%d\n",X[i]);
return ;
}
if (__hash[mod]) continue;
__hash[mod]=true;
node[cnt]=(__node){X[i],mod,-1};
q[++r]=cnt;
cnt++;
} while (f<=r)
{
int x=q[f++];
for (int i=0;i<m;i++)
{
int mod=(node[x].mod*10+X[i])%n;
if (__hash[mod]) continue;
__hash[mod]=true;
node[cnt]=(__node){X[i],mod,x};
q[++r]=cnt;
if (!mod)
{
write(cnt);
return ;
}
cnt++;
}
} printf("0\n");
} int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/fcbruce/文档/code/t","r",stdin);
#endif // ONLINE_JUDGE while (~scanf("%d",&n))
{
scanf("%d",&m); for (int i=0;i<m;i++)
scanf("%d",X+i); sort(X,X+m); bfs();
} return 0;
}

POJ 1465 Multiple (BFS,同余定理)的更多相关文章

  1. HDU 1104 Remainder(BFS 同余定理)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...

  2. poj 1465 Multiple(bfs+余数判重)

    题意:给出m个数字,要求组合成能够被n整除的最小十进制数. 分析:用到了余数判重,在这里我详细的解释了.其它就没有什么了. #include<cstdio> #include<cma ...

  3. (简单) POJ 1426 Find The Multiple,BFS+同余。

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  4. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  5. [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

    The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   A ...

  6. POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)

    The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15767   A ...

  7. poj1061-青蛙的约会-(贝祖定理+扩展欧几里得定理+同余定理)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:132162   Accepted: 29199 Descripti ...

  8. SPOJ 370 Ones and zeros BFS + 同余剪枝

    题意:给一些n,求出最小的只包含0,1的n的倍数 设两数a, b满足: a < b 并且a % n = b % n. 如果 ( a * 10^x + c ) % n = z , 根据同余定理,( ...

  9. Light oj 1214-Large Division (同余定理)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我 ...

随机推荐

  1. DIV-CSS布局中position属性详解

    本文向大家描述一下DIV CSS布局中的position属性的用法,position属性主要有四种属性值,任何元素的默认position的属性值均是static,静态.这节课主要讲讲relative( ...

  2. [转]How to query posts filtered by custom field values

    Description It is often necessary to query the database for a list of posts based on a custom field ...

  3. 实战:INNOBACKUPEX for mysql 5.6自己主动还原脚本-v2

    脚本再次更新,共享一下! #!/bin/sh # # 用法: # ./restore.sh /你备份文件的全路径 #ocpyang@126.com INNOBACKUPEX=innobackupex ...

  4. (字符串)最长公共子序列(Longest-Common-Subsequence,LCS)

    问题: 最长公共子序列就是寻找两个给定序列的子序列,该子序列在两个序列中以相同的顺序出现,但是不必要是连续的. 例如序列X=ABCBDAB,Y=BDCABA.序列BCA是X和Y的一个公共子序列,但是不 ...

  5. 【网络文摘】Androidguy:当你的才华还无法撑起你的野心时,那么应该静下心来学习

    [序] 2013年12月初,接到51CTO的邀请写一篇关于挨踢人物传记的邀请.尽管写文章对我已不是什么新鲜事.数年来写的书和博客至少也在600万字以上.不过基本都是与技术有关的.写传记这还是头一次,也 ...

  6. MVC应用积累

    1.Controller中的跳转 (1)直接Redirect后加(Controller/Action):Response.Redirect("/Home/Index"); (2)直 ...

  7. HTTP和Socket的区别

    1: HTTP协议即超文本传送协议(Hypertext Transfer Protocol ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种应用. HTTP ...

  8. Uubntu14.04下 Hadoop集群环境搭建

    1机器信息 master192.168.3.10 hadoop/ hadoop slave1192.168.3.11 hadoop/ hadoop slave2192.168.3.12 hadoop/ ...

  9. 【剑指offer】二分查找二维数组

    1 2 3 4 5 6 7 8 9 3 3 1 2 3 4 5 6 7 8 9 10 3 3 12 2 3 4 5 6 7 8 9 10 例子输出: Yes No No 时间限制:1 秒 内存限制:3 ...

  10. How to Analyze Java Thread Dumps

    When there is an obstacle, or when a Java based Web application is running much slower than expected ...