题意:给一个数求该数的最小倍数的数且不含限定的数字,刚开始是不知道如果找不到的话限定到该数的多少倍 ,然后我就直接枚举到100倍TLE了,改成10倍又WA了。
五个小时训练过去后还是没有做出来,看题解才知道枚举时直接判断是否该数的模被枚举过了就可以了, 虽然有点不太理解是为什么,再好好想想吧。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;
struct node{
string s;
int x;
};
int vis[10050];
int num[12];
int n, m;
char ch[2];
void bfs()
{
queue<node> q;
node tmp, tpop;
ch[1] = '\0';
for (int i = 1; i < 10; i++)
{
if (num[i])
{
int m = i%n;
if (m == 0) {
cout << i << endl;
return;
}
vis[m] = 1;
tmp.x = i;
ch[0] = i + '0';
tmp.s = ch;
q.push(tmp);
}
}
while(!q.empty())
{
tpop = q.front(); q.pop();
for (int i = 0; i < 10; i++)
{
if (num[i])
{
int m = (tpop.x*10+i)%n;
//cout << m << endl;
if (m == 0) {
cout << tpop.s <<i << endl;
return;
}
if (!vis[m]) {
ch[0] = i + '0';
tmp.s = tpop.s + ch;
tmp.x = m;
q.push(tmp);
vis[m] = 1;
}
}
}
}
cout << "-1" << endl;
return;
}
int main()
{
//freopen("input.txt", "r", stdin);
int kcase = 0;
while (cin >> n >> m)
{
for (int i = 0; i < 10; i++)num[i] = 1;
for (int i = 0; i < m; i++)
{
int x;
scanf("%d", &x);
num[x] = 0;
}
printf("Case %d: ", ++kcase);
memset(vis, 0, sizeof(vis));
bfs();
}
return 0;
}