HDU 4474 Yet Another Multiple Problem BFS
题意:求m的倍数中不包含一些数码的最小倍数数码是多少。比如15 ,不包含0 1 3,答案是45.
BFS过程:用b[]记录可用的数码。设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是所有可用的数码。这样从根到叶子节点这条路径所组成的数表示一个可行的数。
__ __
剪枝:(A % m == B % m) => (AX % m == BX % m) 即如果搜索到一个数X, X%m == a (a !=0) , 则以后如果搜索到Y , Y%m == a,则不必继续搜索。 这样一棵树是有尽头的。
所有这棵树的节点最多才m-1个。
这个剪枝在逻辑电路课上突然灵光突现,昨天模拟赛就怎么也想不来了,额,熬夜要变傻子了。 还有碰到两个陷阱,m==0的时候要特判。不能用节点来直接保存答案,long long 不够用,递归输出答案吧。
- #include <stdio.h>
- #include <string.h>
- //#include <queue>
- using namespace std;
- #define MAXN 100005
- int N,m;
- bool vis[MAXN];
- int pre[MAXN];
- int a[];
- int b[];
- int num[MAXN];
- int que[MAXN];
- int front,rear;
- void print(int x) //递归从前往后输出答案
- {
- if (pre[x] != - )
- print(pre[x]);
- printf("%d",num[x]);
- return;
- }
- int main()
- {
- int i,j,k,x,cas=;
- int casb,ans;
- while (scanf("%d%d",&N,&m)!=EOF)
- {
- printf("Case %d: ",++cas);
- int x;
- int casb=;
- memset(a, , sizeof(a));
- memset(b, , sizeof(b));
- memset(vis, , sizeof(vis));
- for (i=; i<m; i++)
- {
- scanf("%d",&x);
- a[x]=;
- }
- for (i = ; i <= ; i++)
- if (a[i] == )
- b[casb++]=i;
- if (N == ) //特判
- {
- if (b[] == )
- printf("0\n");
- else
- printf("-1\n");
- continue;
- }
- int flag=;
- int ans;
- front=;rear=;
- for (i = ; i < casb; i++) //最高位
- if (b[i]!=)
- {
- que[rear++]=b[i]%N;
- vis[b[i]%N]=true;
- num[rear-]=b[i];
- pre[rear-]=-;
- if (b[i] % N == )
- {
- ans=b[i];
- flag=;
- break;
- }
- }
- if (flag == )
- {
- printf("%d\n",ans);
- continue;
- }
- int tmp;
- ans=-;
- flag=;
- while (front != rear) //bfs
- {
- tmp=que[front];
- for (i = ; i < casb; i++)
- if (!vis[(tmp*+b[i]) % N]) //剪枝
- {
- que[rear]=(tmp*+b[i])% N;
- vis[(tmp*+b[i])% N ]=true;
- pre[rear] = front;//保存父节点
- num[rear] = b[i]; //保存本节点这个位数的数字
- if ((tmp*+b[i]) % N == )
- {
- print(rear);
- printf("\n");
- flag=;
- break;
- }
- rear++;
- }
- if (flag)
- break;
- front++;
- }
- if (flag == )
- printf("-1\n");
- }
- return ;
- }
HDU 4474 Yet Another Multiple Problem BFS的更多相关文章
- HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...
- HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】
Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- hdu 4474 Yet Another Multiple Problem
题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...
- Yet Another Multiple Problem(bfs好题)
Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other) Memory Limit : 65536/65536K ( ...
- hdu 4474 大整数取模+bfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...
- HDU-4471 Yet Another Multiple Problem (BFS+路径还原)
Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- hdu4474 Yet Another Multiple Problem
Yet Another Multiple Problem Description There are tons of problems about integer multiples. Despite ...
- HDU 1019 Least Common Multiple【gcd+lcm+水+多个数的lcm】
Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
随机推荐
- vim三种模式
一般模式 以vi打开一个文件就直接进入一般模式了.一般模式下可以移动光标查看内容,通过ESC回到一般模式. 一般模式下常用的操作: 撤销与重做 命令 说明 u 复原上一个操作 . 小数点 重复上一个操 ...
- Self-Attetion
四.self-attention 1.是什么? attention机制通常用在encode与decode之间,但是self-attention则是输入序列与输出序列相同,寻找序列内部元素的关系即 K= ...
- jquery动态生成二维码添加自定义logo
动态生成二维码中间带logo. jquery.qrcode.js 动态生成二维码api很简单. 引入jquer(版本任意),引入jquery.qrcode.js 不需要中间带logo这样就可以了.带l ...
- vue中的父组件及子组件生命周期的执行顺序
一.没有任何任何显示与隐藏限制条件的情况下: 1.运行的顺序依次是: 父组件created→父组件beforeMounted→子组件created→子组件beforeMounted→子组件mounte ...
- CentOS6.8 安装python2.7,pip以及yum
由于CentOS6.8里自带的yum所依赖的python是2.6.66版本,但是安装pip至少要求python是2.7版本,因而原有的2.6并不能卸载,又得安装新的2.7.之前安装的时候强制卸载了2. ...
- javascript 闭包笔记
先来解释一下闭包: 1.闭包就是函数嵌套函数 2.内部函数可以引用外部函数的参数和变量 3.参数和变量不会被垃圾回收机制所收回( 垃圾回收机制就是用完变量之后就在内存中释放 ) 使用闭包的好处: ...
- 清北学堂模拟赛d3t4 a
分析:很水的一道题,就是用栈来看看是不是匹配就好了,只是最后没有判断栈是否为空而WA了一个点,以后做题要注意了. #include <bits/stdc++.h> using namesp ...
- hdu 2527哈夫曼树(二叉树的运用)
#include<stdio.h> #include<string.h> #define N 100 #define INF 2000000000 int b[N]; c ...
- hdu_1065_I Think I Need a Houseboat_201311160023
I Think I Need a Houseboat Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- 学习webpack过程并上传到github
使用工具:sublimeText+node+git 1,一个包的文件结构,生成初始文件 在node 命令行窗口中创建demo_a文件夹 使用命令 npm init 初始化包,生成package.jso ...