fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. inline int random_value(int min, int max) {
  5. static mt19937 random(chrono::system_clock::now().time_since_epoch().count());
  6. return uniform_int_distribution<int>(min,max)(random); }
  7.  
  8. unordered_map<int,int> dp; int used;
  9.  
  10. inline int solve(int n, int p = 0) {
  11. if (n == 1)
  12. return 1-p;
  13. if (n == 2 or n&1)
  14. return p;
  15. auto it = dp.find(n);
  16. if (it != dp.end())
  17. return ++used, it->second^p;
  18. int ans = 1-p;
  19. for (int y, x = 2; x*x <= n; ++x)
  20. if (n%x == 0) {
  21. if (x&1 and solve(n/x,1-p) == p) {
  22. ans = p; break; }
  23. if (y = n/x, y > x and y&1 and solve(n/y,1-p) == p) {
  24. ans = p; break; } }
  25. dp[n] = ans^p;
  26. return ans; }
  27.  
  28. int main() {
  29. for (int i = 0; i < 1000000; ++i)
  30. solve(random_value(1,1000000000));
  31. cout << "dp value used " << used << " time(s)"; }
  32.  
Success #stdin #stdout 3.92s 60848KB
stdin
Standard input is empty
stdout
dp value used 1206134 time(s)