Advertisement
Bekzhan

CF 153 div 2 E

Dec 8th, 2012
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. /****************************************
  2. **     Solution by Bekzhan Kassenov    **
  3. ****************************************/
  4.  
  5. #include <iostream>
  6. #include <cstdio>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <string>
  10. #include <vector>
  11. #include <deque>
  12. #include <queue>
  13. #include <stack>
  14. #include <map>
  15. #include <set>
  16. #include <list>
  17. #include <algorithm>
  18. #include <cstdlib>
  19. #include <cmath>
  20. #include <ctime>
  21. #include <iomanip>
  22.  
  23. using namespace std;
  24.  
  25. #define PII pair <int, int>
  26. #define F first
  27. #define S second
  28. #define MP make_pair
  29. #define PB push_back
  30. #define VI vector <int>
  31. #define sqr(x) (x) * (x)
  32. #define INF (ll) (1e18)
  33. #define MOD (1000 * 1000 * 1000 + 7)
  34. #define ull unsigned long long
  35. #define ll long long
  36.  
  37. ll gcd(ll q, ll z)
  38. {
  39.     if (q == z)
  40.         return q;
  41.  
  42.     if (q == 0 || z == 0)
  43.         return (q + z);
  44.  
  45.     if (q < z)
  46.         return gcd(z % q, q);
  47.  
  48.     return gcd(q % z, z);
  49. }
  50.  
  51. ll lcm(ll q, ll z)
  52. {
  53.     return ((q / gcd(q, z)) * z);
  54. }
  55.  
  56. ll a, b, k;
  57.  
  58. ll make(ll aa, ll ba)
  59. {
  60.    
  61.     vector <ll> dp((int)(ba - aa + 1), INF);
  62.  
  63.     cerr << "OK" << endl;
  64.  
  65.     dp[0] = 0;
  66.  
  67.     for (ll i = aa; i <= ba; i++)
  68.         {
  69.             int l = i - aa;
  70.             dp[l + 1] = min(dp[l + 1], dp[l] + 1);
  71.  
  72.             for (int j = 2; j <= k; j++)
  73.                 {
  74.                     if (i % j == 0)
  75.                         {
  76.                             for (int w = 1; w < j; w++)
  77.                                 if (i + w <= ba)
  78.                                     {
  79.                                         dp[(int)(i + w - aa)] = min(dp[(int)(i + w - aa)], dp[(int)l] + 1);
  80.                                     }
  81.                         }
  82.                 }
  83.         }
  84.  
  85.     //cout << dp[ba - aa] << endl;
  86.  
  87.     return dp[(int)(ba - aa)];
  88. }
  89.  
  90. int main()
  91. {
  92.     #ifndef ONLINE_JUDGE
  93.         freopen("in", "r", stdin);
  94.     #endif
  95.  
  96.     cin >> a >> b >> k;
  97.  
  98.     ll ans = 0;
  99.  
  100.     if (a == b)
  101.         {
  102.             cout << 0;
  103.             return 0;
  104.         }
  105.  
  106.     ll t = 1;
  107.  
  108.     for (int i = 2; i <= k; i++)
  109.         t = lcm(t, i);
  110.  
  111.     if ((a - b) < t)
  112.         {
  113.             cout << make(b, a);
  114.             return 0;
  115.         }
  116.  
  117.     if (a % t != 0)
  118.         {
  119.             cerr << "asdas" << endl;
  120.             ans += make(a - a % t, a);
  121.             a -= a % t;
  122.         }
  123.  
  124.     if (b % t != 0)
  125.         {
  126.             ans += make(b, b - b % t + t);
  127.             cerr << "asdas" << endl;
  128.             b += t - b % t;
  129.             cerr << "asdas" << endl;
  130.         }
  131.  
  132.     if (a != b)
  133.         {
  134.             cerr << "Asdas" << endl;
  135.             ll tmp = make(0, t);
  136.  
  137.             cerr << tmp << endl;
  138.  
  139.             ans += ((a - b) / t) * tmp;
  140.             cerr << "asdasd" << endl;
  141.         }
  142.  
  143.     cout << ans;
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement