给定一个自然数N,找出一个最小的膜法二进制M满足M除以N的余数为0
例如:N = 4,M = 100
bfs + 剪枝
#include <cstdio> #include <cstring> #include <string.h> #include <algorithm> #include <iostream> #include <queue> #include <cstring> #include <stack> #define MAX_N 2000005 using namespace std; int n; int vised[MAX_N]; struct node { string a; int num; }now,nex; void bfs(){ memset(vised,0,sizeof(vised)); queue<node> s; now.a="10"; now.num=10%n; vised[now.num]=1; s.push(now); now.a="11"; now.num=11%n; vised[now.num]=1; s.push(now); while(!s.empty()){ now=s.front(); if(now.num==0) cout<<now.a<<endl; s.pop(); for(int i=0;i<=1;i++){ if(i==0) nex.a=now.a+'0'; else nex.a=now.a+'1'; nex.num=(now.num*10+i)%n; if(vised[nex.num]==1) continue; vised[nex.num]=1; s.push(nex); } } } int main() { while(cin>>n) { if(n==1) { printf("1"); continue; } else bfs(); } }