博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
251C - Number Transformation
阅读量:4558 次
发布时间:2019-06-08

本文共 2625 字,大约阅读时间需要 8 分钟。

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

Output

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

Sample test(s)
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.

分析:求出模数们的最小公倍数c,那么若a-b>c,则中途必须经过几个c状态,所以我们只要DP计算出c内的转换最小数即可;若a-b>c,则直接计算从a到b的次数

#include
#include
#include
using namespace std;long long dp[400000];#define maxn 0x7fffffffint k;int gcd(int a,int b){ for(int t;t=b;b=a%b,a=t); return a;}long long gao(long long a,long long b){ int i,j,s; dp[0]=0; for(i=1;i<=a-b;++i){ dp[i]=dp[i-1]+1; for(j=2;j<=k;++j){ s=(b+i)%j; if(i-s>=0 && dp[i-s]+1
> a >> b >> k) { c=1; for(i=2;i<=k;++i){ c=c*i/gcd(c,i); // cout<
<

 

 

转载于:https://www.cnblogs.com/baidongtan/archive/2012/12/09/2809407.html

你可能感兴趣的文章
LeetCode Maximum Subarray
查看>>
让我们再聊聊浏览器资源加载优化
查看>>
underscore demo
查看>>
CSS hack
查看>>
C# Enum Name String Description之间的相互转换
查看>>
PHP wamp server问题
查看>>
Spring Data Redis学习
查看>>
js闭包理解案例-解决for循环为元素注册事件的问题
查看>>
2015.04.23,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 33
查看>>
Spring+SpringMVC+JDBC实现登录
查看>>
生与死之间
查看>>
NEFU 109
查看>>
HDU 5435
查看>>
git从已有分支拉新分支开发
查看>>
滚动条隐藏兼容写法
查看>>
SQL2005查询所有表的大小
查看>>
Shell 正则表达式
查看>>
Docker run命令参数整理
查看>>
qt-opencv配置mingw编译器
查看>>
CSS之Medial Queries的另一用法:实现IE hack的方法
查看>>