C. Number Transformation
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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:

  1. Subtract 1 from his number.
  2. 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.

Input

The only line contains three integers ab (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.

Output

Print a single integer — the required minimum number of seconds needed to transform number a into number b.

Examples
input
10 1 4
output
6
input
6 3 10
output
2
input
1000000000000000000 1 3
output
666666666666666667
Note

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的更多相关文章

  1. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

  2. [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& ...

  3. http://codeforces.com/contest/555/problem/B

    比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...

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

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

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

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

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

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

随机推荐

  1. Refs 和 DOM

    在常规的 React 数据流中,props 是父组件与子组件交互的唯一方式.要修改子元素,你需要用新的 props 去重新渲染子元素.然而,在少数情况下,你需要在常规数据流外强制修改子元素.被修改的子 ...

  2. PHP Filter 函数 日常可用

    PHP Filter 函数 PHP Filesystem PHP FTP PHP Filter 简介 PHP 过滤器用于对来自非安全来源的数据(比如用户输入)进行验证和过滤. 安装 filter 函数 ...

  3. 20172319 2018.03.27-04.05 《Java程序设计》第4周学习总结

    20172319 2018.03.27-04.05 <Java程序设计>第4周学习总结 教材学习内容总结 第四章 编写类 类与对象的回顾:对象是有状态的,状态由对象的属性值确定.属性由类中 ...

  4. Task 6.4 冲刺Two之站立会议6

    今天对视频的画面质量进行了优化,又把所有的界面更换了一些比较美观的图片和背景.使界面看起来更加地合理,易于接受.

  5. 第二次作业<1>

    1001.A+B Format (20) ac代码 1. 解题思路 先求和,再输出. 答案区间为-2,000,000至2,000,000,将答案分为三个区段分类讨论.虽然觉得很烦但是想不出更好的方法. ...

  6. Leetcode题库——13.罗马数字转整数

    @author: ZZQ @software: PyCharm @file: Luoma2Int.py @time: 2018/9/16 17:06 要求: 罗马数字转数字 字符 数值 I 1 V 5 ...

  7. 剑指offer:旋转数组的最小数字

    题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个 ...

  8. 阅读<构建之法>第三10、11、12章并提出问题

    <构建之法>第10.11.12章 第10章: 问题:对我们了解了用户的需求后,但是我们想法和做出来的软件会和用户的需求有偏差,比如风格.界面的修饰等等,那么我们程序猿怎样才能让自己的想法更 ...

  9. Week4-作业1:《构建之法》第四章、第十七章 阅读笔记与思考

    第四章 两人合作   这一章是讲述了两人结对编程的一些东西,包括一些代码的规范,还有结对编程的优点.怎么做.以及一些注意事项. 1.“错误处理 当程序的主要功能实现后,一些程序员会乐观地估计只需要另外 ...

  10. C# 正则提取字符串(提取一个或多个)

    实例一:string result = ""; string str = "大家好! <User EntryTime='2010-10-7' Email='zhan ...