codeforces.com/contest/251/problem/C
2 seconds
256 megabytes
standard input
standard output
Little Petya likes positive integers a lot. Recently his mom has presented him a positive integer a. There's only one thing Petya likes more than numbers: playing with little Masha. It turned out that Masha already has a positive integer b. Petya decided to turn his number a into the number b consecutively performing the operations of the following two types:
- Subtract 1 from his number.
- Choose any integer x from 2 to k, inclusive. Then subtract number (a mod x) from his number a. Operation a mod x means taking the remainder from division of number a by number x.
Petya performs one operation per second. Each time he chooses an operation to perform during the current move, no matter what kind of operations he has performed by that moment. In particular, this implies that he can perform the same operation any number of times in a row.
Now he wonders in what minimum number of seconds he could transform his number a into number b. Please note that numbers x in the operations of the second type are selected anew each time, independently of each other.
The only line contains three integers a, b (1 ≤ b ≤ a ≤ 1018) and k (2 ≤ k ≤ 15).
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
Print a single integer — the required minimum number of seconds needed to transform number a into number b.
10 1 4
6
6 3 10
2
1000000000000000000 1 3
666666666666666667
In the first sample the sequence of numbers that Petya gets as he tries to obtain number b is as follows: 10 → 8 → 6 → 4 → 3 → 2 → 1.
In the second sample one of the possible sequences is as follows: 6 → 4 → 3.
题意:
给出 a b k
求a变到b的最小步数,每一步可以选择两种变化中的一种:a=a-1和a=a-a%i (2<=i<=k);
代码:
//k最大只有15,首先要想到2~15的lcm必然是一个循环节,然后就可以利用分块的思想,分成(a-1)/lcm+1块,每一块
//的大小是lcm,然后中间整个块的可以计算(每一块最大不会超过360360),两边的剩余的也可以计算了。这里我用的
//记忆化bfs找的。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=;
int k,vis[MAXN];
int gcd_(int x,int y) { return y==?x:gcd_(y,x%y); }
int lcm_(int x,int y) { return x/gcd_(x,y)*y; }
ll bfs(ll x,ll y)
{
queue<ll>q;
memset(vis,-,sizeof(vis));
q.push(x);
vis[x-y]=;
while(!q.empty()){
ll now=q.front();q.pop();
if(now==y) return vis[];
for(int i=;i<=k;i++){
ll xx=now-now%i;
if(xx<y||vis[xx-y]!=-) continue;
vis[xx-y]=vis[now-y]+;
q.push(xx);
}
ll xx=now-;
if(vis[xx-y]!=-) continue;
vis[xx-y]=vis[now-y]+;
q.push(xx);
}
}
int main()
{
ll a,b,ans=;
cin>>a>>b>>k;
if(k==){
cout<<a-b<<"\n";
return ;
}
ll last=;
for(int i=;i<=k;i++) last=lcm_(last,i);
ll tmp1=a/last;
ll tmp2=(b-)/last+;
if(tmp1>tmp2) ans=bfs(a,tmp1*last)+(tmp1-tmp2)*bfs(last,)+bfs(last*tmp2,b);
else ans=bfs(a,b);
cout<<ans<<"\n";
return ;
}
codeforces.com/contest/251/problem/C的更多相关文章
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环
E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...
- http://codeforces.com/contest/555/problem/B
比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...
- http://codeforces.com/contest/610/problem/D
D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/612/problem/D
D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- http://codeforces.com/contest/536/problem/B
B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/535/problem/C
C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- http://codeforces.com/contest/838/problem/A
A. Binary Blocks time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- http://codeforces.com/contest/402/problem/E
E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- tensorflow enqueue_many传入多个值的列表传入异常问题————Shape () must have rank at least 1
tf 的队列操作enqueue_many传入的值是列表,但是放入[]列表抛异常 File "C:\Users\lihongjie\AppData\Local\Programs\Python\ ...
- 基于spec评论作品
组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶 刘佳瑞 公冶令鑫 杨磊 杨金铭 张宇 卢帝同 一.测试目标:拉格朗日2018——飞词 下面是他们的小游戏在运行时的一些截图画面: 1.开始: ...
- Scrum立会报告+燃尽图(十二月七日总第三十八次):功能测试
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...
- JSON toBean Timestamp To Date 时间戳转日期
时间戳格式的时间从json转为date时 配置: import java.util.Date; import net.sf.ezmorph.object.AbstractObjectMorpher; ...
- eclipse 中使用git
1.安装egit插件,在新版的eclipse中已经集成了这个插件,省了不少时间, 旧版的eclipse可以在help->install new software中点击add,写入名称,网址具体如 ...
- 第三次作业---excel导入数据库及显示
好吧首先承认这次作业失败了,而且我并不知道原因.另外,我也没有采用PowerDesigner 设计所需要的数据库,代码就用了全部的时间.感觉自己就像一个刚学会爬着走路的小孩去参加一百一十米跨栏,能不能 ...
- Software Defined Networking(Week 2, part 2)
History of SDN 1.3 - 1.4 课程地址 Network Virtualization 网络可虚拟化,可以说是SDN的一项核心内容,同样也源自很多先前的技术和思想.我们先讨论何为网络 ...
- 07_Java基础语法_第7天(练习)_讲义
今日内容介绍 1.循环练习 2.数组方法练习 01奇数求和练习 * A: 奇数求和练习 * a: 题目分析 * 为了记录累加和的值,我们需要定义一个存储累加和的变量 * 我们要获取到1-100范围内的 ...
- profibus 的DPV0 和DPV1
DP的功能经过扩展,一共有3个版本:DP-V0,DP-V1和DP-V2.有的用户手册将DP-V1简写为DPV1. 1.基本功能(DP-V0) (1)总线访问方法:各主站之间为令牌传送,主站与从站间为主 ...
- PAT 1067 试密码
https://pintia.cn/problem-sets/994805260223102976/problems/994805266007048192 当你试图登录某个系统却忘了密码时,系统一般只 ...