Advertisement
a53

FirstPrime_50P

a53
Mar 24th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #define N 2500 /// Pentru N 100000000/2/8+1 => sqrt(N)=2500
  3. using namespace std;
  4. char p[N+1];
  5.  
  6. void Ciur()
  7. {
  8. for(int i=1;((i*i)<<1)+(i<<1)<=N;i+=1)
  9. {
  10. if((p[i>>3]&(1<<(i&7)))==0)
  11. {
  12. for(int j=((i*i)<<1)+(i<<1);(j<<1)+1<=N;j+=(i<<1)+1)
  13. {
  14. p[j>>3]|=(1<<(j&7));
  15. }
  16. }
  17. }
  18. }
  19.  
  20. int FP(int x)
  21. {
  22. if(x%2==0)
  23. return 2;
  24. for(int i=1;(2*i+1)*(2*i+1)<=N;++i)
  25. if((p[i>>3]&(1<<(i&7)))==0&&(x%(2*i+1)==0))
  26. return 2*i+1;
  27. return x;
  28. }
  29.  
  30. int main()
  31. {
  32. Ciur();
  33. int n;
  34. cin>>n;
  35. int x;
  36. unsigned long long int s=0;
  37. while(n--)
  38. cin>>x,s+=FP(x);
  39. cout<<s;
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement