您的位置:首页 > 其它

【搜索】 HDU 4474 Yet Another Multiple Problem

2015-10-03 20:27 507 查看
点击打开链接

题意 : 求一个最小的n的倍数 ans,使得ans不包括 输入的几个数字 ,不存在输出 -1

思路 :对于 两个数 A , B   存在  A > B  且 A%n = B%n   则  (A*10 + K ) %n  = (B*10 + K ) %n  然而 A*10 + K 仍然 大于 B*10 + K

所以就不需要搜 A 了  ,判断 余数 是否出现过 在剪枝

ans 会很大 所以用字符串模拟 

类似: HDU
1226

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <deque>
#include <set>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1<<29;
const LL mod = 1e9+7;
const int MAXN = 100100;//点数的最大值
const int MAXM = 520;//边数的最大值
const double pi = acos(-1);
int a[21213],m;
ULL n;
bool vis[22344];
struct node
{
string ans;
int mmm;
};
queue<node>q;
string gao()
{
while(!q.empty()) q.pop();
node fro;
fro.ans="";fro.mmm=0;
q.push(fro);
while(!q.empty())
{
node x=q.front();
q.pop();
for(int i=0;i<=9;i++)
{
if(a[i]) continue;
if(x.mmm==0&&i==0) continue;
fro.mmm=(x.mmm*10+i)%n;
if(vis[fro.mmm]) continue;
vis[fro.mmm]=1;
fro.ans=x.ans+(char)('0'+i);
if(fro.mmm==0)
return fro.ans;
q.push(fro);
}
}
return "-1";
}
int main()
{
int cas=0;
while(scanf("%I64u%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=0;i<m;i++)
{
int x;
scanf("%d",&x);
a[x]=1;
}
memset(vis,0,sizeof(vis));
string ans=gao();
printf("Case %d: ",++cas);
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: