思路:
和几天前集训的题一模一样。
上次打深搜,这次打广搜
c o d e code code
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int n;
queue<pair<int, string> > q;
bool v[1000010];
int main() {
scanf("%d", &n);
q.push(make_pair(1, "1"));
v[1] = 1;
while(!q.empty()) {
int r = q.front().first;
string ss = q.front().second;
q.pop();
int g = r * 10 % n;
if(!v[g]) {
v[g] = 1;
q.push(make_pair(g, ss + "0"));
if(g == 0) {
cout<<ss + "0";
return 0;
}
}
g = (r * 10 + 1) % n;
if(!v[g]) {
v[g] = 1;
q.push(make_pair(g, ss + "1"));
if(g == 0) {
cout<<ss + "1";
return 0;
}
}
}
cout<<-1;
return 0;
}