Multiple
Time Limit: 1000MS   Memory Limit: 32768K
Total Submissions: 7183   Accepted: 1540

Description

a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the smallest strictly positive multiple of N that has no other digits besides X1,X2..XM (if such a multiple exists).

Input

The input has several data sets separated by an empty line, each data set having the following format:


On the first line - the number N

On the second line - the number M

On the following M lines - the digits X1,X2..XM.

Output

For
each data set, the program should write to standard output on a single
line the multiple, if such a multiple exists, and 0 otherwise.



An example of input and output:

Sample Input

22
3
7
0
1 2
1
1

Sample Output

110
0
题意:用下面所给的数字这些数构成的数字中找到是k的最小的倍数,每个数能用多次。
思路:bfs;
首先如果k为0的时候需要特判,然后将所给的数按升序排列,然后BFS的时候就能保证先被广搜到的数小。
然后这样会爆,所以需要剪枝。
我们要找的是k的倍数,那么这个数modk=0;然后看到k的范围很小。
假如两个数A,B;
A=n*k+s;B=m*k+s;
也就是A=B(modk);那么当这两数去搜下一层的时候,A*10+b[i];
B*10+b[i];那么这两个数对于k取余相等,所以我们只要将小的数加入队列。那么每个模数在队列中就出现一次就行,队列中的元素最多k个
还有这个数了可能很大,所以用静态数组记录前驱去找数。
C++超时,G++100多MS
  1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 #include <cmath>
5 #include <iostream>
6 #include <algorithm>
7 #include <map>
8 #include <queue>
9 #include <vector>
10 #include<set>
11 using namespace std;
12 typedef long long LL;
13 bool flag[6000];
14 int ans[20];
15 set<int>que;
16 set<int>::iterator it;
17 typedef struct pp
18 {
19 int mod;
20 int id;
21 int pre;
22 int digit;
23 pp()
24 {
25 pre=-1;
26 }
27 } ss;
28 ss bns[1000000];
29 int ask[1000000];
30 int cp[2000000];
31 int bfs(int n,int m);
32 int main(void)
33 {
34 int i,j,k;
35 while(scanf("%d",&k)!=EOF)
36 {
37 int n;
38 int m;
39 que.clear();
40 scanf("%d",&n);
41 for(i=0;i<n;i++)
42 {
43 scanf("%d",&cp[i]);
44 }
45 sort(cp,cp+n);
46 ans[0]=cp[0];
47 int uu=cp[0];
48 int t=1;
49 for(i=1;i<n;i++)
50 {
51 if(cp[i]!=uu)
52 {
53 ans[t++]=cp[i];
54 uu=cp[i];
55 }
56 }
57
58 if(k==0)printf("0\n");
59 else
60 {
61 int sum=0;
62 int id=bfs(t,k);
63 if(id==-1)
64 {
65 printf("0\n");
66 }
67 else
68 {
69 while(id!=-1)
70 {
71 ask[sum++]=bns[id].digit;
72 id=bns[id].pre;
73 }
74 for(i=sum-1; i>=0; i--)
75 {
76 printf("%d",ask[i]);
77 }
78 printf("\n");
79 }
80 }
81 }
82 return 0;
83 }
84 int bfs(int n,int m)
85 {
86 int i,j,k;
87 int kk=0;
88 memset(flag,0,sizeof(flag));
89 queue<ss>stc;
90 for(i=0; i<n; i++)
91 {
92 int mod=ans[i]%m;
93 if(!flag[mod]&&ans[i]!=0)
94 {
95 flag[mod]=true;
96 bns[kk].id=kk;
97 bns[kk].mod=mod;
98 bns[kk].pre=-1;
99 bns[kk].digit=ans[i];
100 stc.push(bns[kk]);
101 kk++;
102 }
103 }
104 while(!stc.empty())
105 {
106 ss tt=stc.front();
107 stc.pop();
108 for(i=0; i<n; i++)
109 {
110 int mod=(tt.mod*10+ans[i])%m;
111 if(!flag[mod])
112 {
113 bns[kk].id=kk;
114 bns[kk].pre=tt.id;
115 bns[kk].mod=mod;
116 bns[kk].digit=ans[i];
117 if(mod==0)
118 {
119 return kk;
120 }
121 stc.push(bns[kk]);
122 kk++;
123 flag[mod]=true;
124 }
125 }
126 }
127 return -1;
128 }
												

Multiple(poj1465)的更多相关文章

  1. ACM/ICPC 之 BFS范例(ZOJ2913-ZOJ1136(POJ1465))

    通过几道经典BFS例题阐述BFS思路 ZOJ2913-Bus Pass 题意:找一个center区域,使得center到所有公交线路最短,有等距的center则输出id最小的. 题解:经典的BFS,由 ...

  2. 【BFS】【余数剪枝】Multiple

    [poj1465]Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 7731   Accepted: 1723 ...

  3. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  4. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  5. [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  6. SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“

    最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...

  7. 2012Chhengdu K - Yet Another Multiple Problem

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

  8. JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher

    加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...

  9. scala - multiple overloaded alternatives of method bar define default arguments

    同名同位置默认参数不能overload def bar(i:Int,s:String="a"){} def bar(i:String,s:String="b") ...

随机推荐

  1. 零基础学习java------day10------带包编译,权限修饰符,内部类,调式,junitTest

    0.  带包编译 解决使用notepad++编写的java类中如果有package的解决方案,如下代码 package com._51doit.test; class HelloWorld{ publ ...

  2. 爬虫系列:存储 CSV 文件

    上一期:爬虫系列:存储媒体文件,讲解了如果通过爬虫下载媒体文件,以及下载媒体文件相关代码讲解. 本期将讲解如果将数据保存到 CSV 文件. 逗号分隔值(Comma-Separated Values,C ...

  3. Linux学习 - 文件特殊权限

    一.SUID权限(只针对文件) 只有可执行的二进制程序才能设定SUID权限 命令执行者要对该程序拥有x(执行)权限 1 拥有SUID的文件 /usr/bin/passwd 2 功能: 命令执行者(其他 ...

  4. Oracle trunc和round的区别

    1.关于trunc 和round函数比较 整体概括: round函数 四舍五入trunc函数 直接截取 对于时间: Round函数对日期进行"四舍五入",Trunc函数对日期进行截 ...

  5. Linux基础命令---slabtop

    slabtop slabtop实时显示详细的内核板条缓存信息.它显示按所列排序条件之一排序的顶级缓存的列表.它还会显示一个统计信息头,其中填充了板坯层信息. 此命令的适用范围:RedHat.RHEL. ...

  6. OSGI 生命周期

    1 生命周期管理 对于非模块化应用,生命周期将应用作为一个整体来操作: 而对于模块化应用,则可以以细粒度的方式来管理应用的某一个独立部分. OSGi生命周期管理 OSGi生命周期层有两种不同的作用: ...

  7. UIButton总结

    UIButton 1. 功能 既能显示文字,又能显示图片(能显示2张图片,背景图片.内容图片) 长按高亮的时候可以切换图片\文字 直接通过addTarget...方法监听点击 2. 状态 normal ...

  8. Linux 网卡配置文件,命令详细设置

    1.配置文件/etc/hosts(本地主机ip地址映射,可以有多个别名)./etc/services(端口号与标准服务之间的对应关系)./etc/sysconfig/network(设置主机名,网关, ...

  9. 安装本地jar包到仓库

    1. 下载并解压 sdk 包本地文件夹下 2. 进入项目目录 执行以下操作之前,先确定 maven 的 settings 文件中 配置的 仓库地址是否为本项目的 仓库地址,如果不是,则会安装到其他仓库 ...

  10. Echarts 实现tooltip自动显示自动播放

    1.其实这个很容易实现,一个 dispatchAction 方法就解决问题:但是博主在未实现该功能时是花了大力气,各种百度,各种搜: 很难找到简单粗暴的例子,大多数随便回一句你的问题就没下文: 废话太 ...