Advertisement
yeskendir_sultanov

F - ГЖО 8 класс

May 12th, 2025
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. const int MX = 1e6 + 100; // максимум возможного x
  6.  
  7. int main() {
  8.     int n;
  9.     cin >> n;
  10.  
  11.     vector<int> a(n + 1);
  12.     for (int i = 1; i <= n; ++i) {
  13.         cin >> a[i];
  14.         if (a[i] >= i) {
  15.             cout << -1 << endl; // условие a[i] < i должно выполняться
  16.             return 0;
  17.         }
  18.     }
  19.  
  20.     vector<int> p(MX + 1, 0); // p[x] — сколько i таких, что x % i == a[i]
  21.  
  22.     for (int i = 1; i <= n; ++i) {
  23.         for (int j = a[i]; j <= MX; j += i) {
  24.             if (j % i == a[i]) {
  25.                 p[j]++;
  26.             }
  27.         }
  28.     }
  29.  
  30.     for (int x = 0; x <= MX; ++x) {
  31.         if (p[x] == n) {
  32.             cout << x << endl;
  33.             return 0;
  34.         }
  35.     }
  36.  
  37.     cout << -1 << endl;
  38.     return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement