Yet Another Multiple Problem 同余定理 bfs

  题意:

给出一个n和m个数

 

求一个最小的数 1 为n的倍数  2 没有这m个数字中的任意一个

 

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

如果     a%n==b%n     

那么    (a+x)%n==(b+x)%n

 

这样就可以剪枝了   之前取模n出现过的后来再出现就可以不要了     

 

例如  

A%n==B%n 且 A<B 那么B就不用处理了

#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

 

转载于:https://www.cnblogs.com/bxd123/p/10994241.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值