Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int MX = 1e6 + 100; // максимум возможного x
- int main() {
- int n;
- cin >> n;
- vector<int> a(n + 1);
- for (int i = 1; i <= n; ++i) {
- cin >> a[i];
- if (a[i] >= i) {
- cout << -1 << endl; // условие a[i] < i должно выполняться
- return 0;
- }
- }
- vector<int> p(MX + 1, 0); // p[x] — сколько i таких, что x % i == a[i]
- for (int i = 1; i <= n; ++i) {
- for (int j = a[i]; j <= MX; j += i) {
- if (j % i == a[i]) {
- p[j]++;
- }
- }
- }
- for (int x = 0; x <= MX; ++x) {
- if (p[x] == n) {
- cout << x << endl;
- return 0;
- }
- }
- cout << -1 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement