Yet Another Multiple Problem congruence theorems bfs

  Meaning of the questions:

A given number of n and m

 

Seeking a minimum number of 1 to 2 n is not a multiple of any of the m numbers in a

 

123%n = ((((1%n)*10+2)%n)*10+3)%n

If a% n == b% n     

Then (a + x)% n == (b + x)% n

 

This can be pruned before modulo n appeared later on, do not appear to be a     

 

E.g  

A% n == B% n ​​and A <B then B can not process the

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define LL __int64
using namespace std;
 
const int MAXN = 11111;
 
struct Node
{
    vector <int> num;
    int c;
} tmp;
 
queue <Node> q;
 
int have[MAXN];
int vis[11];
 
int n;
 
void bfs()
{
    while(!q.empty()) q.pop();
    memset(have,0,sizeof(have));
    for(int i = 1;i < 10;i++)
        if(!vis[i])
        {
            if(have[i%n]) continue;
            tmp.num.clear();
            tmp.num.push_back(i);
            tmp.c = i%n;
            q.push(tmp);
            have[i%n] = 1;
        }
    while(!q.empty())
    {
        Node cur = q.front();
        if(cur.c == 0) break;
        q.pop();
        for(int i = 0;i < 10;i++)
        {
            if(vis[i]) continue;
            int tt = (cur.c*10+i)%n;
            if(!have[tt])
            {
                have[tt] = 1;
                tmp.num.clear();
                for(int j = 0;j < cur.num.size();j++)
                    tmp.num.push_back(cur.num[j]);
                tmp.num.push_back(i);
                tmp.c = tt;
                q.push(tmp);
            }
        }
    }
    if(!q.empty())
    {
        for(int i = 0;i < q.front().num.size();i++)
            printf("%d",q.front().num[i]);
        puts("");
    }
    else puts("-1");
}
 
int main()
{
    int m;
    int cas = 0;
    while(~scanf("%d%d",&n,&m))
    {
        memset(vis,0,sizeof(vis));
        for(int i = 0;i < m;i++)
        {
            int a;
            scanf("%d",&a);
            vis[a] = 1;
        }
        printf("Case %d: ",++cas);
        bfs();
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/bxd123/p/10994241.html