Advertisement
Saleh127

UVA 10140

Aug 19th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define maX 1000008
  4. #define ll long long
  5. bool marked[maX];
  6. vector<ll>zz;
  7. void sieve()
  8. {
  9. marked[0]=1;
  10. marked[1]=1;
  11. for(ll i=4;i<=maX;i+=2)
  12. {
  13. marked[i]=1;
  14. }
  15. zz.push_back(2);
  16. for(ll i=3;i<=maX;i+=2)
  17. {
  18. if(marked[i]==0)
  19. {
  20. zz.push_back(i);
  21. for(ll j=i*i; j<=maX; j+=i+i)
  22. {
  23. marked[j]=1;
  24. }
  25. }
  26. }
  27. }
  28. bool check(ll x)
  29. {
  30. if(x<=1000005)
  31. {
  32. if(marked[x]==0)
  33. {
  34. return 0;
  35. }
  36. else return 1;
  37. }
  38. else
  39. {
  40. for(ll i=0;i<zz.size() && zz[i]*zz[i]<=x;i++)
  41. {
  42. if(x%zz[i]==0)
  43. {
  44. return 1;
  45. }
  46. }
  47. return 0;
  48. }
  49. }
  50. int main()
  51. {
  52. ios_base::sync_with_stdio(0);
  53. cin.tie(0);cout.tie(0);
  54.  
  55. sieve();
  56.  
  57. ll a,c,d,e,f,i,j,k,p1,p2,p3,p4,x,y;
  58.  
  59. while(cin>>a>>c)
  60. {
  61. d=1000000000;
  62. e=-100000;
  63. f=0;
  64. j=0;
  65. for(i=a;i<=c;i++)
  66. {
  67. if(check(i)==0)
  68. {
  69. if(f==0)
  70. {
  71. x=i;
  72. f=1;
  73. }
  74. else
  75. {
  76. y=i;
  77. k=y-x;
  78. if(k<d)
  79. {
  80. d=k;
  81. p1=x;
  82. p2=y;
  83. j++;
  84. }
  85. if(k>e)
  86. {
  87. e=k;
  88. p3=x;
  89. p4=y;
  90. j++;
  91. }
  92. x=y;
  93. }
  94. }
  95. }
  96. if(j==0) cout<<"There are no adjacent primes."<<endl;
  97. else cout<<p1<<","<<p2<<" are closest, "<<p3<<","<<p4<<" are most distant."<<endl;
  98. }
  99. return 0;
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement