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只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...
随机推荐
- for循环(foreach型)流程
- Linux历史命令管理以及用法
history [-c] [-d offset] [n] history -anrw [filename] history -ps arg [arg...] -c: 清空命令历史 -d offset: ...
- Oracle之分页问题
前面的Top-N问题使用了reownum,但是又遇到个分页问题,将表emp的4行为1页输出,前4行很好做: select rownum,empno,ename,sal from emp ; 但是4-- ...
- html+jquery+php实现文件上传全过程
本例子采用html+jquery+php实现上传功能 html部分 <!DOCTYPE html> <html> <head> <meta charset=& ...
- java--ArrayList,LinkedList应用比较
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class ListDem ...
- eclipse设置tomcat部署目录地址
参考: https://blog.csdn.net/lvyuan1234/article/details/53418818 右键,open 操作前提是所有项目移除,并且右键clean掉相关数据! 修改 ...
- JS的加载和执行
从JS的加载和执行谈性能优化 ---高性能JS读后感(第一章) 从脚本的"霸道"说起,随着浏览器的进步,js越来越听话了,所以,我们先说说以前的浏览器是怎么加载js的,以及js如何 ...
- 【leetcode】654. Maximum Binary Tree
题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...
- 每天一个linux命令:file(11)
file file命令用来探测给定文件的类型.file命令对文件的检查分为文件系统.魔法幻数检查和语言检查3个过程. 格式 file [选项] [参数] 参数选项 参数 备注 -b 列出辨识结果时,不 ...
- 关于scrub的详细分析和建议
https://ceph.com/planet/%E5%85%B3%E4%BA%8Escrub%E7%9A%84%E8%AF%A6%E7%BB%86%E5%88%86%E6%9E%90%E5%92%8 ...