您的位置:首页 > 其它

51nod 1109 01组成的N的倍数

2017-10-14 23:43 344 查看
1109 01组成的N的倍数

基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题


 收藏


 关注

给定一个自然数N,找出一个M,使得M > 0且M是N的倍数,并且M的10进制表示只包含0或1。求最小的M。

例如:N = 4,M = 100。

Input
输入1个数N。(1 <= N <= 10^6)


Output
输出符合条件的最小的M。


Input示例
4


Output示例
100


之前写过这类题目,就用广搜写了,然后WA了,想了一下应该是后来的数字超过了位数,连long long 都爆了,看了一下题解,dalao们都是用字符串+同余剪枝写的,学习了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
typedef long long ll;
struct Node{
string str;
ll num;
};
bool book[1000010];
int n;
void bfs(){
queue<Node> que;
memset(book,false,sizeof(book));
Node start;
start.str="1";
start.num=1;
book[start.num]=true;
que.push(start);
while(!que.empty()){
Node tmp=que.front();
que.pop();
if(tmp.num==0){
cout<<tmp.str<<endl;
return;
}
for(int i=0;i<2;i++){
Node end;
if(i==0){
end.str=tmp.str+"0";
end.num=(tmp.num*10)%n;
if(book[end.num]) continue;
book[end.num]=true;
que.push(end);
}
else{
end.str=tmp.str+"1";
end.num=(tmp.num*10+1)%n;
if(book[end.num]) continue;
book[end.num]=true;
que.push(end);
}
}
}
return;
}
int main(){
while(cin>>n){
if(n==1) cout<<1<<endl;
else bfs();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: