fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int, int> pii;
  8. typedef pair<ll, ll> pll;
  9. typedef vector<int> vi;
  10. typedef vector<ll> vll;
  11. typedef string str;
  12.  
  13. #define pb push_back
  14. #define mp make_pair
  15. #define fi first
  16. #define se second
  17. #define all(x) (x).begin(), (x).end()
  18. #define len(x) ((int)(x).size())
  19.  
  20. #define forn(i, n) for (int i = 0; i < (int)(n); ++i)
  21. #define forr(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i)
  22. #define ford(i, r, l) for (int i = (int)(r); i >= (int)(l); --i)
  23.  
  24. #define cmin(a, b) a = min(a, b)
  25. #define cmax(a, b) a = max(a, b)
  26.  
  27. ll legendre(ll n, ll p) {
  28. ll cnt = 0;
  29. while (n > 0) {
  30. cnt += n / p;
  31. n /= p;
  32. }
  33. return cnt;
  34. }
  35.  
  36. int main() {
  37. ios_base::sync_with_stdio(false);
  38. cin.tie(NULL);
  39.  
  40.  
  41.  
  42. ll n, m;
  43. if (cin >> n >> m) {
  44. if (m == 1) {
  45. cout << 0 << "\n";
  46. return 0;
  47. }
  48.  
  49. ll ans = 1e18;
  50. ll temp = m;
  51.  
  52. for (ll i = 2; i * i <= temp; ++i) {
  53. if (temp % i == 0) {
  54. ll e = 0;
  55. while (temp % i == 0) {
  56. e++;
  57. temp /= i;
  58. }
  59. ll cnt = legendre(n, i);
  60. cmin(ans, cnt / e);
  61. }
  62. }
  63.  
  64. if (temp > 1) {
  65. ll cnt = legendre(n, temp);
  66. cmin(ans, cnt / 1);
  67. }
  68.  
  69. cout << ans << "\n";
  70. }
  71.  
  72. return 0;
  73. }
  74.  
Success #stdin #stdout 0s 5348KB
stdin
5 2
stdout
3