hdu 4474 Yet Another Multiple Problem
2015-11-02 22:06
489 查看
Yet Another Multiple Problem
Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4962 Accepted Submission(s): 1087
Problem Description
There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is highly challenging. That’s why we call it “Yet Another Multiple Problem”.
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
Input
There are several test cases.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 104). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.
Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) while Y is the minimal multiple satisfying the above-mentioned conditions or “-1” (without quotation marks) in case there does not exist such a multiple.
Sample Input
2345 3
7 8 9
100 1
0
Sample Output
Case 1: 2345
Case 2: -1
题意:告诉一个数n,以及k个数字,求一个n的整数倍,并且这个倍数中不存在这k个数,求最小的倍数值。
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
typedef __int64 ll;
struct Node
{
int yu;
int digit;
int pre;
int id;
}f[1000009];
queue<int>q;
int use[20];
int vis[1000009];
ll n;
int k;
int cnt;
int ca;
void output(int id)//数字可能很大,递归输出
{
if(f[id].pre==-1)return;
output(f[id].pre);
printf("%d",f[id].digit);
}
int bfs()
{
f[0].yu=0;
f[0].pre=-1;
f[0].id=0;
q.push(0);
int t;
cnt=1;
while(!q.empty())
{
t=q.front();
q.pop();
for(int i=0;i<=9;i++)
{
if(use[i]) continue;
if(f[t].yu==0 && i==0) continue;
int yu=(f[t].yu*10+i)%n;
if(vis[yu]) continue;
if(yu==0)
{
printf("Case %d: ",ca);
output(t);
printf("%d\n",i);
return 1;
}
vis[yu]=1;
f[cnt].digit=i;
f[cnt].id=cnt;
f[cnt].yu=yu;
f[cnt].pre=t;
q.push(cnt++);
}
}
return -1;
}
int main()
{
ca=1;
while(~scanf("%I64d%d",&n,&k))
{
memset(vis,0,sizeof vis);
memset(use,0,sizeof use);
for(int i=0;i<k;i++)
{
int a;
scanf("%d",&a);
use[a]=1;
}
while(!q.empty()) q.pop();
int flag=bfs();
if(flag==-1)
{
printf("Case %d: ",ca);
puts("-1");
}
ca++;
}
return 0;
}
相关文章推荐
- 使用WebView 进行网络开发
- Lucene分词器测试
- ftp推送
- WPF 基础到企业应用系列3——WPF开发漫谈
- UIImageView
- 第十一章 存储过程和函数
- Fragment基础点滴学习---Fragment的生命周期
- opencv实现双目标定与校正
- sublime text2 之 lua环境的搭建
- 记录——《C Primer Plus (第五版)》第十章编程练习第五题
- Redis概念
- [leetcode 118]Pascal's triangle
- CGAffineTransform对View的缩放、平移和旋转
- 常用数据结构
- 超级好用的解析JSON数据的网站
- 05.Curator分布式锁
- 项目冲刺--第四天
- 软件工程第三次作业
- DDOs攻击-Java实现
- IsPostBack的用法