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. WINDOWS-API:操作网络映射盘-WNetAddConnection2

    首先在VC项目属性,开发依赖项里添加MPR.lib:然后,配置文件里填入以下信息.  //本地映射盘符 MapDriver=T: //目标根目录 //MapSharedPath=\\192.168.0 ...

  2. Pacman常用命令 文内搜索吧

    列出已经安装的软件包 https://wiki.archlinux.org/index.php/Pacman_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)  维基 pa ...

  3. BOM函数之history对象

    前面的话 history对象保存着用户上网的历史记录,从窗口被打开的那一刻算起.由于安全方面的考虑,开发人员无法得到用户浏览器的URL,但借由用户访问过的页面列表,可以在不知道实际URL的情况下实现后 ...

  4. jQuery-AJAX简介

    AJAX是浏览器后台与服务器交换数据的技术,无须加载整个页面的情况下,对页面中的局部进行更新. AJAX=异步的JavaScript与XML(Asynchronous JavaScript and X ...

  5. PAT 乙级 1041

    题目 题目地址:PAT 乙级 1041 题解 这道题学到的东西恰好和1037中学到的东西相互补充,总结如下: 在之前的博文中我曾提到过——“结构体在函数内部对数据的操作不能赋给主函数中的实参,函数内部 ...

  6. memory bist lib

    model NVIC_REG6T_1024x32(resetb,margin,clk,en,we,addr,d,q) ( bist_definition( clock clk high; chip_e ...

  7. sed快速学习

  8. spring,spring mvc,mybatis 常用注解

    文章来源:https://www.cnblogs.com/hello-tl/p/9209063.html  0.在spring,soring mvc, mybistis 中的常用注解有一下 <! ...

  9. python中datetime模块中datetime对象的使用方法

    本文只讲述datetime模块中datetime对象的一些常用的方法,如果读者需要更多datetime模块的信息,请查阅此文档. datetime模块的对象有如下: timedelta date da ...

  10. saltstack管理八之常用执行模块

    所有执行模块: http://docs.saltstack.cn/zh_CN/latest/ref/states/all/index.html 常用模块:cmd, cron, file, mount, ...