标签:
Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4734 Accepted Submission(s): 1021
解题:余数相同的情况下保存数位值较小的
因为两者同样都有可能成为n的倍数,但是我们要求的正是最小的
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 10010; 4 int n,m,pre[maxn],num[maxn]; 5 bool bad[maxn]; 6 void out(int u){ 7 if(pre[u] != -1) out(pre[u]); 8 printf("%d",num[u]); 9 } 10 void bfs() { 11 queue<int>q; 12 for(int i = 1; i < 10; ++i) { 13 if(bad[i]) continue; 14 int t = i%n; 15 if(!t) { 16 printf("%d",i); 17 return; 18 } 19 num[t] = i; 20 q.push(t); 21 } 22 while(!q.empty()) { 23 int u = q.front(); 24 q.pop(); 25 for(int i = 0; i < 10; ++i) { 26 if(bad[i]) continue; 27 int t = (u*10 + i)%n; 28 if(num[t] == -1) { 29 num[t] = i; 30 pre[t] = u; 31 q.push(t); 32 } 33 if(!t) { 34 out(t); 35 return; 36 } 37 } 38 } 39 printf("-1"); 40 } 41 int main() { 42 int kase = 1; 43 while(~scanf("%d%d",&n,&m)) { 44 memset(pre,-1,sizeof pre); 45 memset(bad,false,sizeof bad); 46 memset(num,-1,sizeof num); 47 while(m--) { 48 int tmp; 49 scanf("%d",&tmp); 50 bad[tmp] = true; 51 } 52 printf("Case %d: ",kase++); 53 bfs(); 54 putchar(‘\n‘); 55 } 56 return 0; 57 }
HDU 4474 Yet Another Multiple Problem
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4776844.html