JuliaMelkozerova

HW2E

Mar 11th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int main () {
  8.     const int MAX = 20000000;
  9.     vector <bool> S (MAX, true);
  10.     for (int k = 2; k * k <= MAX; k++) {
  11.         if (S[k]) {
  12.             for (int l = k * k; l < MAX; l += k) S[l] = false;
  13.         }
  14.     }
  15.    
  16.     int M, N;
  17.     cin >> M >> N;
  18.     int count = 0;
  19.  
  20.     for (int i = 1; i <= N; ++i) {
  21.         count += S[i];
  22.     }
  23.    
  24.     if (count == M) {
  25.         cout << "1";
  26.         return 0;
  27.     }
  28.    
  29.     for (int i = N + 1; i < MAX; ++i) {
  30.         count += (S[i] - S[i - N]);
  31.         if (count == M) {
  32.             cout << i - N + 1;
  33.             return 0;
  34.         }
  35.     }
  36.     cout << "-1";
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment