题意及思路: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,广搜的更多相关文章

  1. 【bzoj5123】[Lydsy12月赛]线段树的匹配 树形dp+记忆化搜索

    题目描述 求一棵 $[1,n]$ 的线段树的最大匹配数目与方案数. $n\le 10^{18}$ 题解 树形dp+记忆化搜索 设 $f[l][r]$ 表示根节点为 $[l,r]$ 的线段树,匹配选择根 ...

  2. 【BZOJ】1415 [Noi2005]聪聪和可可 期望DP+记忆化搜索

    [题意]给定无向图,聪聪和可可各自位于一点,可可每单位时间随机向周围走一步或停留,聪聪每单位时间追两步(先走),问追到可可的期望时间.n<=1000. [算法]期望DP+记忆化搜索 [题解]首先 ...

  3. [题解](树形dp/记忆化搜索)luogu_P1040_加分二叉树

    树形dp/记忆化搜索 首先可以看出树形dp,因为第一个问题并不需要知道子树的样子, 然而第二个输出前序遍历,必须知道每个子树的根节点,需要在树形dp过程中记录,递归输出 那么如何求最大加分树——根据中 ...

  4. poj1664 dp记忆化搜索

    http://poj.org/problem?id=1664 Description 把M个相同的苹果放在N个相同的盘子里,同意有的盘子空着不放,问共同拥有多少种不同的分法?(用K表示)5.1.1和1 ...

  5. 状压DP+记忆化搜索 UVA 1252 Twenty Questions

    题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...

  6. 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. ...

  7. POJ 1088 DP=记忆化搜索

    话说DP=记忆化搜索这句话真不是虚的. 面对这道题目,题意很简单,但是DP的时候,方向分为四个,这个时候用递推就好难写了,你很难得到当前状态的前一个真实状态,这个时候记忆化搜索就派上用场啦! 通过对四 ...

  8. CodeForces 398B 概率DP 记忆化搜索

    题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...

  9. Codeforces 148D Bag of mice:概率dp 记忆化搜索

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...

随机推荐

  1. 作业(二)—python实现wc命令

    Gitee地址:https://gitee.com/c1e4r/word-count(为什么老师不让我们用github) 0x00 前言 好久没发博客了,感觉自己的学习是有点偷懒了.这篇博客也是应专业 ...

  2. Codeforces 1179D 树形DP 斜率优化

    题意:给你一颗树,你可以在树上添加一条边,问添加一条边之后的简单路径最多有多少条?简单路径是指路径中的点只没有重复. 思路:添加一条边之后,树变成了基环树.容易发现,以基环上的点为根的子树的点中的简单 ...

  3. error: device unauthorized.

    1 执行 adb install   com.taobao.taobao_250.apk 报错 2 先看手机是不是未授权,执行命令之后,手机回弹出授权信息,点击确认就行了

  4. 浅谈CICD持续集成、持续部署的流程(转)

    Jenkins是一个比较流行的持续集成工具GitLab是存储镜像的镜像仓库由客户端将代码push推送到git仓库,gitlab上配置了一个webHook的东西可以触发Jenkins的构建.进入到Jen ...

  5. linux make: *** No targets specified and no makefile found. Stop.

    [root@localhost Python-]# ./configure checking build system type... x86_64-unknown-linux-gnu checkin ...

  6. 监控服务(keepalived,httpd)

    #!/bin/bashpidof httpdif [ $? -eq 0 ];then echo "httpd is ok"else echo "httpd is not ...

  7. ICU lirary DownLoad

    { //https://github.com/unicode-org/icu }

  8. openwrt增加密码及ssh的方法

    openwrt增加密码及ssh的方法 1.进入openwrt系统源码的顶层目录,然后执行 make menuconfig命令进入 Network--> SSH-->  <*>o ...

  9. Vue学习笔记【33】——相关文章

    vue.js 1.x 文档 vue.js 2.x 文档 String.prototype.padStart(maxLength, fillString) js 里面的键盘事件对应的键码 Vue.js双 ...

  10. SPI总线介绍和裸机编程分析

    一.SPI总线结构 SPI(Serial Peripheral Interface)串行外设接口,是一种高速的,全双工,同步的通信总线.采用主从模式(Master Slave)架构,支持多个slave ...