Codeforces 251C 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的更多相关文章
- Codeforces 251C Number Transformation DP, 记忆化搜索,LCM,广搜
题意及思路:https://blog.csdn.net/bossup/article/details/37076965 代码: #include <bits/stdc++.h> #defi ...
- CodeForces 346C Number Transformation II
Number Transformation II 题解: 对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数. 也就是说 [ k * x[i] + 1, (k+1)* x ...
- Codeforces 346C Number Transformation II 构造
题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> # ...
- Codeforces 346C Number Transformation II 贪心(复杂度计算)
题意及思路:https://www.cnblogs.com/liuzhanshan/p/6560499.html 这个做法的复杂度看似是O(n ^ 2),实际上均摊是O(n)的.我们考虑两种极端数据: ...
- hdu4952 Number Transformation (找规律)
2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...
- bzoj 3858: Number Transformation 暴力
3858: Number Transformation Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 82 Solved: 41[Submit][Sta ...
- HDU-4952 Number Transformation
http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/ ...
- Codeforces 486C Palindrome Transformation(贪心)
题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...
- CodeForces346 C. Number Transformation II
C. Number Transformation II time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- centos7安装telnet
yum list |grep telnet yum install telnet.x86_64 安装后再测试
- 如何利用FastReport.Net 设计并导出报表?
在你的程序中,你可以使用FastReport.Net 报表设计器.要做到这一点,你需要使用报表对象设计方法: report1 = new Report();report1.Load("rep ...
- @ImportResource 导入Spring 的xml配置文件
在配置类尚标注此注解,等同于spring配置文件中的 <import resource="beans.xml"/> Spring Boot里面没有Spring的配置文件 ...
- mysql创建数据库指定编码格式
CREATE DATABASE `databasename` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
- PhoneUtil
package cn.fraudmetrix.octopus.horai.biz.utils; import org.springframework.util.StringUtils; import ...
- ubuntu14.04 放开串口权限
可以用如下命令查看串口信息: ls -l /dev/ttyUSB*来查看相关的信息. 但是普通用户没有usb操作权限(函数open()打不开串口:refused),如果我们想在ROS程序里面打开串口, ...
- Python startswith() 函数 判断字符串开头
Python startswith() 函数 判断字符串开头 函数:startswith() 作用:判断字符串是否以指定字符或子字符串开头 一.函数说明语法:string.startswith(str ...
- EB-GAN系(Energy-based GAN)
学习总结于国立台湾大学 :李宏毅老师 EB-GAN: Energy-based Generative Adversarial Network MA-GAN:MAGAN: Margin Adaptati ...
- scrapy通过修改配置文件发送状态邮件
EXTENSIONS = { 'scrapy.extensions.statsmailer.StatsMailer': 500,} STATSMAILER_RCPTS = ['159882826 ...
- RMAN-06059(转)
change archivelog all crosscheck;