【搜索】 HDU 4474 Yet Another Multiple Problem

本文介绍了一个算法问题的解决方法,该问题要求找到一个最小的n的倍数,该倍数不能包含某些特定的数字。通过使用字符串模拟和剪枝策略,避免了不必要的搜索,从而高效地找到了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接

题意 : 求一个最小的n的倍数 ans,使得ans不包括 输入的几个数字 ,不存在输出 -1

思路 :对于 两个数 A , B   存在  A > B  且 A%n = B%n   则  (A*10 + K ) %n  = (B*10 + K ) %n  然而 A*10 + K 仍然 大于 B*10 + K

所以就不需要搜 A 了  ,判断 余数 是否出现过 在剪枝

ans 会很大 所以用字符串模拟 

类似: HDU 1226

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <deque>
#include <set>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1<<29;
const LL mod = 1e9+7;
const int MAXN = 100100;//点数的最大值
const int MAXM = 520;//边数的最大值
const double pi = acos(-1);
int a[21213],m;
ULL n;
bool vis[22344];
struct node
{
    string ans;
    int mmm;
};
queue<node>q;
string gao()
{
    while(!q.empty()) q.pop();
    node fro;
    fro.ans="";fro.mmm=0;
    q.push(fro);
    while(!q.empty())
    {
        node x=q.front();
        q.pop();
        for(int i=0;i<=9;i++)
        {
            if(a[i]) continue;
            if(x.mmm==0&&i==0) continue;
            fro.mmm=(x.mmm*10+i)%n;
            if(vis[fro.mmm]) continue;
            vis[fro.mmm]=1;
            fro.ans=x.ans+(char)('0'+i);
            if(fro.mmm==0)
                return fro.ans;
            q.push(fro);
        }
    }
    return "-1";
}
int main()
{
    int cas=0;
    while(scanf("%I64u%d",&n,&m)!=EOF)
    {
        memset(a,0,sizeof(a));
        for(int i=0;i<m;i++)
        {
            int x;
            scanf("%d",&x);
            a[x]=1;
        }
        memset(vis,0,sizeof(vis));
        string ans=gao();
        printf("Case %d: ",++cas);
        cout<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值