Josif_tepe

Untitled

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