Advertisement
a53

CountPrime

a53
Mar 20th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <fstream>
  2. #define ULL unsigned long long int
  3. using namespace std;
  4. ULL base[] = {0, 2, 3, 23};
  5.  
  6. ULL exPow(ULL n,ULL p,ULL mod)
  7. {
  8. ULL sol = 1, nr = n;
  9. for(ULL i = 0; (1ULL << i) <= p; i++)
  10. {
  11. if((1 << i) & p)
  12. sol = 1LL * sol * nr % mod;
  13. nr = 1LL * nr * nr % mod;
  14. }
  15. return sol;
  16. }
  17.  
  18. int rabin_miller(ULL n)
  19. {
  20. if(n < 2)
  21. return 0;
  22. for(int i = 1; i <= 3; i++) {
  23. if(n == base[i])
  24. return 1;
  25. }
  26. if(n % 2 == 0)
  27. return 0;
  28. ULL tmp = n - 1, p2 = 0;
  29. while(tmp % 2 == 0)
  30. tmp /= 2, p2++;
  31. for(int i = 1; i <= 3; i++) {
  32. ULL ans = exPow(base[i], tmp, n);
  33. if(ans == 1 || ans == n - 1)
  34. continue;
  35. ULL j = 1;
  36. while(j < p2 && ans != n - 1)
  37. ans = 1LL * ans * ans % n, j++;
  38. if(ans != n - 1)
  39. return 0;
  40. }
  41. return 1;
  42. }
  43.  
  44. int main()
  45. {
  46. ULL st,dr;
  47. ifstream f("countprime.in");
  48. f>>st>>dr;
  49. ULL c=0;
  50. for(ULL i=st;i<=dr;++i)
  51. c+=rabin_miller(i);
  52. ofstream g("countprime.out");
  53. g<<c;
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement