例如:N = 4,M = 100。
4
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<string> using namespace std; const int maxn=1000005; struct node { string str; long long sum; } now,temp; queue<node>q; int vis[maxn]; int main() { int n; while(~scanf("%d",&n)) { while(!q.empty()) q.pop(); memset(vis,0,sizeof(vis)); now.str="1"; now.sum=1%n; vis[now.sum]=1; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); if(now.sum==0) { cout << now.str << endl; break; } temp.str=now.str+'0'; temp.sum=(now.sum*10)%n; if(vis[temp.sum]==0) { vis[temp.sum]=1; q.push(temp); } temp.str=now.str+'1'; temp.sum=(now.sum*10+1)%n; if(vis[temp.sum]==0) { vis[temp.sum]=1; q.push(temp); } } } return 0; }
100