Advertisement
a53

numbers_tree_50p

a53
Jan 1st, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <cstring>
  3. #define N 1000001
  4. using namespace std;
  5. bool *isPrime=new bool[N];
  6.  
  7. /// Ciurul lui Atkin (Sieve of Atkin) este un algoritm rapid si modern de gasire a tuturor numerelor prime pana la un numar întreg. A fost creat de A.O.L. Atkin si Daniel J. Bernstein.
  8. void Atkin(int lim)
  9. {
  10. /// Initial toate numerele sunt neprime
  11. memset(isPrime,0,sizeof(bool)*(lim+1));
  12. double sqr=sqrt(lim);
  13. int n;
  14. for(int x=1;x<=sqr;++x)
  15. {
  16. for(int y=1;y<=sqr;++y)
  17. {
  18. n=4*x*x+y*y;
  19. if(n<=lim&&(n%12==1||n%12==5))
  20. isPrime[n]=!isPrime[n];
  21. n=3*x*x+y*y;
  22. if(n<=lim&&n%12==7)
  23. isPrime[n]=!isPrime[n];
  24. n=3*x*x-y*y;
  25. if(x>y&&n<=lim&&n%12==11)
  26. isPrime[n]=!isPrime[n];
  27. }
  28. }
  29. for(int i=5;i<=sqr;i+=2)
  30. {
  31. /// Daca este prim, marcheaza toti multiplii i*i ca fiind neprimi
  32. if(isPrime[i])
  33. {
  34. for(int k=i*i;k<=lim;k+=i*i)
  35. isPrime[k]=false;
  36. }
  37. }
  38. }
  39.  
  40. #define DIM 262144
  41. char buff[DIM];
  42. int poz=0;
  43. FILE *f=fopen("numbers_tree.in","r");
  44.  
  45. void citeste(int &numar)
  46. {
  47. numar=0;
  48. while(buff[poz]<'0'||buff[poz]>'9') /// cat timp caracterul din buffer nu e cifra ignor
  49. if (++poz==DIM) ///daca am "golit" bufferul atunci il umplu
  50. fread(buff,1,DIM,f),poz=0;
  51. while('0'<=buff[poz]&&buff[poz]<='9') ///cat timp dau de o cifra recalculez numarul
  52. {
  53. numar=numar*10+buff[poz] - '0';
  54. if(++poz==DIM)
  55. fread(buff,1,DIM,f),poz=0;
  56. }
  57. }
  58.  
  59. int main ()
  60. {
  61. Atkin(N);
  62. isPrime[2]=1;
  63. isPrime[3]=1;
  64. isPrime[5]=1;
  65. int n,q;
  66. citeste(n),citeste(q);
  67. int a[n+1];
  68. for(int i=1;i<=n;++i)
  69. citeste(a[i]);
  70. int op=0,st,dr,val;
  71. ofstream g("numbers_tree.out");
  72. while(q--)
  73. {
  74. citeste(op),citeste(st),citeste(dr);
  75. if(op==1) /// toate valorile a[i] cu i din intervalul [st,dr] devin egale cu val
  76. {
  77. citeste(val);
  78. for(int i=st;i<=dr;++i)
  79. a[i]=val;
  80. }
  81. if(op==2) /// cate elemente ale sirului a care au indicii aflati in intervalul [st, dr] sunt numere compuse
  82. {
  83. int sol=0;
  84. for(int i=st;i<=dr;++i)
  85. if(!isPrime[a[i]]) /// Daca nu sunt prime, atunci sunt compuse
  86. ++sol;
  87. g<<sol<<'\n';
  88. }
  89. if(op==3) /// lungimea celei mai lungi secvente de numere prime alcatuita exclusiv din elemente ale sirului care au indicii aflati in intervalul [st, dr]
  90. {
  91. int k=0,Max=-1;
  92. for(int i=st;i<=dr;++i)
  93. {
  94. if(isPrime[a[i]])
  95. ++k;
  96. else
  97. k=0;
  98. if(k>Max)
  99. Max=k;
  100. }
  101. g<<Max<<'\n';
  102. }
  103. }
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement