http://codeforces.com/contest/251/problem/C
关键就是注意到从i*lcm(1,2 ...K)转移到(i-1)*lcm(1, 2...K)的代价等于lcm(1, 2...K)转移到0的代价,
而且由于第一种操作只能减小1而第二种操作不可能越过过i*lcm(1, 2...K)的(可以用反证法证明),
所以转移的过程必然会经过所有的i*lcm(1, 2, ...K)(a <= i*lcm(1, 2...K) <= b),bfs求出转移代价再
乘上区间个数,再处理一下俩个边界就可以了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <ctime>
#include <bitset>
#include <iomanip>
//#pragma comment(linker, "/STACK:102400000,102400000")
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;
using std::ios;
using std::make_heap;
using std::push_heap;
using std::pop_heap;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<ULL, ULL> PAIR;
typedef multimap<int, int> MMAP;
typedef long double LF;
const int MAXN(10010);
const int MAXM(10010);
const int MAXE(2100010);
const int MAXK(6);
const int HSIZE(13131);
const int SIGMA_SIZE(26);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-6);
const LF PI(acos(-1.0));
template<typename T> inline bool checkmax(T &a, T b){if(b > a) { a = b; return true;} return false;}
template<typename T> inline bool checkmin(T &a, T b){if(b < a) { a = b; return true;} return false;}
template<typename T> inline T ABS(T a){return a < 0? -a: a;}
template<typename T> inline bool EZ(T a){return ABS(a) < EPS;}
int gcd(int a, int b){
return b? gcd(b, a%b): a;
}
int lcm(int a, int b){
return a/gcd(a, b)*b;
}
bool vis[400010];
struct NODE{
int s, c;
NODE(){}
NODE(int s_, int c_): s(s_), c(c_){}
} que[400010];
int front, back;
int bfs(LL a, LL b, LL K){
memset(vis, 0, sizeof(vis[0])*(a+1));
front = back = 0;
que[back++] = NODE(a, 0);
vis[a] = true;
while(front < back){
NODE cur = que[front++];
if(cur.s == b) return cur.c;
int ts = cur.s-1;
if(ts >= b && !vis[ts]){
que[back++] = NODE(ts, cur.c+1);
vis[ts] = true;
}
for(int i = 2; i <= K; ++i){
ts = cur.s-cur.s%i;
if(ts >= b && !vis[ts]){
que[back++] = NODE(ts, cur.c+1);
vis[ts] = true;
}
}
}
}
int main(){
LL a, b, K;
while(~scanf("%I64d%I64d%I64d", &a, &b, &K)){
int lc = 1;
for(int i = 2; i <= K; ++i) lc = lcm(lc, i);
LL step = a/lc-(b/lc+(b%lc? 1: 0)), ans; //step = floor(a/lc)-ceiling(b/lc)
if(step < 0) ans = bfs(a%lc, b%lc, K);
else ans = bfs(lc, b%lc? b%lc: lc, K)+step*bfs(lc, 0, K)+bfs(a%lc, 0, K);
printf("%I64d\n", ans);
}
return 0;
}