hdu 4474 大整数取模+bfs

时间:2021-11-19 21:03:29

题目链接: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=): s(s),mod(mod) {}
}; bool can[];
bool vis[];
string ans; bool bfs(int n)
{
queue<Node> Q; string temp = "";
Q.push(Node(temp,)); while(!Q.empty())
{
Node node = Q.front();
Q.pop(); if(node.mod == && node.s != "" && node.s!="")
{
ans = node.s;
return true;
} for(int i=; i<=; i++)
{
if(!can[i] ) continue;
if(i == && node.s == "") continue;
int mod = (node.mod* + i)%n;
if(vis[mod]) continue; temp = node.s + char(i + '');
vis[mod] = true; Q.push(Node(temp,mod));
}
}
return false;
} int main()
{
//freopen("E:\\acm\\input.txt","r",stdin); int n,m;
int T = ;
while(cin>>n>>m)
{
for(int i=; i<=; i++) can[i] = true;
for(int i=; i<m; i++)
{
int a;
scanf("%d",&a);
can[a] = false;
}
ans = "";
memset(vis,,sizeof(vis));
printf("Case %d: ",++T); if(!bfs(n))
printf("-1\n");
else
cout<<ans<<endl;
}
}