Advertisement
pb_jiang

ABC300G TLE

May 13th, 2023
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  6.  
  7. using ll = long long;
  8. using pii = pair<int, int>;
  9. using pll = pair<ll, ll>;
  10. using vl = vector<ll>;
  11. using vi = vector<int>;
  12.  
  13. vi primes;
  14. void get_primes(ll ub)
  15. {
  16.     for (int i = 2; i <= ub; ++i) {
  17.         for (int j = 2; j * j <= i; ++j)
  18.             if (i % j == 0)
  19.                 continue;
  20.         primes.push_back(i);
  21.     }
  22. }
  23.  
  24. ll res;
  25. constexpr int cap = 1 << 16;
  26. ll dp[32][cap];
  27.  
  28. void dfs(ll n, int p)
  29. {
  30.     if (n < cap && dp[p][n]) {
  31.         res += dp[p][n];
  32.         return;
  33.     }
  34.  
  35.     if (p == 0) {
  36.         res += __lg(n) + 1;
  37.         return;
  38.     }
  39.  
  40.     // fprintf(stderr, "dfs(n:%lld, p:%d)\n", n, p);
  41.     ll cache = res;
  42.  
  43.     dfs(n, p - 1);
  44.     if (n >= primes[p])
  45.         dfs(n / primes[p], p);
  46.  
  47.     if (n < cap)
  48.         dp[p][n] = res - cache;
  49. }
  50.  
  51. int main(int argc, char **argv)
  52. {
  53.     ll n, p;
  54.     cin >> n >> p;
  55.     get_primes(p);
  56.  
  57.     dfs(n, primes.size() - 1);
  58.  
  59.     cout << res << endl;
  60.     return 0;
  61. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement