题意:
给一个数n,让你找出一个只有1,0,组成的十进制数,要求是找到的数可以被n整除。
解析:
BFS+打表,队列用数组来模拟,如果直接用STL会超时。
AC代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int N = 1500000;
ll que[N];
ll dp[205];
ll bfs(int mod) {
int front, rear;
front = rear = 0;
que[rear++] = 1;
while(front < rear) {
ll tmp = que[front++];
if(tmp % mod == 0) return tmp;
que[rear++] = (tmp*10);
que[rear++] = (tmp*10+1);
}
return -1;
}
int main() {
for(int i = 1; i <= 200; i++) {
dp[i] = bfs(i);
}
int n;
while(cin >> n && n) {
cout << dp[n] << endl;
}
return 0;
}