Yet Another Multiple Problem

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3407    Accepted Submission(s): 825

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
 
Source
 
Recommend
liuyiding
 

题意转自:http://blog.csdn.net/yang_7_46/article/details/12356587

题意:0-9这十个数字里面的若干个数字组合出一个数,使这个数是N的倍数,求最小的这个这样的数,不存在的话输出-1。

按照数的位数BFS,从小向大枚举就可以保证构造出来的数是递增的,如果不加判断就直接搜索的话,复杂度非常高。因此需要剪枝。

优化方法:如果一个数%N==0,那么这个数就是N的倍数。在没有找到的前提下,如果A%N==B%N,而且A<B,那么其实我们就可以取A而不取B,因为如果在A末尾增加C可以使得AC%N==0,那么BC%N也等于0,易得:如果A和B追加数之后%N==0,那么最优条件下追加的数肯定相同。

因此我们只需要维护组合出来的数%N的值即可,如果在搜索的途中出现了相同的%N值,就可以直接忽略了,因为肯定没有前面的优秀。

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string>
//#include<pair> #define N 10005
#define M 10005
#define mod 10000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,ans;
int m;
int vis[15];
int pre[N];
int val[N]; void ini()
{
int x;
ans=-1;
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
while(m--){
scanf("%d",&x);
vis[x]=1;
}
} void out(int x)
{
if(pre[x]){
out(pre[x]);
}
printf("%d",val[x]);
} void solve()
{
int i;
queue<int> q;
for(i=1;i<=9;i++){
if(vis[i]==1) continue;
if(i>=n && i%n==0){
printf("%d\n",i);
return;
}
if(pre[i%n]!=-1) continue;
pre[i%n]=0;
val[i%n]=i;
q.push(i);
}
while(q.size()>=1)
{
int te=q.front();
int nx;
q.pop();
for(i=0;i<=9;i++){
if(vis[i]==1) continue;
nx=(te*10+i)%n;
if(nx%n==0){
out(te);
printf("%d\n",i);
return;
}
if(pre[nx]!=-1) continue;
pre[nx]=te;
val[nx]=i;
q.push(nx);
}
}
printf("-1\n");
return;
} int main()
{
int cnt=1;
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
// scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%d%d",&n,&m)!=EOF)
{
//if(n==0 && k==0 ) break;
//printf("Case %d: ",ccnt);
printf("Case %d: ",cnt);
ini();
solve(); cnt++;
// out();
} return 0;
}

HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】的更多相关文章

  1. hdu 4474 Yet Another Multiple Problem

    题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...

  2. HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )

    没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...

  3. HDU 4474 Yet Another Multiple Problem BFS

    题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0  1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...

  4. HDU 4716 A Computer Graphics Problem 2013年四川省赛题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4716 题目大意不用解释了吧,看案例就能明白 #include<cstdio> #inclu ...

  5. hdu 4463 第37届ACM/ICPC杭州赛区K题 最小生成树

    题意:给坐标系上的一些点,其中有两个点已经连了一条边,求最小生成树的值 将已连接的两点权值置为0,这样一定能加入最小生成树里 最后的结果加上这两点的距离即为所求 #include<cstdio& ...

  6. HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online)

    HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online) 题目链接http://acm.hdu.edu.cn/showp ...

  7. 2012Chhengdu K - Yet Another Multiple Problem

    K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  8. HDU 1019 Least Common Multiple【gcd+lcm+水+多个数的lcm】

    Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  9. ACM hdu 1019 Least Common Multiple

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

随机推荐

  1. block总结我的

    1) struct Block_descriptor { unsigned long int reserved; unsigned long int size; void (*copy)(void * ...

  2. Linux文件操作函数

    creat() 函数 close() 函数 read() 函数 read 函数实际读到的字节数少于要求读的字节数时: 读普通文件,在读到要求字节数之前就到达文件尾: 当从终端设备读,通常一次最多读一行 ...

  3. Spring根据XML配置文件 p名称空间注入属性(property后出现,简便但只针对基本数据类型管用,自定义集合等引用类型无效)

    要生成对象并通过名称空间注入属性的类 代码如下: package com.swift; public class User { private String userName; public void ...

  4. 【动态规划】poj2353Ministry

    拓扑序……好些玄妙 Description Mr. F. wants to get a document be signed by a minister. A minister signs a doc ...

  5. 学习Python第一天,命令很多跟Linux还有脚本语言相似。

    学习Python第二天,看了一天,有点头疼,准备先休息一会,再继续.有一点C语言和Java基础,学起来不是很费劲.学习热情尚好. 学习了dir,math模块,import加载模块,有跟Linux相似的 ...

  6. django第11天(分页器)

    django第11天分页器 分页模块 批量插入数据 book_list = [] #先生成对象 for i in range(100): book = Book(name = 'book%s'%i,p ...

  7. web开发框架之DRF

    RESTful架构解释: # Representational State Transfer 表现层状态转化 到底什么是RESTFul架构: 如果一个架构符合REST原则,就称它为RESTful架构. ...

  8. Java线程和多线程(三)——线程安全和同步

    线程安全在Java中是一个很重要的课题.Java提供的多线程环境支持使用Java线程.我们都知道多线程共享一些对象实例的话,可能会在读取和更新共享数据的事后产生数据不一致问题. 线程安全 之所以会产生 ...

  9. 【SaltStack】SaltStack研究心得

    基础篇 ------------------------------------------------------------------------------------------------ ...

  10. 二分查找与 bisect 模块

    Python 的列表(list)内部实现是一个数组,也就是一个线性表.在列表中查找元素可以使用 list.index() 方法,其时间复杂度为O(n).对于大数据量,则可以用二分查找进行优化.二分查找 ...