首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > PKU1465 Multiple
【标  题】:PKU1465 Multiple
【关键字】:PKU1465,Multiple
【来  源】:http://www.cppblog.com/sicheng/archive/2007/01/15/17640.html

PKU1465 Multiple

Multiple
Time Limit:1000MS? Memory Limit:32768K
Total Submit:992 Accepted:221

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
思路:
设基数为X
首先 如果两个数对bs取模相等(为M) 则
A = aX + M
B = bX + M
那么只要其中一个可以取到结果 即
(10*(bX + M)+ C)MOD X = 0
也就是(10*M)MOD X?+ C MOD X = 0
则另一个必定也可以整除 X
这样在搜索的时候如果只需要搜索A (假设A<B) 就可以了
也就是说如果我们按照BFS扩展 把状态设置为MOD值 则只需要M的长度的队列就可以完成这个搜索了
无解的情况如何?自然就是无法扩展的情况。至于是否要优化最后一层(即搜索到了所有的非0 mod值 我认为没有必要)
注意不要把一个5000的字符串和其mod数封装在一起作为队列元素 会TLE的 呵呵
Solution:
//by Optmistic
#include <algorithm>
using namespace std;
const int N = 5010;
struct E{char num; int m; E * f;};
E q[N];
int cd[10], X;
bool chk[N];
void print(const E e) {
?if(e.f)?{
??print(*e.f);
??printf("%c", e.num);
?}
}
int main() {
?int i, nc;
?while(scanf("%d", &X)!=EOF) {
??scanf("%d", &nc);
??memset(chk, 0, sizeof(chk));
??for(i = 0; i<nc; i++)
???scanf("%d", cd+i);
??if(X == 0) {printf("0\n");continue;}
??sort(cd, cd+nc);
??int qs = 0, qe = 1;
??q[0].m = 0;
??q[0].f = NULL;
??E * first = &q[0];
??while(qs < qe) {
???E cur = q[qs];
???E now;
???now.f = &q[qs];
???int m = cur.m;
???for(i = 0; i< nc; i++) {
????int s = (10*m+cd[i])%X;
????if(!chk[s] && (now.f != first || cd[i] > 0) ) {
?????now.m = s;
?????now.num = cd[i]+'0';
?????q[qe++] = now;
?????chk[s] = 1;
?????if(s == 0) {
??????print(now);
??????putchar('\n');
??????goto HERE;
?????}
????}
???}
???qs++;
??}
HERE:??if(qs == qe) printf("0\n");
?}
?return 0;
}
写这个的时候发现一首很好听的歌
《No promises》
shayne ward

Hey baby, when we are together, doing things that we love.
Every time you're near I feel like I’m in heaven, feeling high
I don’t want to let go, girl.
I just need you to know girl.
I don’t wanna run away, baby you’re the one I need tonight,
No promises.
Baby, now I need to hold you tight, I just wanna die in your arms
Here tonight
Hey baby, when we are together, doing things that we love.
Everytime you're near I feel like I’m in heaven, feeling high
I don’t want to let go, girl.
I just need you you to know girl.
I don’t wanna run away, baby you’re the one I need tonight,
No promises.
Baby, now I need to hold you tight, I just wanna die in your arms
I don’t want to run away, I want to stay forever, thru Time and Time..
No promises
I don’t wanna run away, I don’t wanna be alone
No Promises
Baby, now I need to hold you tight, now and forever my love
No promises
I don’t wanna run away, baby you’re the one I need tonight,
No promises.
Baby, now I need to hold you tight, I just wanna die in your arms
I don’t wanna run away, baby you’re the one I need tonight,
No promises.
Baby, now I need to hold you tight, I just wanna die in your arms
Here tonight..

C++强制类型转换带来的一个安全隐患:【上一篇】
帮朋友发个招聘公告:【下一篇】
【相关文章】
  • Sql tricks: multiple rows in one output value
  • why gcc can link objs with multiple same symbols after ar?
  • jfreechart生成Multiple Pie Chart
  • Creating tablespaces with multiple block sizes
  • Oracle9i New Feature Series: Using Multiple Block
  • multiplexer protocol研究笔记
  • debian的postfix Hosting Multiple Domains (即是虚拟域)
  • multiple LUN on Linux
  • How to install multiple versions of GCC (zz)
  • Multiple actions in Sieve script.
  • 【随机文章】
  • 黑客技巧之利用图片做木马应用完全解析
  • "Argument list too long" 问题
  • Symantec企业用户如何部署安全中心
  • Matlab 模型的创建
  • 日文假名全半角转换空格删除
  • 转发:超搞笑,什么叫网关
  • 存储方案与存储产品之DAS篇
  • 用Delphi建立通讯与数据交换服务器—Transceiver技术剖析(下)
  • JediCodeFormat v0.65
  • Nokia系列手机上的一个手电筒J2ME程序(附源代码)
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.