CF251C.Number Transformation

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

小佩佳非常喜欢正整数。最近,他妈妈送给了他一个正整数 aa。佩佳比数字更喜欢的一件事是和小玛莎一起玩耍。结果发现,玛莎已经拥有一个正整数 bb。佩佳决定通过连续执行以下两类操作,将他的数 aa 变为数 bb:

  1. 将当前数减 11;
  2. 任选一个整数 xx,满足 2≤x≤k2 \le x \le k。然后将当前数 aa 减去 a mod xa \bmod x。其中运算 a mod xa \bmod x 表示 aa 除以 xx 所得的余数。

佩佳每秒执行一次操作。每次他都可自由选择当前要执行的操作类型,而无需考虑此前已执行过哪些操作。特别地,这意味着他可以连续多次执行同一类操作。

现在他想知道:将数 aa 变为数 bb 所需的最少秒数是多少?请注意,在第二类操作中,每次所选的整数 xx 都是重新独立选取的。

输入格式

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 %I64d specifier.

唯一一行包含三个整数 aa、bb(1 ≤ b ≤ a ≤ 10181 ≤ b ≤ a ≤ 10^{18})和 kk(2 ≤ k ≤ 152 ≤ k ≤ 15)。

在 C++ 中,请勿使用 %lld 说明符读取或写入 64 位整数。推荐使用 cin、cout 流,或 %I64d 说明符。

输出格式

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

输出一个整数——将数字 aa 变换为数字 bb 所需的最少秒数。

输入输出样例

  • 输入#1

    10 1 4

    输出#1

    6
  • 输入#2

    6 3 10

    输出#2

    2
  • 输入#3

    1000000000000000000 1 3

    输出#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.

在第一个样例中,Petya 为得到数字 bb 而得到的数字序列为:10→8→6→4→3→2→110 \rightarrow 8 \rightarrow 6 \rightarrow 4 \rightarrow 3 \rightarrow 2 \rightarrow 1。

在第二个样例中,一种可能的序列为:6→4→36 \rightarrow 4 \rightarrow 3。

输入解题思路,AI测评打分。不知道怎么写?

首页