fork download
  1. // mt19937 -> efficient pseudo-random number generator like rand()
  2. // 70 bcz [26+26] for upper + lowercase alphabets + 10 for '0' to '9'
  3. // and base must be greather than max. value of s[i]
  4.  
  5. mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
  6.  
  7. const int MOD1 = 1e9 + 7;
  8. const int MOD2 = 1e9 + 9;
  9.  
  10. // uniform_int_distribution<int> --> long long int must be used here
  11. const int base1 = uniform_int_distribution<int>(70, MOD1 - 1)(rng);
  12. const int base2 = uniform_int_distribution<int>(70, MOD2 - 1)(rng);
  13.  
  14. const int N = 5e5 + 100;
  15. int hash1[N], hash2[N], x1[N], x2[N], inv1[N], inv2[N];
  16.  
  17. int mod_add(int a, int b, int mod)
  18. {
  19. int tmp = (a + b) % mod;
  20. if (tmp < 0)
  21. tmp += mod;
  22. return tmp;
  23. }
  24.  
  25. int mod_mul(int a, int b, int mod)
  26. {
  27. int tmp = (a * 1LL * b) % mod;
  28. if (tmp < 0)
  29. tmp += mod;
  30. return tmp;
  31. }
  32.  
  33. int binExpo(int a, int b, int mod)
  34. {
  35. int tmp = 1;
  36. while (b)
  37. {
  38. if (b % 2)
  39. tmp = mod_mul(tmp, a, mod);
  40. a = mod_mul(a, a, mod);
  41. b /= 2;
  42. }
  43. return tmp;
  44. }
  45.  
  46. void preCalc()
  47. {
  48. x1[0] = x2[0] = 1;
  49. for (int i = 1; i < N; i++)
  50. {
  51. x1[i] = mod_mul(x1[i - 1], base1, MOD1);
  52. x2[i] = mod_mul(x2[i - 1], base2, MOD2);
  53. }
  54.  
  55. int x_inv1 = binExpo(base1, MOD1 - 2, MOD1);
  56. int x_inv2 = binExpo(base2, MOD2 - 2, MOD2);
  57. inv1[0] = inv2[0] = 1;
  58. for (int i = 1; i < N; i++)
  59. {
  60. inv1[i] = mod_mul(inv1[i - 1], x_inv1, MOD1);
  61. inv2[i] = mod_mul(inv2[i - 1], x_inv2, MOD2);
  62. }
  63. }
  64.  
  65. int val(char c)
  66. {
  67. if (c >= 'a' && c <= 'z')
  68. return (c - 'a' + 1);
  69.  
  70. if (c >= 'A' && c <= 'Z')
  71. return (c - 'A' + 27);
  72.  
  73. if (c >= '0' && c <= '9')
  74. return (c - '0' + 53);
  75. }
  76.  
  77. void build(string &s)
  78. {
  79. hash1[0] = hash2[0] = val(s[0]);
  80. for (int i = 1; i < s.size(); i++)
  81. {
  82. hash1[i] = mod_add(hash1[i - 1], mod_mul(x1[i], val(s[i]), MOD1), MOD1);
  83. hash2[i] = mod_add(hash2[i - 1], mod_mul(x2[i], val(s[i]), MOD2), MOD2);
  84. }
  85. }
  86.  
  87. // Hash for s[x....y] => x, y are index
  88. pii getHash(int x, int y)
  89. {
  90. int tmp1 = mod_add(hash1[y], (x == 0) ? 0 : -hash1[x - 1], MOD1);
  91. tmp1 = mod_mul(tmp1, inv1[x], MOD1);
  92.  
  93. int tmp2 = mod_add(hash2[y], (x == 0) ? 0 : -hash2[x - 1], MOD2);
  94. tmp2 = mod_mul(tmp2, inv2[x], MOD2);
  95.  
  96. // answer = {Hash1, Hash2}
  97. return {tmp1, tmp2};
  98. }
  99.  
Compilation error #stdin compilation error #stdout 0s 5284KB
stdin
Standard input is empty
compilation info
prog.cpp:5:1: error: ‘mt19937’ does not name a type
 mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
 ^~~~~~~
prog.cpp:11:19: error: ‘uniform_int_distribution’ was not declared in this scope
 const int base1 = uniform_int_distribution<int>(70, MOD1 - 1)(rng);
                   ^~~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:11:44: error: expected primary-expression before ‘int’
 const int base1 = uniform_int_distribution<int>(70, MOD1 - 1)(rng);
                                            ^~~
prog.cpp:12:19: error: ‘uniform_int_distribution’ was not declared in this scope
 const int base2 = uniform_int_distribution<int>(70, MOD2 - 1)(rng);
                   ^~~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:12:44: error: expected primary-expression before ‘int’
 const int base2 = uniform_int_distribution<int>(70, MOD2 - 1)(rng);
                                            ^~~
prog.cpp:77:12: error: variable or field ‘build’ declared void
 void build(string &s)
            ^~~~~~
prog.cpp:77:12: error: ‘string’ was not declared in this scope
prog.cpp:77:12: note: suggested alternative: ‘struct’
 void build(string &s)
            ^~~~~~
            struct
prog.cpp:77:20: error: ‘s’ was not declared in this scope
 void build(string &s)
                    ^
prog.cpp:88:1: error: ‘pii’ does not name a type
 pii getHash(int x, int y)
 ^~~
prog.cpp: In function ‘int val(char)’:
prog.cpp:75:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
stdout
Standard output is empty