ZOJ 1136

ZOJ 1136 Multiple(BFS + 数论 同余剪枝 搜索数字的倍数 ) ZOJ Problem Set - 1136 Multiple Time Limit: 10 Seconds Memory Limit: 32768 KBa program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (a 阅读详情

广搜,每个节点有两个值,一个是当前的数字,另一个是余数,用余数可以减少以后的计算量,并且可进行剪枝,从而减少了常数时间的复杂度


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

struct Node {
	string num;
	int mod;
	Node() {}
	Node(string a, int b) {
		num = a;
		mod = b;
	}
};

int n, m;
int a[11];
int vis[50005];
string ans;

bool solve() {
	queue<Node> que;
	que.push(Node("", 0));
	memset(vis, 0, sizeof(vis));
	while(!que.empty()) {
		Node p = que.front();
		que.pop();
		for(int i = 0; i < m; i++) {
			Node tmp;
			tmp.num = p.num + (char)('0' + a[i]);
			tmp.mod = (p.mod*10 + a[i])%n;
			if(tmp.num != "0" && tmp.mod == 0) {
				ans = tmp.num;
				return true;
			}
			else if(vis[tmp.mod] == 0 && tmp.num != "0") {
				que.push(tmp);
				vis[tmp.mod] = 1;
			}
		}
	}
	return false;
}

int main() {
	while(scanf("%d%d", &n, &m) != EOF) {
		for(int i = 0; i < m; i++) {
			scanf("%d", &a[i]);
		}
		sort(a, a+m);
		if(0 == n) cout<< 0<< endl;
		else if(solve()) cout<< ans<< endl;
		else cout<< 0<< endl;
	}
	return 0;
}


AXI VIP配置全指南:从报错到成功仿真的完整流程(Vivado 2023版) 本文详细解析了在Vivado 2023环境中配置AXI Verification IP(VIP)进行协议仿真的完整流程。针对常见的include和import报错问题,提供了具体的解决方案和代码示例,指导用户从IP核创建、参数化配置,到构建可工作的仿真测试平台,最终实现成功仿真。 阅读详情

相关推荐

局域网tcp通信实验

IPv4 地址 . . . . . . . . . . . . : 192.168.92.79。IPv4 地址 . . . . . . . . . . . . : 192.168.92.59。子网掩码 . . . . . . . . . . . . : 255.255.255.0。子网掩码 . . . . . . . . . . . . : 255.255.255.0。使用这篇文章的小工具。

weixin_51883798的博客 409

zoj 1136(同余+bfs)

题意:求用m个数字组成n的倍数

一点一滴,从不止步! 1035

地级市低空经济企业数据(2000-2025年).xlsx

详细介绍及样例数据:https://blog.csdn.net/li514006030/article/details/156942899

zoj 1136 Multiple(bfs+数论+string)

题意:求N的最小的正整数倍数是否能由给出的M个整数给出。 分析:本题是1530的加强版。先将M个数排序,再广搜之。关键在与根据同余定理判重。凡是被搜过的接点都不必再搜。标记数组的大小不会超过5000因为N 还有的话就是存数,据说longlong能行,但这种题目还是用字符串来做合适点。 学习了下string用法。 #include //1136:关键是剪枝,方法1:运用同余可知

uiiの技术随笔 881

ZOJ 1136 Multiple( 搜索 BFS )

题目: 给一个数N,N>= 0, N 解析: 第一,分析可知,由于它是加的位数,而不是数字,所以,对于数一定要有处理,不可能盲目的追加位数! 第二,这m个数,根据题意,可以组成的数字是无穷多的 所以,这里需要一个解决的办法,那就是取余。事实上,任何一个数对n取余,一共只能有n个结果(如果算0的话)。而且有这样的一个式子,如果x%n == y%n, 那么x%n * 10 + c == y%

AClion的专栏 1238

zoj 1136 Multiple

借鉴别人的 主要是需要知道一点: 如果 a%n==b%n 那么根据题目要求只需要对min(a,b)进行继续的添加组合来查找即可! #include<iostream> #include<cstring> #include<algorithm> #include<math.h> #include<vector> #include<set> #include<queue> #include<list> #includ

ZekiDai的博客 231

ZOJ 1136 Multiple(分支界限算法)

Multiple Time Limit: 10 Seconds      Memory Limit: 32768 KB a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), f

H煊的博客 875

zoj1136

https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364635 #include <iostream> #include <map> #include <string.h> #include <string> #include <vector> #include <stdio.h> #include <algorithm> #include <

老李2009 246

ZOJ1136

比较简单,但被坑了好久,开始没用字符串,直接判定队列首元素大于2000000000,return 0..这题关键还是用mark数组记录余数达到优化。。搓的死,跑了500ms。#include #include #include #include #include #include using namespace std; int n,m; int p[12]; int nu[6000]; stru

终究我要华丽逆袭 1107

ZOJ 1136 Multiply

题目: 给一个数N,N>= 0, N 解析: 第一,分析可知,由于它是加的位数,而不是数字,所以,对于数一定要有处理,不可能盲目的追加位数! 第二,这m个数,根据题意,可以组成的数字是无穷多的 所以,这里需要一个解决的办法,那就是取余。事实上,任何一个数对n取余,一共只能有n个结果(如果算0的话)。而且有这样的一个式子,如果x%n == y%n, 那么x%n * 10 +

Ezereal的博客 487

zoj1136 Code

 #include #include #include #include #include using namespace std;struct item{       string s;       int r; //modulo n};int main(int argc, char *argv[]){    int n, m;    int d[10];    while (cin

yllfever的专栏 784

ZOJ-1136

核心思路就是BFS搜索,再加上对算过的余数

xhldtc的专栏 390

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

通过几道经典BFS例题阐述BFS思路 ZOJ2913-Bus Pass   题意:找一个center区域,使得center到所有公交线路最短,有等距的center则输出id最小的。   题解:经典的BFS,由公交线路最多只经过10*20个区域,而总区域数可达10^5个,因此应该从公交线路通过队列一层层向外扩展,最后判断一次center的位置即可。 ...

disuo8001的博客 288

zoj1136 Multiple

MultipleTime limit: 10 Seconds   Memory limit: 32768K   Total Submit: 2845   Accepted Submit: 764   a program that, given a natural number N between 0 and 4999 (inclusively), and M distinc

yllfever的专栏 774

ZOJ 1136 Multiple

Multiple Time Limit: 10 Seconds      Memory Limit: 32768 KB a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one)

ruangongshi的专栏 618

ZOJ 1136 Multiple(BFS)

#include #include #include #include #include #include #include using namespace std; const int maxn=5100; int n,m,a[12]; bool vis[maxn]; struct node { string num; int mod; node(string str=""

蚂蚁大战大象 693

ZOJ 1136 Longest Ordered Subsequence DP

传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1136 题目大意:给定一串序列,求最长的升序列长度,如1, 7, 3, 5, 9, 4, 8 最长的为 1, 3, 5, 8 输出4 进行DP,设dp [ i] 为 以 i 结尾的升序列的最大值,那么从 i 开始向前查找,若 a[ j ]

细语呢喃 891
上一篇: Linux一些重要的头文件
下一篇: ZOJ 1091 BFS
X-Wyatt
博客等级 码龄14年 45粉丝 140原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值