Josif_tepe

Untitled

Feb 27th, 2026
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <stack>
  5. #include <cstring>
  6. #include <cmath>
  7. using namespace std;
  8. typedef long long ll;
  9. const int maxn = 1e6 + 1;
  10. const int INF = 2e9;
  11. bool is_prime[maxn];
  12. int main() {
  13.     ios_base::sync_with_stdio(false);
  14.    
  15.     memset(is_prime, true, sizeof is_prime);
  16.     is_prime[0] = false;
  17.     is_prime[1] = false;
  18.    
  19.     for(ll i = 2; i <= maxn; i++) {
  20.         if(is_prime[i]) {
  21.             for(ll j = i * i; j <= maxn; j += i) {
  22.                 is_prime[j] = false;
  23.             }
  24.         }
  25.     }
  26.    
  27.     int n, q;
  28.     cin >> n >> q;
  29.     vector<int> v(n);
  30.     vector<int> divs[n];
  31.     for(int i = 0; i < n; i++) {
  32.         cin >> v[i];
  33.        
  34.         int sq = sqrt(v[i]);
  35.        
  36.         for(int j = 1; j <= sq; j++) {
  37.             if(v[i] % j == 0) {
  38.                 if(is_prime[j]) {
  39.                     divs[i].push_back(j);
  40.                 }
  41.                
  42.                 if(j != v[i] / j) {
  43.                     if(is_prime[v[i] / j]) {
  44.                         divs[i].push_back(v[i] / j);
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.     }
  50.    
  51.     for(int i = 0; i < q; i++) {
  52.         int L, R, x;
  53.         cin >> L >> R >> x;
  54.         L--; R--;
  55.        
  56.         int res = INF;
  57.         for(int j = L; j <= R; j++) {
  58.             for(int c: divs[j]) {
  59.                 res = min(res, abs(c - x));
  60.             }
  61.         }
  62.         cout << res << " ";
  63.  
  64.     }
  65.    
  66.  
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment