fork download
  1. #include <iostream>
  2. #include <cassert>
  3. #include <chrono>
  4. #include <random>
  5.  
  6. typedef long long ll;
  7.  
  8. const int mod = 2147483647;
  9.  
  10. // Calculate x % (2^31-1) fast
  11. // 0 <= x <= (2^31-2)^2
  12. inline int fast_mod(ll value) {
  13. value = (value >> 31) + (value & mod);
  14. value = (value >> 31) + (value & mod);
  15. return value == mod ? 0 : value;
  16. }
  17.  
  18. int main() {
  19. auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
  20. std::mt19937 mt_rand(seed);
  21. std::uniform_int_distribution<ll> dis(ll(1e9), ll(1e18));
  22.  
  23. for (int n = 0; n < 100000000; ++n) {
  24. ll value = dis(mt_rand);
  25. assert(value % mod == fast_mod(value));
  26. }
  27. std::cout << "ok!";
  28. return 0;
  29. }
Success #stdin #stdout 3.12s 4496KB
stdin
Standard input is empty
stdout
ok!