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很大,那我 ...
随机推荐
- 【反射】Reflect Class Field Method Constructor
关于反射 Reflection 面试题,什么是反射(反射的概念)? 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义 ...
- Android GUI之View事件处理
Android中的事件分为按键事件和触屏事件,本篇文章将分析View是如何处理Touch事件的.在View中定义了许多触屏事件,比如OnClick.OnLongClick等等,这些事件都是由一次To ...
- IOS 设置圆角用户头像
在App中有一个常见的功能,从系统相册或者打开照相机得到一张图片,然后作为用户的头像.从相册中选取的图片明明都是矩形的图片,但是展示到界面上却变成圆形图片,这个神奇的效果是如何实现的呢? 请大家跟着下 ...
- android 中的 window,view,activity具体关系
通过讨论这个问题,我们能够见识到google是对面向对象模式的理解,能够理解android底层的一些调用.这也是一道很常见的面试题. 我们这篇文章就来解决这四个问题: Android 中view的显 ...
- 【Python】Django删除数据迁移记录
find . -path "*migrations*" -name "*.py" -not -path "*__init__*" -exec ...
- 解决PHP在Windows IIS 上传的图片无法访问的问题
最近在做一个网站项目遇到了一个很奇怪的问题,现记录下来希望可以帮助到其他的朋友 问题描述: 最近公司刚刚在香港购买了一个Windows Server 2008 服务器用于将一个客户的N个php网站 ...
- 线性表 顺序存储 链式存储 ---java实现
首先抽象出一个线性表抽象类(包括主要的增删操作) public abstract class MyAbstractList<E> { public abstract void add(E ...
- 页面回到顶部的三种实现(锚标记,js)
一.使用锚标记返回页面顶部 使用HTML锚标记最简单,就是看起来有点不好看,点击后会在地址栏显示这个锚标记,其它的倒没什么. 页面顶部放置: <a name="top" id ...
- MySQL工具1:mysqladmin
每两秒显示一下MySQL的状态,一共显示5次. # mysqladmin -uroot -p -i 2 -c 5 status 查看MySQL的运行状态: #mysqladmin -uroot -p ...
- Oracle使用——PLSQL的中文乱码显示全是问号
问题 这两天刚将PLSQL与Oracle配置好,但是在PLSQL中插入数据时.出现一个问题,PLSQL中的表里无法显示中文,中文无法保存.无法输出.中文在表中显示问号.如图: 原因 经过一番查证,发现 ...