POJ Multiple (BFS,同餘定理)

來源:互聯網
上載者:User

標籤:algorithm   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

223701211

Sample Output

1100

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<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 maxnusing 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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.