Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. inline bool MillerRabinTest( ll n, int k ) {
  2. if ( n <= 3 ) return true;
  3. if ( n % 2 == 0 ) return false;
  4. ll t = n - 1;
  5. ll s = 0;
  6.  
  7. while ( t % 2 == 0 ) {
  8. t /= 2;
  9. s += 1;
  10. }
  11. for ( int i = 0; i < k; i++ ) {
  12. ll a = rnd( 2LL, n - 2 );
  13. ll x = binpow( a, t, n );
  14. if ( x == 1 || x == n - 1 ) {
  15. continue;
  16. }
  17.  
  18. for ( int r = 1; r < s; r++ ) {
  19. x = binmul( x, x, n );
  20.  
  21. if ( x == 1 ) return false;
  22.  
  23. if ( x == n - 1 ) break;
  24. }
  25. if ( x != n - 1 ) return false;
  26. }
  27. return true;
  28. }
  29.  
  30.  
  31. inline ll polard( ll n ) {
  32. ll x = rnd( 2LL, n - 2 );
  33. ll i = 0, stage = 2, y = 1;
  34.  
  35. while ( true ) {
  36. if ( i == stage ) {
  37. y = x;
  38. stage *= 2;
  39. }
  40. x = binmul( x, x, n ) + 1;
  41. x %= n;
  42. i++;
  43. ll d = gcd( x > y ? x - y : y - x, n );
  44. if ( d != 1 && d != n ) {
  45. return d;
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement