1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 using namespace std; 5 int n; 6 bool v[210]; 7 unsigned long long bfs(int n) 8 { 9 memset(v,0,sizeof(v)); 10 queue<unsigned long long>que; 11 unsigned long long now,next; 12 13 now=1;v[1%n]=1; 14 que.push(now); 15 16 while (!que.empty()) 17 { 18 now=que.front(); 19 if (now%n==0) break; 20 que.pop(); 21 22 next=now*10; 23 if (!v[next%n]) 24 { 25 v[next%n]=1; 26 que.push(next); 27 } 28 29 next=now*10+1; 30 if (!v[next%n]) 31 { 32 v[next%n]=1; 33 que.push(next); 34 } 35 36 } 37 return now; 38 } 39 int main() 40 { 41 scanf("%d",&n); 42 while (n!=0) 43 { 44 printf("%lld\n",bfs(n)); 45 scanf("%d",&n); 46 } 47 return 0; 48 }
时间: 2024-11-10 21:41:56