Advertisement
agul

ITMO IO 20120526 (Basic) :: Problem D

May 27th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1.  
  2. #pragma comment(linker, "/STACK:66777216")
  3. #define _USE_MATH_DEFINES
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include <vector>
  6. #include <list>
  7. #include <map>
  8. #include <set>
  9. #include <deque>
  10. #include <stack>
  11. #include <bitset>
  12. #include <algorithm>
  13. #include <functional>
  14. #include <numeric>
  15. #include <utility>
  16. #include <sstream>
  17. #include <iostream>
  18. #include <iomanip>
  19. #include <cstdio>
  20. #include <cmath>
  21. #include <cstdlib>
  22. #include <ctime>
  23. #include <queue>
  24. #include <assert.h>
  25. #include <memory.h>
  26. #pragma hdrstop
  27.  
  28. using namespace std;
  29.  
  30. #define pb push_back
  31. #define mp make_pair
  32. #define X first
  33. #define Y second
  34. #define y0 __MY_Y0__
  35. #define y1 __MY_Y1__
  36. #define sz(a) (int)a.size()
  37. #define fill(a, x) memset (a, x, sizeof(a))
  38.  
  39. #ifdef _DEBUG
  40.     #define Eo(x) {cout << "# " << #x << " = " << (x) << endl;}
  41.     #define E(x) {cout << "# " << #x << " = " << (x) << " ";}
  42.     #define Ou(x) {cout << "# " << (x) << endl;}
  43.     #define OK {cout << "# OK    Line : " << __LINE__ << endl;}
  44. #else
  45.     #define Eo(x)
  46.     #define E(x)
  47.     #define Ou(x)
  48.     #define OK
  49. #endif
  50.  
  51. #ifdef WIN32
  52.     #define LLD "%I64d"
  53. #else
  54.     #define LLD "%lld"
  55. #endif
  56.  
  57. typedef long long ll;
  58. typedef unsigned long long ull;
  59. typedef pair<int, int> pii;
  60.  
  61. inline void sIO() {
  62. #ifdef _DEBUG
  63.     freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  64. #endif
  65. }
  66. inline void iIO() {freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);}
  67. inline void fIO(string fn) {
  68. #ifdef _DEBUG
  69.     freopen("input.txt", "r", stdin); freopen ("output.txt", "w", stdout);
  70. #else
  71.     freopen((fn + ".in").c_str(), "r", stdin); freopen((fn + ".out").c_str(), "w", stdout);
  72. #endif
  73. }
  74. inline void TM() {
  75. #ifdef _DEBUG
  76.     cout << endl << "# Time: " << clock() / 1000. << endl;
  77. #endif
  78. }
  79. template<class T> inline T abs(T x) {return x < 0 ? -x : x;}
  80. template<class T> inline T sqr(T x) {return x * x;}
  81. template<class T> inline T gcd(T a, T b) {if (a < b) swap(a, b); while (b) {a %= b; swap(a, b);} return a;}
  82. template<class T> inline T lcm(T a, T b) {return a * b / gcd(a, b);}
  83. template<class T> inline bool isPrime(T n) {if (n < 2) return false; T kk = (T)sqrt(n + 0.); for (T i = 2; i <= kk; ++i) if (!(n % i)) return false; return true;}
  84. template<class T> inline string toa(T x) {stringstream ss; ss << x; string ret; ss >> ret; return ret;}
  85. template<class T> inline T ppow(T a, ll b) {T ret = 1; while (b) {if (b & 1) ret *= a; a *= a; b >>= 1;} return ret;}
  86. inline int toi(string s) {stringstream ss; ss << s; int ret; ss >> ret; return ret;}
  87. inline ll tol(string s) {stringstream ss; ss << s; ll ret; ss >> ret; return ret;}
  88. inline void swap(short& a, short& b) {b ^= a ^= b ^= a;}
  89. inline void swap(int& a, int& b) {b ^= a ^= b ^= a;}
  90. inline void swap(char& a, char& b) {b ^= a ^= b ^= a;}
  91. inline void swap(ll& a, ll& b) {b ^= a ^= b ^= a;}
  92. inline char upperCase(char ch) {return (ch >= 'a' && ch <= 'z') ? ch^32 : ch;}
  93. inline char lowerCase(char ch) {return (ch >= 'A' && ch <= 'Z') ? ch^32 : ch;}
  94. inline string upperCase(string s) {int ls = s.length(); for (int i = 0; i < ls; ++i) if (s[i] >= 'a' && s[i] <= 'z') s[i] ^= 32; return s;}
  95. inline string lowerCase(string s) {int ls = s.length(); for (int i = 0; i < ls; ++i) if (s[i] >= 'A' && s[i] <= 'Z') s[i] ^= 32; return s;}
  96. inline int dig(char ch) {return ch - 48;}
  97. inline bool isAlpha(char ch) {return (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z');}
  98. inline bool isDigit(char ch) {return (ch >= '0' && ch <= '9');}
  99. inline bool isLowerCase(char ch) {return (ch >= 'a' && ch <= 'z');}
  100. inline bool isUpperCase(char ch) {return (ch >= 'A' && ch <= 'Z');}
  101.  
  102. const int INF = 0x3f3f3f3f;
  103. const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
  104. const double EPS = 1e-9;
  105. const int MD = 1000000007;
  106.  
  107. ll n, k, mn, ans, t, kk, x, qq, cur;
  108. vector< pair<ll, ll> > a;
  109.  
  110. ll find(ll n, ll m) {
  111.     ll ret = 0;
  112.     while (n) {
  113.         ret += n / m;
  114.         n /= m;
  115.     }
  116.     return ret;
  117. }
  118.  
  119. int main() {
  120.     fIO("mars");
  121.     scanf(LLD LLD, &n, &k);
  122.     kk = (ll)sqrt(k + 0.) + 1;
  123.     t = k;
  124.     mn = 0;
  125.     for (int i = 2; i <= kk; ++i)
  126.         if (!(t % i)) {
  127.             qq = 0;
  128.             while (!(t % i)) {
  129.                 t /= i;
  130.                 ++qq;
  131.             }
  132.             mn = i;
  133.             a.pb(mp(i, find(n, i)));
  134.         }
  135.     if (t > 1) a.pb(mp(t, find(n, t)));
  136.     ans = LINF;
  137.     kk = sz(a);
  138.     for (int i = 0; i < kk; ++i) {
  139.         cur = 0;
  140.         while (!(k % a[i].X)) {
  141.             ++cur;
  142.             k /= a[i].X;
  143.         }
  144.         if (cur) ans = min(ans, a[i].Y / cur);
  145.     }
  146.     printf(LLD, (ans == LINF ? 0 : ans));
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement