基准时间限制: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
膜拜的文章:https://blog.csdn.net/Na_OH/article/details/63268662
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
typedef struct{
string str;
int val;
} node;
queue <node> q;
int n;
int vis[maxn];
void BFS()
{
if(n==1) {
cout<<n<<endl;
return;
}
node cur,nex;
cur.str="1";
cur.val=1;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
if(cur.val==0) {cout<<cur.str<<endl;return;}
nex.str=cur.str+"0";
nex.val= (cur.val*10)%n;
if(!vis[nex.val])
{
vis[nex.val]=1;
q.push(nex);
}
nex.str=cur.str+"1";
nex.val= (cur.val*10+1)%n;
if(!vis[nex.val])
{
vis[nex.val]=1;
q.push(nex);
}
}
}
int main(void)
{
cin>>n;
memset(vis,0,sizeof(vis));
BFS();
}