POJ 1465 Multiple (BFS,同余定理)
id=1465">http://poj.org/problem?id=1465
Multiple
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 Sample Output 110 Source |
题意:
给出一个整数N。和M个0~9的数。求N的一个最小倍数,且该数仅由这M个数构成,不存在则输出0。
分析:
如果存在终于的数,一定能够写成A1*10^(k-1)+A2*10^(k-2)+...+Ak,Ai属于给出的M个数的集合。k有可能非常大。64位整数也可能存不下。
注意到最后的结果是N的倍数,如果结果是X,则有X%N=0,注意到结果的多项式形式,显然能想到使用同余定理。
我们须要从1位扩展到k位(当然要一步步来)。能够用BFS,用余数来记录状态(仅仅须要第一个),这样状态不超过N个。一旦余数为0,我们须要的结果就出来了。
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm
#define maxn using namespace std; int X[10];
int n,m;
int q[5555];
int st[5555];
bool __hash[5555]; struct __node
{
int x,mod,fir;
}node[5555]; void write(int x)
{
int top=-1; for (;~x;x=node[x].fir)
st[++top]=node[x].x; while (top>=0)
printf("%d",st[top--]); puts("");
} void bfs()
{
int f=0,r=-1,cnt=0;
if (!n)
{
printf("%d\n",0);
return ;
}
memset(__hash,0,sizeof __hash);
for (int i=0;i<m;i++)
{
if (!X[i]) continue;
int mod=X[i]%n;
if (!mod)
{
printf("%d\n",X[i]);
return ;
}
if (__hash[mod]) continue;
__hash[mod]=true;
node[cnt]=(__node){X[i],mod,-1};
q[++r]=cnt;
cnt++;
} while (f<=r)
{
int x=q[f++];
for (int i=0;i<m;i++)
{
int mod=(node[x].mod*10+X[i])%n;
if (__hash[mod]) continue;
__hash[mod]=true;
node[cnt]=(__node){X[i],mod,x};
q[++r]=cnt;
if (!mod)
{
write(cnt);
return ;
}
cnt++;
}
} printf("0\n");
} int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/fcbruce/文档/code/t","r",stdin);
#endif // ONLINE_JUDGE while (~scanf("%d",&n))
{
scanf("%d",&m); for (int i=0;i<m;i++)
scanf("%d",X+i); sort(X,X+m); bfs();
} return 0;
}
POJ 1465 Multiple (BFS,同余定理)的更多相关文章
- HDU 1104 Remainder(BFS 同余定理)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...
- poj 1465 Multiple(bfs+余数判重)
题意:给出m个数字,要求组合成能够被n整除的最小十进制数. 分析:用到了余数判重,在这里我详细的解释了.其它就没有什么了. #include<cstdio> #include<cma ...
- (简单) POJ 1426 Find The Multiple,BFS+同余。
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11978 A ...
- POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15767 A ...
- poj1061-青蛙的约会-(贝祖定理+扩展欧几里得定理+同余定理)
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions:132162 Accepted: 29199 Descripti ...
- SPOJ 370 Ones and zeros BFS + 同余剪枝
题意:给一些n,求出最小的只包含0,1的n的倍数 设两数a, b满足: a < b 并且a % n = b % n. 如果 ( a * 10^x + c ) % n = z , 根据同余定理,( ...
- Light oj 1214-Large Division (同余定理)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我 ...
随机推荐
- assets raw 资源 AssetManager
assets raw 目录简介 assets核心特性:不会被编译成二进制,支持子目录(可以分类,这是相对raw目录最大的好处),通过文件名访问,调用getAssets通过AssetManager访问 ...
- js list数据 转 树状 层级 JSON,递归生成树状 层级 JSON
<!DOCTYPE html> <html> <head> <script> var data=[ {"id":"aaa& ...
- jQuery用面向对象的思想来编写验证表单的插件
本人的重点是怎么构建一个简单有效可扩展的jQuery表单验证插件,这篇文章没有教你怎么用 validate plugin.我们的重点在学习一些jQuery,Javascript面向对象编程的知识. 下 ...
- 【ES】elasticsearch学习笔记
ES学习 1 优势 1.1 简单 1.1.1 相比Solor配置部署等非常简单 1.2 高效 1.2.1 ES使用Netty作为内部RPC框架,Solor使用Jetty 1.3 插件化 1.3.1 E ...
- IOS NSString 用法详解
[cpp] view plain copy //NSString 操作均不改变自身值 //构建字符串 NSString *szTmp = @"A string"; ...
- kettle根据参数动态派生列
抽取数据的时候没有日期字段,需要根据抽取日期自动生成月份,如下图结构 表输入_参数部分,接收来自其他系统传过来的参数(JAVA程序或者页面),具体设置如图 在查询数据时候派生列 运行模型的时候,给参数 ...
- 如何在程序中使用CString
在新建项目的时候,如果选择了MFC并且使用ATL,那么在程序中使用CString是没有问题的. 但是如果当初没有选,后面再改,虽然选上了,但是CString在编译的时候还是不被编译器识别.怎么办那? ...
- 宏定义偷懒型set,get
之前看到有这么一个写法 #define DEF_SET_GET(varType,varName,funName) \ private : varType varName; \ public : voi ...
- 【python】理想论坛帖子爬虫1.06
昨天认识到在本期同时起一百个回调/线程后程序会崩溃,造成结果不可信. 于是决定用Python单线程操作,因为它理论上就用主线程跑不会有问题,只是时间长点. 写好程序后,测试了一中午,210个主贴,11 ...
- Eclipse.ini參数设置(Maven Integration for Eclipse JDK Warning)
安装EclipseMaven插件后,Eclipse启动问题:Maven Integration for Eclipse JDK Warning. 解决方法: 1. 设置Eclipse使用的JRE为本 ...