Yet Another Multiple Problem(同余方程+BFS)

SP12810

There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is highly challenging. That’s why we call it “Yet Another Multiple Problem”.
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
Input
There are several test cases.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 10
4). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.
Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) while Y is the minimal multiple satisfying the above-mentioned conditions or “-1” (without quotation marks) in case there does not exist such a multiple.
Sample Input
2345 3
7 8 9
100 1
0
Sample Output
Case 1: 2345
Case 2: -1

将合法的数位保存下来,然后将合法的数为拼接起来,判断是否是 n n n的倍数。
采用 B F S BFS BFS能保证搜索的数字是递增的,保证第一个找到的是最优解。
设两个数 A , B A,B A,B,若 A ≡ B ( m o d n ) A\equiv B\pmod n AB(modn)那么将数位 C C C拼接到 A , B A,B A,B后面,有 A C ≡ B C ( m o d n ) AC\equiv BC\pmod n ACBC(modn),简单的证明如下:

因为:
A C m o d    n = ( 10 ∗ A + C ) m o d    n = ( 10 ∗ A m o d    b + C m o d    n ) m o d    n = ( ( 10 m o d    n ) ( A m o d    n ) m o d    n + C m o d    n ) m o d    n AC\mod n\\=(10*A+C)\mod n\\=(10*A\mod b+C\mod n)\mod n\\=((10\mod n)(A\mod n)\mod n+C\mod n)\mod n ACmodn=(10A+C)modn=(10Amodb+Cmodn)modn=((10modn)(Amodn)modn+Cmodn)modn同理:
B C m o d    n = ( ( 10 m o d    n ) ( B m o d    n ) m o d    n + C m o d    n ) m o d    n BC\mod n\\=((10\mod n)(B\mod n)\mod n+C\mod n)\mod n BCmodn=((10modn)(Bmodn)modn+Cmodn)modn
A ≡ B ( m o d n ) A\equiv B\pmod n AB(modn),得: A C ≡ B C ( m o d n ) AC\equiv BC\pmod n ACBC(modn)
证毕。

因此如果有 A < B A<B A<B A ≡ B ( m o d n ) A\equiv B\pmod n AB(modn),那么 B B B就不用搜了,因为 B B B在拼接后的所有结果在模 n n n的结果都被 A A A搜索过了,因此可以做一个剪枝。

由于模 n n n的每一个结果都只搜索一次,事件复杂度为 O ( n ) O(n) O(n)

数为拼接后模数的更新:
A C m o d    n = ( 10 ∗ A + C ) m o d    n AC\mod n=(10*A+C)\mod n ACmodn=(10A+C)modn

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
using namespace std;

int n, m;
vector<int> Digits;
bool Vis[10005];

struct Node {
	int mod;
	string Number;
};

inline bool Input() {

	memset(Vis, false, sizeof(Vis));
	Digits.clear();

	bool No[10];
	memset(No, false, sizeof(No));
	if (scanf("%d%d", &n, &m) == EOF) {
		return false;
	}
	while (m--) {
		int x;
		scanf("%d", &x);
		No[x] = true;
	}
	for (int i = 0; i < 10; ++i) {
		if (No[i] == false) {
			Digits.push_back(i);
		}
	}
	return true;
}

void BFS() {
	queue<Node>Q;
	for (int i = 0; i < Digits.size(); ++i) {
		const int& digit = Digits[i];
		if (digit == 0) {
			continue;
		}
		Node node;
		node.mod = digit % n;
		node.Number = digit + '0';

		if (!node.mod) {
			cout << node.Number << endl;
			return;
		}
		Q.push(node);
	}

	while (!Q.empty()) {
		Node CurNode = Q.front();
		Q.pop();
		for (int i = 0; i < Digits.size(); ++i) {
			const int& digit = Digits[i];
			Node NewNode;
			string a;
			a = digit + '0';

			NewNode.Number = CurNode.Number + a;
			NewNode.mod = (CurNode.mod * 10 + digit) % n;
			if (!NewNode.mod) {
				cout << NewNode.Number << endl;
				return;
			}
			else if (Vis[NewNode.mod] == false) {
				Q.push(NewNode);
				Vis[NewNode.mod] = true;
			}
		}
	}
	cout << "-1" << endl;
	return;
}

int main() {
	int Case = 0;
	while (Input()) {

		cout << string("Case ") << ++Case << string(": ");
		BFS();
	}

	return 0;

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值