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很大,那我 ...
随机推荐
- Android 跳转到系统应用管理
Intent i = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");String pkg = " ...
- VS2010调试技巧
最近合作开发,代码已经完成了,但是一调试,错误一大堆,由于是合作开发,不确定是哪层的错误,得一步步得走,很是费时费力,平时调试的技巧用的不多,现在集中调试,结果有些手忙脚乱,效率也很低,所以在网上找了 ...
- QlikView图表显示同比数据
数据准备例如以下: SalesData: LOAD Num(ID) as ID, Date(Date) as Date, Month, Num(Year) as Year, Num(Sales) as ...
- Kafka 配置说明
参考地址:http://my.oschina.net/infiniteSpace/blog/312890?p=1 server.properties中所有配置参数说明(解释) broker.id =0 ...
- 【RPC】Thrift ICE 等 RPC 框架相关资料
RPC框架-Thrift-ICE Apache Thrift - Documentation Apache Thrift - Index of tutorial/ Apache Thrift - Ab ...
- php -- 取路径:getcwd()、__DIR__、__FILE__ 的区别
getcwd() :显示是 在哪个文件里调用此文件 的目录 __DIR__ :当前内容写在哪个文件就显示这个文件目录 __FILE__ : 当前内容写在哪个文件就显示这个文件目录+文件名
- c语言基础,\r, \n, \r\n
Enumeration (or enum) in C Enumeration (or enum) is a user defined data type in C. It is mainly used ...
- WordPress 后台评论如何自定义搜索条件
大家都知道WordPress 作为一个非常成熟的博客系统,功能可以说是非常强大,几乎整个网站都可以进行定制开发,已经不算是一个博客系统了而应该是一个成熟的开发框架 最近就用WP给客户开发了一个网站,但 ...
- fatal error C1853: '<filename>' is not a precompiled header file
当编译c和c++混合的项目时,会出现如下类似的错误 fatal error C1853: '<filename>' is not a precompiled header file 解决方 ...
- idea丢失svn解决办法
今天打开Idea,习惯用ctrl+t来更新svn,杯具出现了,快捷键失效了,我觉得可能是其他的什么软件占用了这个快捷键,于是把qq,微信,rtx,各种软件都关掉,发现还是不好使,于是重启了一下,发现还 ...