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:
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.
InputThe 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, coutstreams or the %I64d specifier.
OutputPrint a single integer — the required minimum number of seconds needed to transform number a into number b.
Sample test(s) input10 1 4output
6input
6 3 10output
2input
1000000000000000000 1 3output
666666666666666667Note
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 (1?≤?b?≤?a?≤?1018) 和k(2?≤?k?≤?15).。要你把a變成b。你可以進行兩種操作
1.把a-1.
2.a-(a%c).2<=c<=k.
每步只能執行兩種操作的一種。c可以隨意選擇。
現在問你最少需要多少步把a變成b.
思路:
由於數據范圍比較大不能簡單的搜索。必須找找其他的規律了。
操作1沒什麼好說的。對於操作2.對於每個c肯定就是把a變成c的倍數了。設cm為2~k的最小公倍數。a=x*cm+y。當y=0時操作2已經無效了。此時必須使用操作1.使a減小最快的方法是先把a變成x*cm然後減-1.然後減成(x-1)*cm然後循環。用dp[i]表示。把i減成0需要的最小步數。那沒執行一個上述操作需要的步數就為1+dp[cm-1]。所以可以直接計算出把a減到a-b 詳細見代碼:#include