POJ-1426.Findthemultiple.(BFS)

一开始模拟了一波大数取余结果超时了,最后改成long long过了emmm...  

  本题大意:给出一个200以内的数n,让你找出一个m使得m % n == 0,要求m只有1和0组成。

  本题思路:BFS模拟即可。

  参考代码:

 1 #include <cstdio>
 2 #include <queue>
 3 using namespace std;
 4 
 5 int mod;
 6 typedef long long LL;
 7 
 8 LL bfs() {
 9     LL now = 1;
10     queue <LL> s;
11     s.push(now);
12     while(!s.empty()) {
13         LL cur = s.front();
14         s.pop();
15         for(int i = 0; i < 2; i ++) {
16             LL next;
17             if(i == 1) {
18                 next = cur * 10 + 1;
19             } else next = cur * 10;
20             if(next % mod == 0)  return next;
21             s.push(next);
22         }
23     }
24     return 0;
25 }
26 
27 int main () {
28     while(~scanf("%d", &mod) && mod) {
29         LL ans = bfs();
30         printf("%lld\n", ans);
31     }
32     return 0;
33 }
View Code

  顺便给出模拟大数取余的代码。

 1 #include <string>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main () {
 6     int mod;
 7     string s;
 8     while(cin >> mod >> s) {
 9         int ans = 0;
10         for(int i = 0; i < s.length(); i ++)
11             ans = (ans * 10 + s[i] - '0') % mod;
12         cout << ans << endl;
13     }
14     return 0;
15 }

 

posted @ 2019-03-06 14:22  Cruel_King  阅读(175)  评论(0编辑  收藏  举报