Advertisement
a53

prim997

a53
Jan 5th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <fstream>
  2. #include <bitset>
  3. #define N 10000001
  4. using namespace std;
  5. bitset <N> prim;
  6. struct SOL
  7. {
  8. int primul,L;
  9. };
  10. SOL sol[N];
  11.  
  12. void Ciur()
  13. {
  14. prim[0]=prim[1]=1; /// Ciurul lui Eratostene
  15. for(int i=2;i*i<N;++i)
  16. if(prim[i]==0)
  17. for(int j=2;i*j<N;++j)
  18. prim[i*j]=1;
  19. }
  20.  
  21. void Lungime()
  22. {
  23. int L=0,Lmax=1,primul=1,primul_ant=1;
  24. bool schimb_primul=true; /// Permite schimbarea primul_ant
  25. for(int i=1;i<N;++i)
  26. {
  27. if(prim[i]) /// Daca nu e prim
  28. {
  29. ++L;
  30. if(L>Lmax)
  31. {
  32. Lmax=L;
  33. if(schimb_primul)
  34. primul_ant=primul,schimb_primul=false;
  35. }
  36.  
  37. }
  38. else /// Daca e prim
  39. {
  40. schimb_primul=true;
  41. primul=i+1;
  42. L=0;
  43. }
  44. sol[i].primul=primul_ant,sol[i].L=Lmax;
  45. }
  46. }
  47.  
  48.  
  49. int main()
  50. {
  51. ifstream f("prim997.in");
  52. Ciur();
  53. Lungime();
  54. ofstream g("prim997.out");
  55. int n;
  56. f>>n;
  57. int x;
  58. for(int i=0;i<n;++i)
  59. {
  60. f>>x;
  61. g<<sol[x].primul<<' '<<sol[x].L<<'\n';
  62. }
  63. f.close();
  64. g.close();
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement