题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474
(a*10+b)%c = ((a%c)*10+b%c)%c;
然后从高位开始枚举能填的数字填充,只是注意最高位(第一位)不能为0。
代码:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<string> #include<queue> using namespace std; struct Node { string s; int mod; Node(string s="",int mod=0): s(s),mod(mod) {} }; bool can[15]; bool vis[10050]; string ans; bool bfs(int n) { queue<Node> Q; string temp = ""; Q.push(Node(temp,0)); while(!Q.empty()) { Node node = Q.front(); Q.pop(); if(node.mod == 0 && node.s != "" && node.s!="0") { ans = node.s; return true; } for(int i=0; i<=9; i++) { if(!can[i] ) continue; if(i == 0 && node.s == "") continue; int mod = (node.mod*10 + i)%n; if(vis[mod]) continue; temp = node.s + char(i + '0'); vis[mod] = true; Q.push(Node(temp,mod)); } } return false; } int main() { //freopen("E:\acm\input.txt","r",stdin); int n,m; int T = 0; while(cin>>n>>m) { for(int i=0; i<=9; i++) can[i] = true; for(int i=0; i<m; i++) { int a; scanf("%d",&a); can[a] = false; } ans = ""; memset(vis,0,sizeof(vis)); printf("Case %d: ",++T); if(!bfs(n)) printf("-1 "); else cout<<ans<<endl; } }