POJ-1426.Findthemultiple.(BFS)

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

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

  本题思路:BFS模拟即可。

  参考代码:

 #include <cstdio>
#include <queue>
using namespace std; int mod;
typedef long long LL; LL bfs() {
LL now = ;
queue <LL> s;
s.push(now);
while(!s.empty()) {
LL cur = s.front();
s.pop();
for(int i = ; i < ; i ++) {
LL next;
if(i == ) {
next = cur * + ;
} else next = cur * ;
if(next % mod == ) return next;
s.push(next);
}
}
return ;
} int main () {
while(~scanf("%d", &mod) && mod) {
LL ans = bfs();
printf("%lld\n", ans);
}
return ;
}

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

 #include <string>
#include <iostream>
using namespace std; int main () {
int mod;
string s;
while(cin >> mod >> s) {
int ans = ;
for(int i = ; i < s.length(); i ++)
ans = (ans * + s[i] - '') % mod;
cout << ans << endl;
}
return ;
}
上一篇:android 5.0 -- Activity 过渡动画


下一篇:mybatis缓存