Number Transformation

我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 5e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); LL a, b;
int k, lcm = ;
int dp[N]; void getDp(int x) {
memset(dp, -, sizeof(dp));
dp[x] = ;
queue<int> que;
que.push(x);
while(!que.empty()) {
int u = que.front(); que.pop();
if(dp[u - ] == -) {
dp[u - ] = dp[u] + ;
que.push(u - );
}
for(int i = ; i <= k; i++) {
int v = u - (u % i);
if(~dp[v]) continue;
dp[v] = dp[u] + ;
que.push(v);
}
}
} int main() {
scanf("%lld%lld%d", &a, &b, &k);
swap(a, b);
for(int i = ; i <= k; i++)
lcm = lcm / __gcd(lcm, i) * i;
LL ans = ;
LL R = b - (b % lcm);
LL L = a + (lcm - a % lcm) % lcm;
if(L <= R) {
LL cnt = (R - L) / lcm;
getDp(lcm - );
ans = dp[] * cnt + cnt;
if(a % lcm) ans += dp[a % lcm] + ;
getDp(b % lcm);
ans += dp[];
} else {
getDp(b % lcm);
ans = dp[a % lcm];
}
printf("%lld\n", ans);
return ;
} /*
*/

Codeforces 251C Number Transformation的更多相关文章

  1. Codeforces 251C Number Transformation DP, 记忆化搜索,LCM,广搜

    题意及思路:https://blog.csdn.net/bossup/article/details/37076965 代码: #include <bits/stdc++.h> #defi ...

  2. CodeForces 346C Number Transformation II

    Number Transformation II 题解: 对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数. 也就是说 [ k * x[i] + 1,  (k+1)* x ...

  3. Codeforces 346C Number Transformation II 构造

    题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> # ...

  4. Codeforces 346C Number Transformation II 贪心(复杂度计算)

    题意及思路:https://www.cnblogs.com/liuzhanshan/p/6560499.html 这个做法的复杂度看似是O(n ^ 2),实际上均摊是O(n)的.我们考虑两种极端数据: ...

  5. hdu4952 Number Transformation (找规律)

    2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...

  6. bzoj 3858: Number Transformation 暴力

    3858: Number Transformation Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 82  Solved: 41[Submit][Sta ...

  7. HDU-4952 Number Transformation

    http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/ ...

  8. Codeforces 486C Palindrome Transformation(贪心)

    题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...

  9. CodeForces346 C. Number Transformation II

    C. Number Transformation II time limit per test 1 second memory limit per test 256 megabytes input s ...

随机推荐

  1. Spring MVC 源码分析

      Spring MVC SpringMVC中扮演关键角色的DispatcherServlet类. 1 DispatcherServlet 1.1DispatcherServlet 类图 1.2 初始 ...

  2. js正则匹配table,img及去除各种标签问题

    //获取公示栏内容 s = "$row.detail$"; mainContent =s; //如果有多个table使用下面注释的正则只会匹配成一个table //var tabR ...

  3. Linux - rar 压缩

    Linux - rar yum -y install libstdc++.so. wget http://rarsoft.com/rar/rarlinux-4.0.1.tar.gz cd rar ma ...

  4. 前序遍历and中序遍历and后序遍历

    根据根节点的出现的时间确定前.中.后遍历. 1: 前序遍历首先访问根结点然后遍历左子树,最后遍历右子树.在遍历左.右子树时,仍然先访问根结点,然后遍历左子树,最后遍历右子树. 前序遍历结果:ABDEC ...

  5. luogu P4162 [SCOI2009]最长距离

    传送门 可以枚举两个点然后计算答案,至于是否合法,就要看可不可以通过移不超过\(t\)个箱子使得两点连通,也可以看做找一条路径使得路径上的1个数不超过\(t\) 所以可以考虑最短路,相邻的点两两连边, ...

  6. android AysncTask使用

    1.继承AysncTask类 例子: class downloadTask extends AsyncTask<Void,Integer,Boolean> 第一个参数是传入的参数 第二个参 ...

  7. POI读取Excel(xls、xlsx均可以)——(四)

    maven构建的项目-->pom.xml文件 eclipse提供Dependencies直接添加依赖jar包的工具:直接搜索poi以及poi-ooxml即可,maven会自动依赖需要的jar包: ...

  8. aircrack-ng笔记

    开启监听: airmon-ng start wlan0 抓包: airodump-ng wlan0mon 查看wifi ^C结束 airodump-ng -c 6 --bssid C8:3A:35:3 ...

  9. 【vim】删除指定标记前的内容 dt[标记]

    和删除标记内部有些相似,但目的不同.命令如下: dt[标记] 会删除所有光标和标记之间的内容(保持标记不动),如果在同一行有这个标记的话.例如 dt. 会删除至句子的末尾,但保持 '.' 不动.

  10. UML和模式应用5:细化阶段(6)---操作契约

    1.前言 操作契约使用前置和后置条件,描述领域模型里对象的详细变化,作为系统操作的结果. 操作契约可以作为有用的OOA相关的制品. 操作契约可以视为UP用例模型的一部分,它是对用例之处的系统操作的效用 ...