设为首页 加入收藏

TOP

POJ 1465 Multiple (BFS,同余定理)
2015-07-20 17:59:51 来源: 作者: 【 】 浏览:3
Tags:POJ 1465 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 (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

Southeastern Europe 2000


题意:

给出一个整数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
  
   
#include
   
     #include
    
      #include
     
       #include
      
        #include
       
         #include
        
          #include
         
           #include
          
            #include
           
             #include
            
              #include
             
               #include
              
                #include
                #include
                
                  #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
                 
                  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu 4893 Wow! Such Sequence! 下一篇HDU 1588 Gauss Fibonacci(矩阵..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: