a53

Catalin_si_armata

a53
Jan 28th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. //Dobricean Iount
  2. #include <fstream>
  3. char IN[]="armata.in",OUT[]="armata.out";
  4. std::fstream cin(IN,std::ios::in),cout(OUT,std::ios::out);
  5.  
  6. bool erat[5000000];
  7. int x,cnt,k,n;
  8.  
  9. void ertaostene()
  10. {std::ios::sync_with_stdio(false);
  11. long long i,j;
  12. erat[1]=1;
  13. long long DIM=4*n+100;
  14. for(i=2 ; i<DIM/2; i++)
  15. if(erat[i]==false)
  16. for(j=i*2; j<DIM; j=j+i)
  17. erat[j]=true;
  18.  
  19. }
  20.  
  21. void det(int l)
  22. {
  23.  
  24. if(erat[l*3-1]==true && l*3-1<=n)
  25. {cnt++;
  26. }
  27. else
  28. if(l*3-1<=n)
  29. det(l*3-1);
  30. if(erat[l*3]==true && l*3<=n)
  31. {cnt++;
  32. }
  33. else
  34. if(l*3<=n)
  35. det(l*3);
  36. if(erat[l*3+1]==true && l*3+1<=n)
  37. {cnt++;
  38. }
  39. else
  40. if(l*3+1<=n)
  41. det(l*3+1);
  42.  
  43. }
  44.  
  45. int main()
  46. { int i;
  47. cin>>n>>x;
  48. ertaostene();
  49. while(x>0)
  50. { cin>>i;
  51. if(erat[i]==false) {cout<<-1<<"\n";
  52. }
  53. else
  54. cnt=0,det(i),cout<<cnt<<"\n";
  55. x--;
  56. }
  57.  
  58. }
Add Comment
Please, Sign In to add comment