Advertisement
Guest User

147C

a guest
Oct 25th, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <map>
  6. #include <string>
  7. #include <cstring>
  8. #include <cmath>
  9. #include <set>
  10. #include <list>
  11. #include <queue>
  12. using namespace std;
  13.  
  14. #define LL long long
  15. #define V vector
  16. #define VI V<int>
  17. #define VII V<VI>
  18. #define FOR(t,l,r) for (int t=l; t<r; t++)
  19. #define FORL(t,l,r) for (LL t=l; t<r; t++)
  20. const double mth_pi=2*acos(0.);
  21. #define pi mth_pi
  22. #define inf (1<<23)
  23. #define eps 1e-5
  24. #define pb push_back
  25. #define mp make_pair
  26. #define sz size()
  27. #define mii map<int,int>
  28. #define pii pair<int,int>
  29. #define x first
  30. #define y second
  31.  
  32. const int NN=1234567;
  33. int lp[NN+1], pr[NN], cc=0;
  34. void resh () {
  35.     FOR(i,2,NN+1) {
  36.         if (!lp[i]) {
  37.             lp[i]=i, pr[cc++]=i;}
  38.         int t=i*pr[0];
  39.         for (int j=0; j<cc && pr[j]<=lp[i] && t<=NN; ++j, t=i*pr[j])
  40.             lp[t]=pr[j];} }
  41.  
  42. int main () {
  43.     int a, b, k, kk, t;
  44.     cin >>a>>b>>k;
  45.     VI p; resh();
  46.  
  47.     for (t=a; t<=b; ++t) if (lp[t]==t) p.pb(t);
  48.     int n=p.sz, l=0;
  49.     if (n<k) {cout <<"-1\n"; return 0;}
  50.     for (t=0; t<n-k; ++t) l=max(l,p[t+k]-p[t]);
  51.  
  52.     kk=0;
  53.     for (t=a; t<=a+l-1; ++t) kk+=(lp[t]==t);
  54.     while (t<=b && kk<k) kk+=(lp[t]==t), t++, l++;
  55.     if (l>b-a+1) {cout <<"-1\n"; return 0;}
  56.  
  57.     kk=0;
  58.     for (t=b; t>=b-l+1; --t) kk+=(lp[t]==t);
  59.     while (t>=a && kk<k) kk+=(lp[t]==t), t--, l++;
  60.     if (l>b-a+1) {cout <<"-1\n"; return 0;}
  61.  
  62.     cout <<l<<endl;
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement