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

將合法的數位保存下來,然後將合法的數爲拼接起來,判斷是否是nn的倍數。
採用BFSBFS能保證搜索的數字是遞增的,保證第一個找到的是最優解。
設兩個數A,BA,B,若AB(modn)A\equiv B\pmod n那麼將數位CC拼接到A,BA,B後面,有ACBC(modn)AC\equiv BC\pmod n,簡單的證明如下:

因爲:
ACmod  n=(10A+C)mod  n=(10Amod  b+Cmod  n)mod  n=((10mod  n)(Amod  n)mod  n+Cmod  n)mod  nAC\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同理:
BCmod  n=((10mod  n)(Bmod  n)mod  n+Cmod  n)mod  nBC\mod n\\=((10\mod n)(B\mod n)\mod n+C\mod n)\mod n
AB(modn)A\equiv B\pmod n,得:ACBC(modn)AC\equiv BC\pmod n
證畢。

因此如果有A<BA<BAB(modn)A\equiv B\pmod n,那麼BB就不用搜了,因爲BB在拼接後的所有結果在模nn的結果都被AA搜索過了,因此可以做一個剪枝。

由於模nn的每一個結果都只搜索一次,事件複雜度爲O(n)O(n)

數爲拼接後模數的更新:
ACmod  n=(10A+C)mod  nAC\mod n=(10*A+C)\mod n

#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;

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章