Codeforces 251C Number Transformation DP, 记忆化搜索,LCM,广搜
题意及思路:https://blog.csdn.net/bossup/article/details/37076965
代码:
#include <bits/stdc++.h>
#define LL long long
#define INF 1e18
using namespace std;
int lcm(int x, int y) {
return x * y / __gcd(x, y);
}
const int maxn = 500010;
LL a, b, k;
LL dp[maxn];
unordered_map<LL, bool> v;
queue<pair<LL, LL> > q;
LL bfs(LL x, LL y) {
q.push(make_pair(x, 0));
while(!q.empty()) {
pair<LL, LL> tmp = q.front();
q.pop();
if(v[tmp.first]) continue;
v[tmp.first] = true;
if(tmp.first == y) return tmp.second;
for (int i = 2; i <= k; i++) {
if(tmp.first % i != 0) {
q.push(make_pair(tmp.first - tmp.first % i, tmp.second + 1));
}
}
q.push(make_pair(tmp.first - 1, tmp.second + 1));
}
}
LL dfs(LL x) {
if(dp[x] != -1) return dp[x];
LL tmp = INF;
for (int i = 2; i <= k; i++) {
if(x % i != 0)
tmp = min(tmp, dfs(x - x % i));
}
tmp = min(tmp, dfs(x - 1));
return dp[x] = tmp + 1;
}
int main() {
int LCM = 1;
scanf("%lld%lld%d", &a, &b, &k);
for (int i = 2; i <= k; i++) {
LCM = lcm(LCM, i);
}
memset(dp, -1, sizeof(dp));
dp[0] = 0;
for (int i = 1; i < LCM; i++) {
dfs(i);
}
LL ans = 0;
if(a - a % LCM >= b) {
ans += dp[a % LCM];
a -= a % LCM;
}
LL del = a - b;
LL tmp = del / LCM;
a -= tmp * LCM;
ans += tmp * (dp[LCM - 1] + 1);
if(a > b) {
LL tmp = bfs(a, b);
ans += tmp;
}
printf("%lld\n", ans);
}
Codeforces 251C Number Transformation DP, 记忆化搜索,LCM,广搜的更多相关文章
- 【bzoj5123】[Lydsy12月赛]线段树的匹配 树形dp+记忆化搜索
题目描述 求一棵 $[1,n]$ 的线段树的最大匹配数目与方案数. $n\le 10^{18}$ 题解 树形dp+记忆化搜索 设 $f[l][r]$ 表示根节点为 $[l,r]$ 的线段树,匹配选择根 ...
- 【BZOJ】1415 [Noi2005]聪聪和可可 期望DP+记忆化搜索
[题意]给定无向图,聪聪和可可各自位于一点,可可每单位时间随机向周围走一步或停留,聪聪每单位时间追两步(先走),问追到可可的期望时间.n<=1000. [算法]期望DP+记忆化搜索 [题解]首先 ...
- [题解](树形dp/记忆化搜索)luogu_P1040_加分二叉树
树形dp/记忆化搜索 首先可以看出树形dp,因为第一个问题并不需要知道子树的样子, 然而第二个输出前序遍历,必须知道每个子树的根节点,需要在树形dp过程中记录,递归输出 那么如何求最大加分树——根据中 ...
- poj1664 dp记忆化搜索
http://poj.org/problem?id=1664 Description 把M个相同的苹果放在N个相同的盘子里,同意有的盘子空着不放,问共同拥有多少种不同的分法?(用K表示)5.1.1和1 ...
- 状压DP+记忆化搜索 UVA 1252 Twenty Questions
题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...
- ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. Poor Ramzi -dp+记忆化搜索
ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. ...
- POJ 1088 DP=记忆化搜索
话说DP=记忆化搜索这句话真不是虚的. 面对这道题目,题意很简单,但是DP的时候,方向分为四个,这个时候用递推就好难写了,你很难得到当前状态的前一个真实状态,这个时候记忆化搜索就派上用场啦! 通过对四 ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- Codeforces 148D Bag of mice:概率dp 记忆化搜索
题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...
随机推荐
- WPFのStyle TargetType的不同写法
一.隐式写法 <Style TargetType="TextBlock"> <Setter Property="/> </Style> ...
- 深入Spring:自定义IOC
前言 上一篇文章讲了如何自定义注解,注解的加载和使用,这篇讲一下Spring的IOC过程,并通过自定义注解来实现IOC. 自定义注解 还是先看一下个最简单的例子,源码同样放在了Github. 先定义自 ...
- APScheduler的简单记录
此工具作为 定时任务调度 系统,在日常业务中经常使用,如定时获取第三方数据,定时清理数据 等等: 定时任务 和 业务逻辑 编写方式 一般有2种: 以 定时 清理db数据为例,在flask中,如下: 1 ...
- 何时使用move
https://zhidao.baidu.com/question/1514190267640555740.html 但编译器有copy elision优化,相当于原地构造,效率要高于move,手动指 ...
- vue-router中的router-link的active-class
vue-router中的router-link的active-class 在vue-router中要使用选中样式的方法有两种: 1.直接在路由js文件中配置linkActiveClass 2.在r ...
- jquery 弹出框效果
html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- JavaSE---类、对象、成员变量、局部变量
1.概述 1.1 类 1.1.1 类 是一种 自定义的 引用 数据类型: 1.2 对象 1.2.1 创建对象的根本途径:构造器: 通过new关键字 来调用 某个类的构造器: packa ...
- leetcode-第14周双周赛-1274-矩形内船只的数目
题目描述: 自己的提交: # """ # This is Sea's API interface. # You should not implement it, or s ...
- 使用springBoot完成阿里云短信验证
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...
- mysql的安裝
记得上学的时候,“研究”过一次mysql,找了篇文章,在课堂上念了.至今已经10余年,居然没再碰过数据库,自以为做嵌入式不用数据库,回头一看,却已经out许久... 上网下到最新的mysql5.5,从 ...