HDU4474(数位BFS)

博客介绍了如何利用数位BFS策略,找到一个大于1且小于10000的数N的最小倍数X,这个X不包含特定的m个数字。通过分析n除以a的余数,并在n的末尾添加数字i来更新余数,当余数为0时回溯并输出结果。

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

大意:给一个数N(1<n<10000),求n的最小倍数x,使得x不包含m个给定的数字。

分析:如果n%a=m,则在n末尾加上i,新余数为(m*10+i)%a,按位搜索记录每添一个数字后的余数,余数为0则回溯输出结果。

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
#define N 10005
using namespace std;
bool vis[N], del[10];//记录已经访问的数字,不能使用的数字
int n, pre[N];//记录前置余数
char text[N];//记录输出
bool bfs()
{
	queue<int>q;
	q.push(0);
	int cur;
	while (!q.empty())
	{
		cur = q.front();
		q.pop();
		for (int i = 0; i < 10; i++)
		{
			if(del[i]==1||cur==0&&i==0)
				continue;
			int yu = (cur * 10 + i) % n;
			if(vis[yu])//出现相同余数保存之前较小的
				continue; 
			text[yu] = '0' + i;
			vis[yu] = 1;
			pre[yu] = cur;
			q.push(yu);
			if (yu == 0)
				return 1;
		}
	}
	return 0;
}
void print()
{
	string ans;
	int p = 0;
	while (p != 0 || ans.empty())
	{
		ans += text[p];
		p = pre[p];
	}
	reverse(ans.begin(), ans.end());
	cout << ans<<endl;
}
int main()
{
	int m, cas = 0;
	while (~scanf("%d%d", &n, &m))
	{
		memset(vis, 0, sizeof(vis));
		memset(del, 0, sizeof(del));
		while (m--)
		{
			int k;
			scanf("%d", &k);
			del[k] = 1;
		}
		printf("Case %d: ", ++cas);
		if (!bfs())
			printf("-1\n");
		else
			print();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值