output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; const int N = 123456; const double eps = 1e-11; const int inf = (int)2e9; const int mod = (int)1e9 + 7; map<int, int> freq; map<int, int> first; map<int, int> last; int a[N]; int main() { int n, mx = -1; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", a + i); freq[a[i]]++; mx = max(mx, freq[a[i]]); if (first[a[i]] == 0) { first[a[i]] = i; } last[a[i]] = i; } int ans = inf; int num = inf; for (int i = 1; i <= n; i++) { if (freq[a[i]] == mx) { if (ans > last[a[i]] - first[a[i]] + 1) { ans = last[a[i]] - first[a[i]] + 1; num = a[i]; } } } printf("%d %d\n", first[num], last[num]); return 0; }
### Prompt Please create a solution in cpp to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 123456; const double eps = 1e-11; const int inf = (int)2e9; const int mod = (int)1e9 + 7; map<int, int> freq; map<int, int> first; map<int, int> last; int a[N]; int main() { int n, mx = -1; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", a + i); freq[a[i]]++; mx = max(mx, freq[a[i]]); if (first[a[i]] == 0) { first[a[i]] = i; } last[a[i]] = i; } int ans = inf; int num = inf; for (int i = 1; i <= n; i++) { if (freq[a[i]] == mx) { if (ans > last[a[i]] - first[a[i]] + 1) { ans = last[a[i]] - first[a[i]] + 1; num = a[i]; } } } printf("%d %d\n", first[num], last[num]); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1000010; int n, a[N], fi[N], ls[N], d[N]; int main() { scanf("%d", &n); for (int i = (int)1; i <= (int)n; ++i) scanf("%d", a + i); for (int i = (int)1; i <= (int)n; ++i) d[a[i]]++, ls[a[i]] = i; for (int i = (int)n; i >= (int)1; --i) fi[a[i]] = i; int maxx = 0, ans = 1e9, ans_i, ans_j; for (int i = (int)1; i <= (int)N - 10; ++i) maxx = max(d[i], maxx); for (int i = (int)1; i <= (int)N - 10; ++i) if (d[i] == maxx && fi[i] != 0 && ls[i] - fi[i] + 1 < ans) ans = ls[i] - fi[i] + 1, ans_i = fi[i], ans_j = ls[i]; cout << ans_i << ' ' << ans_j; return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1000010; int n, a[N], fi[N], ls[N], d[N]; int main() { scanf("%d", &n); for (int i = (int)1; i <= (int)n; ++i) scanf("%d", a + i); for (int i = (int)1; i <= (int)n; ++i) d[a[i]]++, ls[a[i]] = i; for (int i = (int)n; i >= (int)1; --i) fi[a[i]] = i; int maxx = 0, ans = 1e9, ans_i, ans_j; for (int i = (int)1; i <= (int)N - 10; ++i) maxx = max(d[i], maxx); for (int i = (int)1; i <= (int)N - 10; ++i) if (d[i] == maxx && fi[i] != 0 && ls[i] - fi[i] + 1 < ans) ans = ls[i] - fi[i] + 1, ans_i = fi[i], ans_j = ls[i]; cout << ans_i << ' ' << ans_j; return 0; } ```
#include <bits/stdc++.h> using namespace std; pair<int, pair<int, int> > arr[1000010]; int main() { int i, j, k, n, it; for (i = 0; i < 1000005; i++) { arr[i].first = 0; arr[i].second.first = 0; arr[i].second.second = 1000000000; } scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &it); arr[it].first++; if (arr[it].second.first == 0) { arr[it].second.first = i; } arr[it].second.second = i; } int maxi = 0; int l = 0; int r = 1000000000; for (i = 1; i <= 1000005; i++) { if (arr[i].first > maxi) { l = arr[i].second.first; r = arr[i].second.second; maxi = arr[i].first; } else if (arr[i].first == maxi && r - l > arr[i].second.second - arr[i].second.first) { l = arr[i].second.first; r = arr[i].second.second; } } printf("%d ", l); printf("%d\n", r); return 0; }
### Prompt Generate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; pair<int, pair<int, int> > arr[1000010]; int main() { int i, j, k, n, it; for (i = 0; i < 1000005; i++) { arr[i].first = 0; arr[i].second.first = 0; arr[i].second.second = 1000000000; } scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &it); arr[it].first++; if (arr[it].second.first == 0) { arr[it].second.first = i; } arr[it].second.second = i; } int maxi = 0; int l = 0; int r = 1000000000; for (i = 1; i <= 1000005; i++) { if (arr[i].first > maxi) { l = arr[i].second.first; r = arr[i].second.second; maxi = arr[i].first; } else if (arr[i].first == maxi && r - l > arr[i].second.second - arr[i].second.first) { l = arr[i].second.first; r = arr[i].second.second; } } printf("%d ", l); printf("%d\n", r); return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[1000005], vis[1000005]; int main() { int n, t, maxx = 0, minn = 0; int l, r; cin >> n; for (int i = 1; i <= n; i++) { cin >> t; vis[t]++; if (vis[t] == 1) { a[t] = i; } if (vis[t] > maxx) { maxx = vis[t]; minn = i - a[t]; l = a[t]; r = i; } else if (vis[t] == maxx) { if ((i - a[t]) < minn) { maxx = vis[t]; l = a[t]; r = i; minn = r - l; } } } cout << l << " " << r << endl; }
### Prompt Please create a solution in CPP to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[1000005], vis[1000005]; int main() { int n, t, maxx = 0, minn = 0; int l, r; cin >> n; for (int i = 1; i <= n; i++) { cin >> t; vis[t]++; if (vis[t] == 1) { a[t] = i; } if (vis[t] > maxx) { maxx = vis[t]; minn = i - a[t]; l = a[t]; r = i; } else if (vis[t] == maxx) { if ((i - a[t]) < minn) { maxx = vis[t]; l = a[t]; r = i; minn = r - l; } } } cout << l << " " << r << endl; } ```
#include <bits/stdc++.h> using namespace std; int a[1000010] = {0}; int l[1000010], r[1000010]; int main() { int n, k, Maxn, Max; scanf("%d", &n); Max = 0; Maxn = 0; for (int i = 1; i <= n; i++) { scanf("%d", &k); a[k]++; if (a[k] == 1) l[k] = i, r[k] = i; else r[k] = i; if (a[k] > Maxn) Maxn = a[k], Max = k; else if (a[k] == Maxn) { if (r[Max] - l[Max] > r[k] - l[k]) Max = k; } } printf("%d %d\n", l[Max], r[Max]); }
### Prompt Your task is to create a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[1000010] = {0}; int l[1000010], r[1000010]; int main() { int n, k, Maxn, Max; scanf("%d", &n); Max = 0; Maxn = 0; for (int i = 1; i <= n; i++) { scanf("%d", &k); a[k]++; if (a[k] == 1) l[k] = i, r[k] = i; else r[k] = i; if (a[k] > Maxn) Maxn = a[k], Max = k; else if (a[k] == Maxn) { if (r[Max] - l[Max] > r[k] - l[k]) Max = k; } } printf("%d %d\n", l[Max], r[Max]); } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int a[maxn]; struct aasd { int l; int r; int num; aasd() { l = -1; r = -1; num = 0; } } res[1000005]; int main() { int n; cin >> n; int x = 0; int ans; for (int i = 0; i < n; i++) { cin >> a[i]; res[a[i]].num++; if (res[a[i]].l == -1) { res[a[i]].l = res[a[i]].r = i; } else { res[a[i]].r = i; } } int len = 0; for (int i = 1; i <= 1000000; i++) { if (x < res[i].num || (x == res[i].num && len > res[i].r - res[i].l)) { x = res[i].num; ans = i; len = res[i].r - res[i].l; } } cout << res[ans].l + 1 << " " << res[ans].r + 1 << endl; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 100005; int a[maxn]; struct aasd { int l; int r; int num; aasd() { l = -1; r = -1; num = 0; } } res[1000005]; int main() { int n; cin >> n; int x = 0; int ans; for (int i = 0; i < n; i++) { cin >> a[i]; res[a[i]].num++; if (res[a[i]].l == -1) { res[a[i]].l = res[a[i]].r = i; } else { res[a[i]].r = i; } } int len = 0; for (int i = 1; i <= 1000000; i++) { if (x < res[i].num || (x == res[i].num && len > res[i].r - res[i].l)) { x = res[i].num; ans = i; len = res[i].r - res[i].l; } } cout << res[ans].l + 1 << " " << res[ans].r + 1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[200000]; int l[2000000], r[2000000]; int cnt[2000000]; int main() { int n; while (scanf("%d", &n) != EOF) { memset(l, -1, sizeof l); memset(r, -1, sizeof r); memset(cnt, 0, sizeof cnt); int maxCnt = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); cnt[a[i]]++; maxCnt = max(maxCnt, cnt[a[i]]); if (l[a[i]] == -1) { l[a[i]] = r[a[i]] = i; } l[a[i]] = min(l[a[i]], i); r[a[i]] = max(r[a[i]], i); } int ansl = -1, ansr = -1; for (int i = 0; i < 2000000; i++) { if (cnt[i] != maxCnt) continue; if (ansl == -1) { ansl = l[i]; ansr = r[i]; } else { if (ansr - ansl <= r[i] - l[i]) continue; ansl = l[i]; ansr = r[i]; } } printf("%d %d\n", ansl + 1, ansr + 1); } return 0; }
### Prompt Generate a CPP solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[200000]; int l[2000000], r[2000000]; int cnt[2000000]; int main() { int n; while (scanf("%d", &n) != EOF) { memset(l, -1, sizeof l); memset(r, -1, sizeof r); memset(cnt, 0, sizeof cnt); int maxCnt = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); cnt[a[i]]++; maxCnt = max(maxCnt, cnt[a[i]]); if (l[a[i]] == -1) { l[a[i]] = r[a[i]] = i; } l[a[i]] = min(l[a[i]], i); r[a[i]] = max(r[a[i]], i); } int ansl = -1, ansr = -1; for (int i = 0; i < 2000000; i++) { if (cnt[i] != maxCnt) continue; if (ansl == -1) { ansl = l[i]; ansr = r[i]; } else { if (ansr - ansl <= r[i] - l[i]) continue; ansl = l[i]; ansr = r[i]; } } printf("%d %d\n", ansl + 1, ansr + 1); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long flag[10000002]; void oka() { long long n, i, j, k, mn, mx = 0, cur, ans = 1e10; cin >> n; long long a[n]; long long l, r; map<long long, long long> ma; map<long long, long long> f, lst; for (i = 0; i < n; i++) cin >> a[i], flag[a[i]]++; for (i = 0; i < n; i++) { if (flag[a[i]] > mx) { mx = flag[a[i]]; } } for (i = 0; i < n; i++) { if (flag[a[i]] == mx) { ma[a[i]]++; if (ma[a[i]] == 1) f[a[i]] = i; if (ma[a[i]] == mx) { lst[a[i]] = i; mn = lst[a[i]] - f[a[i]]; if (mn < ans) { ans = mn; l = f[a[i]]; r = lst[a[i]]; } } } } cout << l + 1 << " " << r + 1 << endl; } int main() { long long t; t = 1; while (t--) { oka(); } }
### Prompt Your challenge is to write a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long flag[10000002]; void oka() { long long n, i, j, k, mn, mx = 0, cur, ans = 1e10; cin >> n; long long a[n]; long long l, r; map<long long, long long> ma; map<long long, long long> f, lst; for (i = 0; i < n; i++) cin >> a[i], flag[a[i]]++; for (i = 0; i < n; i++) { if (flag[a[i]] > mx) { mx = flag[a[i]]; } } for (i = 0; i < n; i++) { if (flag[a[i]] == mx) { ma[a[i]]++; if (ma[a[i]] == 1) f[a[i]] = i; if (ma[a[i]] == mx) { lst[a[i]] = i; mn = lst[a[i]] - f[a[i]]; if (mn < ans) { ans = mn; l = f[a[i]]; r = lst[a[i]]; } } } } cout << l + 1 << " " << r + 1 << endl; } int main() { long long t; t = 1; while (t--) { oka(); } } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<int> a(n), c(1000001); vector<pair<int, int> > b(1000001); for (int i = 0; i < 1000001; i++) { b[i].first = b[i].second = -1; c[i] = 0; } for (int i = 0; i < n; i++) { cin >> a[i]; c[a[i]]++; if (b[a[i]].first == -1) { b[a[i]].first = b[a[i]].second = i; } else { b[a[i]].second = i; } } int max_no = 0, pos = -1, l = 0, r = 0; for (int i = 0; i < 1000001; i++) { if (max_no < c[i]) { max_no = c[i]; l = b[i].first; r = b[i].second; } else if (max_no == c[i]) { if (r - l > b[i].second - b[i].first) { r = b[i].second; l = b[i].first; } } } cout << l + 1 << " " << r + 1; return 0; }
### Prompt In Cpp, your task is to solve the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<int> a(n), c(1000001); vector<pair<int, int> > b(1000001); for (int i = 0; i < 1000001; i++) { b[i].first = b[i].second = -1; c[i] = 0; } for (int i = 0; i < n; i++) { cin >> a[i]; c[a[i]]++; if (b[a[i]].first == -1) { b[a[i]].first = b[a[i]].second = i; } else { b[a[i]].second = i; } } int max_no = 0, pos = -1, l = 0, r = 0; for (int i = 0; i < 1000001; i++) { if (max_no < c[i]) { max_no = c[i]; l = b[i].first; r = b[i].second; } else if (max_no == c[i]) { if (r - l > b[i].second - b[i].first) { r = b[i].second; l = b[i].first; } } } cout << l + 1 << " " << r + 1; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; int main() { int n, k; std::vector<std::vector<int> > a(1000001); cin >> n; int x; for (int i = 0; i < n; i++) { cin >> x; a[x].push_back(i); } int maxnum = 0; int l = 0, r = 9999999; for (int i = 1; i < 1000001; i++) { sort((a[i]).begin(), (a[i]).end()); if (a[i].size() > maxnum) maxnum = a[i].size(); } for (int i = 1; i < 1000001; i++) { int x = a[i].size(); if (x < maxnum) continue; if (a[i][x - 1] - a[i][0] < r - l) { r = a[i][x - 1]; l = a[i][0]; } } cout << l + 1 << ' ' << r + 1; }
### Prompt In cpp, your task is to solve the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long mod = 1000000007; int main() { int n, k; std::vector<std::vector<int> > a(1000001); cin >> n; int x; for (int i = 0; i < n; i++) { cin >> x; a[x].push_back(i); } int maxnum = 0; int l = 0, r = 9999999; for (int i = 1; i < 1000001; i++) { sort((a[i]).begin(), (a[i]).end()); if (a[i].size() > maxnum) maxnum = a[i].size(); } for (int i = 1; i < 1000001; i++) { int x = a[i].size(); if (x < maxnum) continue; if (a[i][x - 1] - a[i][0] < r - l) { r = a[i][x - 1]; l = a[i][0]; } } cout << l + 1 << ' ' << r + 1; } ```
#include <bits/stdc++.h> using namespace std; int a, b[1000005] = {0}; pair<int, int> range[1000005]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a); b[a]++; range[a].second = i + 1; if (b[a] == 1) { range[a].first = i + 1; } } int max = -1; for (int i = 1; i <= 1000000; i++) { if (max < b[i]) max = b[i]; } vector<int> dig; for (int i = 1; i <= 1000000; i++) { if (max == b[i]) dig.push_back(i); } int l = -1, r = -1; for (int i = 0; i < dig.size(); i++) { if (l == -1 && r == -1) { l = range[dig[i]].first; r = range[dig[i]].second; } else { int L = range[dig[i]].first, R = range[dig[i]].second; if (R - L < r - l) { l = L; r = R; } } } printf("%d %d\n", l, r); return 0; }
### Prompt Please create a solution in Cpp to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a, b[1000005] = {0}; pair<int, int> range[1000005]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a); b[a]++; range[a].second = i + 1; if (b[a] == 1) { range[a].first = i + 1; } } int max = -1; for (int i = 1; i <= 1000000; i++) { if (max < b[i]) max = b[i]; } vector<int> dig; for (int i = 1; i <= 1000000; i++) { if (max == b[i]) dig.push_back(i); } int l = -1, r = -1; for (int i = 0; i < dig.size(); i++) { if (l == -1 && r == -1) { l = range[dig[i]].first; r = range[dig[i]].second; } else { int L = range[dig[i]].first, R = range[dig[i]].second; if (R - L < r - l) { l = L; r = R; } } } printf("%d %d\n", l, r); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { map<int, pair<int, int> > memo; int top_len = 1000000, top_cnt = 0, top_num = -1; for (int i = 1; i <= n; ++i) { int a; cin >> a; map<int, pair<int, int> >::iterator itr; if ((itr = memo.find(a)) != memo.end()) { itr->second.second++; } else { pair<map<int, pair<int, int> >::iterator, bool> t; t = memo.insert(make_pair(a, make_pair(i, 1))); itr = t.first; } if (itr->second.second > top_cnt || (itr->second.second == top_cnt && i - itr->second.first < top_len)) { top_len = i - itr->second.first; top_cnt = itr->second.second; top_num = a; } } int ans = memo.find(top_num)->second.first; cout << ans << " " << ans + top_len << endl; } return 0; }
### Prompt Generate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { map<int, pair<int, int> > memo; int top_len = 1000000, top_cnt = 0, top_num = -1; for (int i = 1; i <= n; ++i) { int a; cin >> a; map<int, pair<int, int> >::iterator itr; if ((itr = memo.find(a)) != memo.end()) { itr->second.second++; } else { pair<map<int, pair<int, int> >::iterator, bool> t; t = memo.insert(make_pair(a, make_pair(i, 1))); itr = t.first; } if (itr->second.second > top_cnt || (itr->second.second == top_cnt && i - itr->second.first < top_len)) { top_len = i - itr->second.first; top_cnt = itr->second.second; top_num = a; } } int ans = memo.find(top_num)->second.first; cout << ans << " " << ans + top_len << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long a[n + 5]; map<long long, long long> mp; map<long long, vector<long long>> mx; for (long long i = 1; i <= n; i++) { cin >> a[i]; mp[a[i]]++; mx[a[i]].push_back(i); } long long maxx = INT_MIN; for (long long i = 1; i <= n; i++) { maxx = max(mp[a[i]], maxx); } vector<pair<long long, long long>> v; for (long long i = 1; i <= n; i++) { if (mp[a[i]] == maxx) { v.push_back(make_pair(mx[a[i]][0], mx[a[i]][maxx - 1])); } } long long best = INT_MAX; for (long long i = 0; i < v.size(); i++) { if (v[i].second - v[i].first < best) { best = v[i].second - v[i].first; } } for (long long i = 0; i < v.size(); i++) { if (v[i].second - v[i].first == best) { cout << v[i].first << " " << v[i].second << "\n"; return 0; } } }
### Prompt Please formulate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long a[n + 5]; map<long long, long long> mp; map<long long, vector<long long>> mx; for (long long i = 1; i <= n; i++) { cin >> a[i]; mp[a[i]]++; mx[a[i]].push_back(i); } long long maxx = INT_MIN; for (long long i = 1; i <= n; i++) { maxx = max(mp[a[i]], maxx); } vector<pair<long long, long long>> v; for (long long i = 1; i <= n; i++) { if (mp[a[i]] == maxx) { v.push_back(make_pair(mx[a[i]][0], mx[a[i]][maxx - 1])); } } long long best = INT_MAX; for (long long i = 0; i < v.size(); i++) { if (v[i].second - v[i].first < best) { best = v[i].second - v[i].first; } } for (long long i = 0; i < v.size(); i++) { if (v[i].second - v[i].first == best) { cout << v[i].first << " " << v[i].second << "\n"; return 0; } } } ```
#include <bits/stdc++.h> using namespace std; int main() { vector<pair<int, int> > lr(1000001, make_pair(-1, -1)); vector<int> k(1000001); int n, l = 0, r = 1000001, maxk = 0; cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; k[x]++; if (lr[x].first == -1) lr[x].first = i + 1; lr[x].second = i + 1; } for (int i = 0; i < 1000001; ++i) if (k[i] > maxk) l = lr[i].first, r = lr[i].second, maxk = k[i]; else if (k[i] == maxk && lr[i].second - lr[i].first < r - l) l = lr[i].first, r = lr[i].second; cout << l << ' ' << r; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { vector<pair<int, int> > lr(1000001, make_pair(-1, -1)); vector<int> k(1000001); int n, l = 0, r = 1000001, maxk = 0; cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; k[x]++; if (lr[x].first == -1) lr[x].first = i + 1; lr[x].second = i + 1; } for (int i = 0; i < 1000001; ++i) if (k[i] > maxk) l = lr[i].first, r = lr[i].second, maxk = k[i]; else if (k[i] == maxk && lr[i].second - lr[i].first < r - l) l = lr[i].first, r = lr[i].second; cout << l << ' ' << r; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, x, us[1000001], mx, l, r, mn = 1 << 30; pair<int, int> p[1000001]; int main() { ios_base::sync_with_stdio(0); cin.tie(); cin >> n; for (int i = 1; i <= n; i++) { cin >> x; us[x]++; mx = max(mx, us[x]); if (!p[x].first) p[x].first = i, p[x].second = i; else p[x].second = i; } for (int i = 1; i <= 1000000; i++) if (us[i] == mx && p[i].second - p[i].first < mn) mn = p[i].second - p[i].first, l = p[i].first, r = p[i].second; cout << l << " " << r << endl; return 0; }
### Prompt Create a solution in Cpp for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, x, us[1000001], mx, l, r, mn = 1 << 30; pair<int, int> p[1000001]; int main() { ios_base::sync_with_stdio(0); cin.tie(); cin >> n; for (int i = 1; i <= n; i++) { cin >> x; us[x]++; mx = max(mx, us[x]); if (!p[x].first) p[x].first = i, p[x].second = i; else p[x].second = i; } for (int i = 1; i <= 1000000; i++) if (us[i] == mx && p[i].second - p[i].first < mn) mn = p[i].second - p[i].first, l = p[i].first, r = p[i].second; cout << l << " " << r << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int d[200010]; int a[200010]; int ok[400010]; long long freq[200010]; long long acc[200010]; int cop[200010]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } if (n == 1) { printf("0\n"); return 0; } vector<int> v; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { v.push_back(i); if (i * i != n) { v.push_back(n / i); } } } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); long long ans = 0; for (int i = 1; i < v.size(); i++) { int l = v[i]; int p = 0, fp = -1; for (int j = 0; j < l; j++) { d[j] = 0; } for (int j = 0; j < n; j++) { d[p] = max(d[p], a[j]); if (++p >= l) p -= l; } p = 0; for (int j = 0; j < n; j++) { ok[j] = a[j] == d[p]; if (!ok[j]) fp = j; if (++p >= l) p -= l; } p = l; while (p < n) { if (!cop[p]) { cop[p] = i; } p += l; } if (fp != -1) { for (int j = 0; j <= n + 1; j++) { freq[j] = acc[j] = 0; } p = fp; do { int np = p + 1, cnt = 0; if (np >= n) np -= n; while (ok[np]) { ++cnt; if (++np >= n) np -= n; } freq[1] += cnt; acc[1]++; acc[cnt + 1]--; p = np; } while (p != fp); long long s = 0, add = 0; for (int j = 1; j <= n; j++) { s += freq[j]; add += acc[j]; if (cop[j] == i) { ans += s; } s -= add; } } else { for (int j = 1; j < n; j++) { if (cop[j] == i) { ans += n; } } } } printf("%I64d\n", ans); return 0; }
### Prompt In cpp, your task is to solve the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int d[200010]; int a[200010]; int ok[400010]; long long freq[200010]; long long acc[200010]; int cop[200010]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } if (n == 1) { printf("0\n"); return 0; } vector<int> v; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { v.push_back(i); if (i * i != n) { v.push_back(n / i); } } } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); long long ans = 0; for (int i = 1; i < v.size(); i++) { int l = v[i]; int p = 0, fp = -1; for (int j = 0; j < l; j++) { d[j] = 0; } for (int j = 0; j < n; j++) { d[p] = max(d[p], a[j]); if (++p >= l) p -= l; } p = 0; for (int j = 0; j < n; j++) { ok[j] = a[j] == d[p]; if (!ok[j]) fp = j; if (++p >= l) p -= l; } p = l; while (p < n) { if (!cop[p]) { cop[p] = i; } p += l; } if (fp != -1) { for (int j = 0; j <= n + 1; j++) { freq[j] = acc[j] = 0; } p = fp; do { int np = p + 1, cnt = 0; if (np >= n) np -= n; while (ok[np]) { ++cnt; if (++np >= n) np -= n; } freq[1] += cnt; acc[1]++; acc[cnt + 1]--; p = np; } while (p != fp); long long s = 0, add = 0; for (int j = 1; j <= n; j++) { s += freq[j]; add += acc[j]; if (cop[j] == i) { ans += s; } s -= add; } } else { for (int j = 1; j < n; j++) { if (cop[j] == i) { ans += n; } } } } printf("%I64d\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * f; } struct node { int x, t; } q[200009]; bool cmp(node a, node b) { return a.x > b.x; } int n, k[200009], mx[200009], st[200009], ed[200009]; bool v[200009], hx[200009]; vector<int> g[200009]; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { n = read(); for (int i = 1; i <= n; i++) k[i] = read(); for (int o = 1; o <= n - 1; o++) if (n % o == 0) { for (int i = 0; i <= o - 1; i++) mx[i] = 0; memset(v, 0, sizeof(v)); for (int i = 1; i <= n; i++) mx[i % o] = max(mx[i % o], k[i]); for (int i = 1; i <= n; i++) if (mx[i % o] == k[i]) v[i] = 1; int a = 1; while (a <= n && v[a]) a++; if (a == n + 1) hx[o] = 1; else { int l = 0; for (int i = a; i <= n; i++) if (v[i]) l++; else if (l > 0) g[o].push_back(l), l = 0; for (int i = 1; i <= a; i++) if (v[i]) l++; else if (l > 0) g[o].push_back(l), l = 0; } } for (int o = 1; o <= n; o++) if (!g[o].empty()) sort(g[o].begin(), g[o].end()); for (int o = 1; o <= n; o++) st[o] = 1, ed[o] = g[o].size(); long long ans = 0; memset(v, 0, sizeof(v)); for (int o = 1; o <= n - 1; o++) { int tmp = gcd(n, o); if (hx[tmp]) ans += n; else { long long tmpans = tmp; while (st[tmp] <= ed[tmp] && g[tmp][st[tmp] - 1] < o) st[tmp]++; for (int i = st[tmp]; i <= ed[tmp]; i++) ans += g[tmp][i - 1] - o + 1; if (tmpans == ans) v[tmp] = 1; } } printf("%lld\n", ans); return 0; }
### Prompt Generate a CPP solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * f; } struct node { int x, t; } q[200009]; bool cmp(node a, node b) { return a.x > b.x; } int n, k[200009], mx[200009], st[200009], ed[200009]; bool v[200009], hx[200009]; vector<int> g[200009]; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { n = read(); for (int i = 1; i <= n; i++) k[i] = read(); for (int o = 1; o <= n - 1; o++) if (n % o == 0) { for (int i = 0; i <= o - 1; i++) mx[i] = 0; memset(v, 0, sizeof(v)); for (int i = 1; i <= n; i++) mx[i % o] = max(mx[i % o], k[i]); for (int i = 1; i <= n; i++) if (mx[i % o] == k[i]) v[i] = 1; int a = 1; while (a <= n && v[a]) a++; if (a == n + 1) hx[o] = 1; else { int l = 0; for (int i = a; i <= n; i++) if (v[i]) l++; else if (l > 0) g[o].push_back(l), l = 0; for (int i = 1; i <= a; i++) if (v[i]) l++; else if (l > 0) g[o].push_back(l), l = 0; } } for (int o = 1; o <= n; o++) if (!g[o].empty()) sort(g[o].begin(), g[o].end()); for (int o = 1; o <= n; o++) st[o] = 1, ed[o] = g[o].size(); long long ans = 0; memset(v, 0, sizeof(v)); for (int o = 1; o <= n - 1; o++) { int tmp = gcd(n, o); if (hx[tmp]) ans += n; else { long long tmpans = tmp; while (st[tmp] <= ed[tmp] && g[tmp][st[tmp] - 1] < o) st[tmp]++; for (int i = st[tmp]; i <= ed[tmp]; i++) ans += g[tmp][i - 1] - o + 1; if (tmpans == ans) v[tmp] = 1; } } printf("%lld\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int N, i_N[200009], a[200009]; bool bun[400009]; long long sol; int gcd(int a, int b) { int r; while (b) { r = a % b; a = b; b = r; } return a; } void add(int l, int d) { for (int i = 1; i <= l; i++) if (i_N[i] == d) sol += l - i + 1; } int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) { i_N[i] = gcd(i, N); scanf("%d", &a[i - 1]); } for (int d = 1; d < N; d++) if (N % d == 0) { bool macar = 0; for (int i = 0; i < d; i++) { int maxi = 0; for (int j = i; j < N; j += d) if (a[j] > maxi) maxi = a[j]; for (int j = i; j < N; j += d) { bun[j] = (a[j] == maxi); if (a[j] != maxi) macar = 1; } } if (!macar) { for (int i = 1; i < N; i++) if (i_N[i] == d) sol += N; continue; } for (int j = N; j < 2 * N; j++) bun[j] = bun[j - N]; int beg = 0; while (bun[beg]) beg++; for (int i = beg; i <= N; i++) if (bun[i]) { int j; for (j = i; j < 2 * N; j++) if (bun[j] == 0) break; j--; add(j - i + 1, d); i = j; } } printf("%I64d\n", sol); return 0; }
### Prompt Create a solution in CPP for the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int N, i_N[200009], a[200009]; bool bun[400009]; long long sol; int gcd(int a, int b) { int r; while (b) { r = a % b; a = b; b = r; } return a; } void add(int l, int d) { for (int i = 1; i <= l; i++) if (i_N[i] == d) sol += l - i + 1; } int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) { i_N[i] = gcd(i, N); scanf("%d", &a[i - 1]); } for (int d = 1; d < N; d++) if (N % d == 0) { bool macar = 0; for (int i = 0; i < d; i++) { int maxi = 0; for (int j = i; j < N; j += d) if (a[j] > maxi) maxi = a[j]; for (int j = i; j < N; j += d) { bun[j] = (a[j] == maxi); if (a[j] != maxi) macar = 1; } } if (!macar) { for (int i = 1; i < N; i++) if (i_N[i] == d) sol += N; continue; } for (int j = N; j < 2 * N; j++) bun[j] = bun[j - N]; int beg = 0; while (bun[beg]) beg++; for (int i = beg; i <= N; i++) if (bun[i]) { int j; for (j = i; j < 2 * N; j++) if (bun[j] == 0) break; j--; add(j - i + 1, d); i = j; } } printf("%I64d\n", sol); return 0; } ```
#include <bits/stdc++.h> using namespace std; int ma[200005], a[200005]; long long b[200005]; int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } int gd[200005]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) gd[i] = gcd(i, n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); long long ans = 0; for (int i = 1; i < n; i++) if (n % i == 0) { int d = i; for (int j = 0; j < d; j++) ma[j] = 0; for (int j = 0; j < n; j++) ma[j % d] = max(ma[j % d], a[j]); int ch = 1; for (int j = 0; j < n; j++) if (a[j] != ma[j % d]) ch = 0; b[0] = 0; for (int i = 1; i <= n; i++) if (gd[i] == d) b[i] = b[i - 1] + 1; else b[i] = b[i - 1]; for (int i = 1; i <= n; i++) b[i] += b[i - 1]; if (!ch) { int p1 = 0, p2 = 0; while (ma[p1 % d] == a[p1]) p1++; int j = 0; while (j < n) { if (ma[(p1 + j) % d] != a[(p1 + j) % n]) { j++; continue; } for (p2 = p1 + j; ma[p2 % d] == a[p2 % n]; p2++) ; ans += b[p2 - p1 - j]; j = p2 - p1 + 1; } } else { int x = 0; for (int i = 1; i < n; i++) if (gd[i] == d) x++; ans += 1LL * x * n; } } printf("%I64d\n", ans); }
### Prompt In Cpp, your task is to solve the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int ma[200005], a[200005]; long long b[200005]; int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } int gd[200005]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) gd[i] = gcd(i, n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); long long ans = 0; for (int i = 1; i < n; i++) if (n % i == 0) { int d = i; for (int j = 0; j < d; j++) ma[j] = 0; for (int j = 0; j < n; j++) ma[j % d] = max(ma[j % d], a[j]); int ch = 1; for (int j = 0; j < n; j++) if (a[j] != ma[j % d]) ch = 0; b[0] = 0; for (int i = 1; i <= n; i++) if (gd[i] == d) b[i] = b[i - 1] + 1; else b[i] = b[i - 1]; for (int i = 1; i <= n; i++) b[i] += b[i - 1]; if (!ch) { int p1 = 0, p2 = 0; while (ma[p1 % d] == a[p1]) p1++; int j = 0; while (j < n) { if (ma[(p1 + j) % d] != a[(p1 + j) % n]) { j++; continue; } for (p2 = p1 + j; ma[p2 % d] == a[p2 % n]; p2++) ; ans += b[p2 - p1 - j]; j = p2 - p1 + 1; } } else { int x = 0; for (int i = 1; i < n; i++) if (gd[i] == d) x++; ans += 1LL * x * n; } } printf("%I64d\n", ans); } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 2 * 105000; int n, pn; int a[MAXN]; int curmax[200][MAXN]; vector<int> v; bool ok[MAXN]; int mx; long long ans = 0; int s[MAXN]; int g[MAXN]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } vector<int> lens; int pref[MAXN]; void fact(int n) { for (int i = 1; i < n; i++) { if (n % i == 0) { v.push_back(i); } } } set<int> st; int main() { cin >> n; mx = -5; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] > mx) mx = a[i]; st.insert(a[i]); } for (int i = 1; i < n; i++) { g[i] = gcd(i, n); } if ((int)st.size() == 1) { for (int i = 1; i < n; i++) { ans += n; } cout << ans << endl; return 0; } fact(n); pn = (int)v.size(); for (int j = 0; j < pn; j++) { int cur = v[j]; if (cur == 1) { for (int i = 0; i < n; i++) { if (a[i] == mx) { ok[i] = true; } else ok[i] = false; } } else { for (int i = 0; i < n; i++) { if (curmax[j][i] == 0) { int mx = a[i]; int go = (i + cur) % n; while (go != i) { if (a[go] > mx) mx = a[go]; go = (go + cur) % n; } go = (i + cur) % n; curmax[j][i] = mx; while (go != i) { curmax[j][go] = mx; go = (go + cur) % n; } } if (a[i] == curmax[j][i]) { ok[i] = true; } else { ok[i] = false; } } } for (int i = 0; i < n; i++) { if (ok[i]) { if (i == 0) pref[i] = 1; else pref[i] = 1 + pref[i - 1]; } else { pref[i] = 0; } } memset(s, 0, sizeof(s)); for (int i = 0; i < n; i++) { int total = pref[i]; if (total == i + 1) { total += pref[n - 1]; if (total >= n) { total = n; } } s[total]++; } for (int i = n; i >= 1; i--) { s[i] += s[i + 1]; } int p = 1; for (int i = 1; i < n; i++) { if (g[i] == cur) { while (p < i) { p++; } ans += s[p]; } } } cout << ans << endl; return 0; }
### Prompt In Cpp, your task is to solve the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 2 * 105000; int n, pn; int a[MAXN]; int curmax[200][MAXN]; vector<int> v; bool ok[MAXN]; int mx; long long ans = 0; int s[MAXN]; int g[MAXN]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } vector<int> lens; int pref[MAXN]; void fact(int n) { for (int i = 1; i < n; i++) { if (n % i == 0) { v.push_back(i); } } } set<int> st; int main() { cin >> n; mx = -5; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] > mx) mx = a[i]; st.insert(a[i]); } for (int i = 1; i < n; i++) { g[i] = gcd(i, n); } if ((int)st.size() == 1) { for (int i = 1; i < n; i++) { ans += n; } cout << ans << endl; return 0; } fact(n); pn = (int)v.size(); for (int j = 0; j < pn; j++) { int cur = v[j]; if (cur == 1) { for (int i = 0; i < n; i++) { if (a[i] == mx) { ok[i] = true; } else ok[i] = false; } } else { for (int i = 0; i < n; i++) { if (curmax[j][i] == 0) { int mx = a[i]; int go = (i + cur) % n; while (go != i) { if (a[go] > mx) mx = a[go]; go = (go + cur) % n; } go = (i + cur) % n; curmax[j][i] = mx; while (go != i) { curmax[j][go] = mx; go = (go + cur) % n; } } if (a[i] == curmax[j][i]) { ok[i] = true; } else { ok[i] = false; } } } for (int i = 0; i < n; i++) { if (ok[i]) { if (i == 0) pref[i] = 1; else pref[i] = 1 + pref[i - 1]; } else { pref[i] = 0; } } memset(s, 0, sizeof(s)); for (int i = 0; i < n; i++) { int total = pref[i]; if (total == i + 1) { total += pref[n - 1]; if (total >= n) { total = n; } } s[total]++; } for (int i = n; i >= 1; i--) { s[i] += s[i + 1]; } int p = 1; for (int i = 1; i < n; i++) { if (g[i] == cur) { while (p < i) { p++; } ans += s[p]; } } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MN = 200200; int n; int a[MN]; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long full(int c, int d) { long long re = 0; for (int dd = d; dd <= c; dd += d) { if (gcd(dd, n) != d) continue; re += n; } return re; } long long cou(int c, int d) { long long re = 0; for (int dd = d; dd <= c; dd += d) { if (gcd(dd, n) != d) continue; re += (c - dd + 1); } return re; } int md[MN]; bool ok[MN]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", a + i); } long long sm = 0; for (int l = 1; l < n; l++) { if (n % l != 0) continue; fill_n(md, n, 0); int t = n / l; for (int j = 0; j < t; j++) { for (int k = 0; k < l; k++) { md[k] = max(md[k], a[j * l + k]); } } for (int j = 0; j < t; j++) { for (int k = 0; k < l; k++) { ok[j * l + k] = (a[j * l + k] == md[k]); } } int st = -1; for (int j = 0; j < n; j++) { if (!ok[j]) { st = j; break; } } if (st == -1) { sm += full(n, l); continue; } int ba = 1; for (int j = 1; j < n; j++) { int k = st + j; if (n <= k) k -= n; if (!ok[k]) { sm += cou(j - ba, l); ba = j + 1; } } sm += cou(n - ba, l); } cout << sm << endl; return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MN = 200200; int n; int a[MN]; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long full(int c, int d) { long long re = 0; for (int dd = d; dd <= c; dd += d) { if (gcd(dd, n) != d) continue; re += n; } return re; } long long cou(int c, int d) { long long re = 0; for (int dd = d; dd <= c; dd += d) { if (gcd(dd, n) != d) continue; re += (c - dd + 1); } return re; } int md[MN]; bool ok[MN]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", a + i); } long long sm = 0; for (int l = 1; l < n; l++) { if (n % l != 0) continue; fill_n(md, n, 0); int t = n / l; for (int j = 0; j < t; j++) { for (int k = 0; k < l; k++) { md[k] = max(md[k], a[j * l + k]); } } for (int j = 0; j < t; j++) { for (int k = 0; k < l; k++) { ok[j * l + k] = (a[j * l + k] == md[k]); } } int st = -1; for (int j = 0; j < n; j++) { if (!ok[j]) { st = j; break; } } if (st == -1) { sm += full(n, l); continue; } int ba = 1; for (int j = 1; j < n; j++) { int k = st + j; if (n <= k) k -= n; if (!ok[k]) { sm += cou(j - ba, l); ba = j + 1; } } sm += cou(n - ba, l); } cout << sm << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } int idx[200001] = {0}; vector<int> g[160]; int dp[160][100001] = {0}; int len[160][200001] = {0}; int nless(vector<int>& v, int val) { if (!(int)v.size()) return 0; if (v[0] > val) return 0; int l = 0, r = (int)v.size() - 1; while (r - l + 1 > 2) { int x = (l + r) / 2; if (v[x] > val) { r = x; } else { l = x; } } if (r - l + 1 == 2) { if (v[r] <= val) { return r + 1; } else { return l + 1; } } if (r - l + 1 == 1) { return l + 1; } } void solve() { int n; cin >> n; vector<int> v(n); for (int j = 0; j < n; j++) cin >> v[j]; int num = 1; vector<int> factors; factors.push_back(1); for (int j = 2; j <= sqrt(n); j++) { if (n % j == 0) { factors.push_back(j); num++; if (j * j != n) { factors.push_back(n / j); num++; } } } sort(factors.begin(), factors.end()); for (int j = 0; j < (int)factors.size(); j++) { idx[factors[j]] = j; } for (int j = 1; j < n; j++) { g[idx[gcd(j, n)]].push_back(j); } for (int j = n - 1; j >= 0; j--) { for (int i = 0; i < num; i++) { dp[i][j % factors[i]] = max(dp[i][j % factors[i]], v[j]); } } for (int j = n - 1; j >= 0; j--) { for (int i = 0; i < num; i++) { len[i][j] = (v[j] == dp[i][j % factors[i]]) * (1 + len[i][j + 1]); } } for (int j = n - 1; j >= 0; j--) { for (int i = 0; i < num; i++) { len[i][j] = min( n - 1, (v[j] == dp[i][j % factors[i]]) * (1 + len[i][(j + 1) % n])); } } long long int ans = 0; for (int j = 0; j < n; j++) { for (int i = 0; i < num; i++) { int cnt = nless(g[i], len[i][j]); ans += cnt; } } cout << ans << "\n"; return; } signed main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) { solve(); } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } int idx[200001] = {0}; vector<int> g[160]; int dp[160][100001] = {0}; int len[160][200001] = {0}; int nless(vector<int>& v, int val) { if (!(int)v.size()) return 0; if (v[0] > val) return 0; int l = 0, r = (int)v.size() - 1; while (r - l + 1 > 2) { int x = (l + r) / 2; if (v[x] > val) { r = x; } else { l = x; } } if (r - l + 1 == 2) { if (v[r] <= val) { return r + 1; } else { return l + 1; } } if (r - l + 1 == 1) { return l + 1; } } void solve() { int n; cin >> n; vector<int> v(n); for (int j = 0; j < n; j++) cin >> v[j]; int num = 1; vector<int> factors; factors.push_back(1); for (int j = 2; j <= sqrt(n); j++) { if (n % j == 0) { factors.push_back(j); num++; if (j * j != n) { factors.push_back(n / j); num++; } } } sort(factors.begin(), factors.end()); for (int j = 0; j < (int)factors.size(); j++) { idx[factors[j]] = j; } for (int j = 1; j < n; j++) { g[idx[gcd(j, n)]].push_back(j); } for (int j = n - 1; j >= 0; j--) { for (int i = 0; i < num; i++) { dp[i][j % factors[i]] = max(dp[i][j % factors[i]], v[j]); } } for (int j = n - 1; j >= 0; j--) { for (int i = 0; i < num; i++) { len[i][j] = (v[j] == dp[i][j % factors[i]]) * (1 + len[i][j + 1]); } } for (int j = n - 1; j >= 0; j--) { for (int i = 0; i < num; i++) { len[i][j] = min( n - 1, (v[j] == dp[i][j % factors[i]]) * (1 + len[i][(j + 1) % n])); } } long long int ans = 0; for (int j = 0; j < n; j++) { for (int i = 0; i < num; i++) { int cnt = nless(g[i], len[i][j]); ans += cnt; } } cout << ans << "\n"; return; } signed main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long INF = mod * mod; const long double eps = 1e-6; const long double pi = acos(-1.0); int gcd(int a, int b) { if (a < b) swap(a, b); while (b) { int r = a % b; a = b; b = r; } return a; } vector<int> divisor(int x) { vector<int> ret; for (int i = 1; i <= 1000; i++) { if (x % i == 0) { ret.push_back(i); ret.push_back(x / i); } } sort(ret.begin(), ret.end()); ret.erase(unique(ret.begin(), ret.end()), ret.end()); return ret; } bool allsame(vector<int> &a) { for (int i = 0; i < (int)a.size() - 1; i++) { if (a[i] != a[i + 1]) return false; } return true; } void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; if (allsame(a)) { long long ans = (long long)n * (n - 1); cout << ans << endl; return; } vector<int> d = divisor(n); d.pop_back(); long long ans = 0; for (int i = 0; i < d.size(); i++) { vector<int> non; int k = d[i]; vector<int> ma(k, 0); for (int j = 0; j < n; j++) { ma[j % k] = max(ma[j % k], a[j]); } for (int j = 0; j < n; j++) { if (ma[j % k] != a[j]) non.push_back(j); } int sz = non.size(); for (int j = 0; j < sz; j++) non.push_back(non[j] + n); int u = n / k; vector<bool> p(u, true); for (int j = 0; j < d.size(); j++) { if (u % d[j] || d[j] == 1) continue; for (int l = d[j]; l < u; l += d[j]) p[l] = false; } vector<int> rc(u, 0); for (int j = 0; j < u - 1; j++) { rc[j + 1] += rc[j]; if (p[j + 1]) rc[j + 1]++; } if (non.empty()) { ans += (long long)n * rc[u - 1]; continue; } int le = 0; for (int j = 0; j < n; j++) { if (j > non[le]) le++; if (j == non[le]) continue; int dif = non[le] - j; dif /= k; ans += rc[dif]; } } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long INF = mod * mod; const long double eps = 1e-6; const long double pi = acos(-1.0); int gcd(int a, int b) { if (a < b) swap(a, b); while (b) { int r = a % b; a = b; b = r; } return a; } vector<int> divisor(int x) { vector<int> ret; for (int i = 1; i <= 1000; i++) { if (x % i == 0) { ret.push_back(i); ret.push_back(x / i); } } sort(ret.begin(), ret.end()); ret.erase(unique(ret.begin(), ret.end()), ret.end()); return ret; } bool allsame(vector<int> &a) { for (int i = 0; i < (int)a.size() - 1; i++) { if (a[i] != a[i + 1]) return false; } return true; } void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; if (allsame(a)) { long long ans = (long long)n * (n - 1); cout << ans << endl; return; } vector<int> d = divisor(n); d.pop_back(); long long ans = 0; for (int i = 0; i < d.size(); i++) { vector<int> non; int k = d[i]; vector<int> ma(k, 0); for (int j = 0; j < n; j++) { ma[j % k] = max(ma[j % k], a[j]); } for (int j = 0; j < n; j++) { if (ma[j % k] != a[j]) non.push_back(j); } int sz = non.size(); for (int j = 0; j < sz; j++) non.push_back(non[j] + n); int u = n / k; vector<bool> p(u, true); for (int j = 0; j < d.size(); j++) { if (u % d[j] || d[j] == 1) continue; for (int l = d[j]; l < u; l += d[j]) p[l] = false; } vector<int> rc(u, 0); for (int j = 0; j < u - 1; j++) { rc[j + 1] += rc[j]; if (p[j + 1]) rc[j + 1]++; } if (non.empty()) { ans += (long long)n * rc[u - 1]; continue; } int le = 0; for (int j = 0; j < n; j++) { if (j > non[le]) le++; if (j == non[le]) continue; int dif = non[le] - j; dif /= k; ans += rc[dif]; } } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAX_N = 2e5 + 5; int gcd(int p, int q) { if (q == 0) return p; return gcd(q, p % q); } int length; long long solve_cyc(int k) { long long lenc = 0; for (int s = k; s < length; s += k) { if (gcd(length, s) == k) { lenc++; } } return lenc * length; } long long solve_block(int b, int k) { long long ans = 0; for (int s = k; s <= b; s += k) { if (gcd(length, s) == k) { ans += b - s + 1; } } return ans; } int arr[MAX_N]; int ok[MAX_N]; int dom[MAX_N]; long long solve_for(int k) { for (int i = 0; i < k; i++) { dom[i] = 0; } for (int i = 0; i < length; i++) { dom[i % k] = max(dom[i % k], arr[i]); } for (int i = 0; i < length; i++) { ok[i] = (dom[i % k] == arr[i]); } vector<int> blocks; for (int i = 0; i < length; i++) { if (ok[i]) { if (i == 0 || !ok[i - 1]) { blocks.push_back(0); } blocks.back()++; } } if (ok[length - 1] && ok[0] && (int)blocks.size() >= 2) { blocks[0] += blocks.back(); blocks.pop_back(); } if (!blocks.empty() && blocks[0] == length) { return solve_cyc(k); } long long ans = 0; for (int block : blocks) { ans += solve_block(block, k); } return ans; } int main() { ios::sync_with_stdio(false); cin >> length; for (int i = 0; i < length; i++) { cin >> arr[i]; } set<int> divs; for (int i = 1; i * i <= length; i++) { if (length % i == 0) { divs.insert(i); divs.insert(length / i); } } long long ans = 0; for (int d : divs) { if (d != length) { ans += solve_for(d); } } cout << ans << endl; }
### Prompt Generate a cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX_N = 2e5 + 5; int gcd(int p, int q) { if (q == 0) return p; return gcd(q, p % q); } int length; long long solve_cyc(int k) { long long lenc = 0; for (int s = k; s < length; s += k) { if (gcd(length, s) == k) { lenc++; } } return lenc * length; } long long solve_block(int b, int k) { long long ans = 0; for (int s = k; s <= b; s += k) { if (gcd(length, s) == k) { ans += b - s + 1; } } return ans; } int arr[MAX_N]; int ok[MAX_N]; int dom[MAX_N]; long long solve_for(int k) { for (int i = 0; i < k; i++) { dom[i] = 0; } for (int i = 0; i < length; i++) { dom[i % k] = max(dom[i % k], arr[i]); } for (int i = 0; i < length; i++) { ok[i] = (dom[i % k] == arr[i]); } vector<int> blocks; for (int i = 0; i < length; i++) { if (ok[i]) { if (i == 0 || !ok[i - 1]) { blocks.push_back(0); } blocks.back()++; } } if (ok[length - 1] && ok[0] && (int)blocks.size() >= 2) { blocks[0] += blocks.back(); blocks.pop_back(); } if (!blocks.empty() && blocks[0] == length) { return solve_cyc(k); } long long ans = 0; for (int block : blocks) { ans += solve_block(block, k); } return ans; } int main() { ios::sync_with_stdio(false); cin >> length; for (int i = 0; i < length; i++) { cin >> arr[i]; } set<int> divs; for (int i = 1; i * i <= length; i++) { if (length % i == 0) { divs.insert(i); divs.insert(length / i); } } long long ans = 0; for (int d : divs) { if (d != length) { ans += solve_for(d); } } cout << ans << endl; } ```
#include <bits/stdc++.h> using namespace std; bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; long long ln, lk, lm; int a[200005]; int gcd1(int x, int y) { int z = y; while (x % y != 0) { z = x % y; x = y; y = z; } return z; } unordered_map<int, int> id; int cnt; int dp[170][200005]; void cal(int len) { if (len == n) return; if (id.find(len) != id.end()) return; id[len] = ++cnt; vector<int> v(len); for (int i = 0; i < len; i++) { for (int j = i; j < n; j += len) v[i] = max(v[i], a[j]); } for (int i = n - 1 + n - 1, pre = 0; i >= 0; i--) { int mx = v[i % len]; int fg = mx == a[i % n]; if (fg) pre++; else pre = 0; if (i < n && pre >= len) { dp[cnt][min(pre, n - 1)]++; } } for (int i = n - 2; i >= len; i--) dp[cnt][i] += dp[cnt][i + 1]; } void fmain() { scanf("%d", &n); for (int(i) = 0; (i) < (int)(n); (i)++) scanf("%d", a + i); for (int i = 1; i * i <= n; i++) if (n % i == 0) { cal(i); cal(n / i); } long long ans = 0; for (int len = 1; len < n; len++) { int g = gcd1(len, n); ans += dp[id[g]][len]; } printf("%lld\n", ans); } int main() { fmain(); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; long long ln, lk, lm; int a[200005]; int gcd1(int x, int y) { int z = y; while (x % y != 0) { z = x % y; x = y; y = z; } return z; } unordered_map<int, int> id; int cnt; int dp[170][200005]; void cal(int len) { if (len == n) return; if (id.find(len) != id.end()) return; id[len] = ++cnt; vector<int> v(len); for (int i = 0; i < len; i++) { for (int j = i; j < n; j += len) v[i] = max(v[i], a[j]); } for (int i = n - 1 + n - 1, pre = 0; i >= 0; i--) { int mx = v[i % len]; int fg = mx == a[i % n]; if (fg) pre++; else pre = 0; if (i < n && pre >= len) { dp[cnt][min(pre, n - 1)]++; } } for (int i = n - 2; i >= len; i--) dp[cnt][i] += dp[cnt][i + 1]; } void fmain() { scanf("%d", &n); for (int(i) = 0; (i) < (int)(n); (i)++) scanf("%d", a + i); for (int i = 1; i * i <= n; i++) if (n % i == 0) { cal(i); cal(n / i); } long long ans = 0; for (int len = 1; len < n; len++) { int g = gcd1(len, n); ans += dp[id[g]][len]; } printf("%lld\n", ans); } int main() { fmain(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; else return (gcd(b, a % b)); } const int N = 1000100; int n, a[N], c[N], gg[N]; bool u[N]; int inc(int x) { if (x + 1 == n) return 0; else return (x + 1); } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) gg[i] = gcd(i, n); long long ans = 0; for (int g = 1; g < n; g++) { if (n % g != 0) continue; for (int i = 0; i < n; i++) { u[i] = false; c[i] = 0; } for (int i = 0; i < g; i++) { int mx = 0; for (int j = i; j < n; j += g) mx = max(mx, a[j]); for (int j = i; j < n; j += g) if (a[j] == mx) u[j] = true; } bool any = false; for (int j = 0; j < n;) { int r = inc(j); if (u[j]) { j++; continue; } any = true; int len = 0; while (u[r]) { len++; r = inc(r); } for (int i = 1; i <= len; i++) c[i] += len - i + 1; if (r <= j) break; j = r; } if (!any) for (int i = 1; i <= n; i++) c[i] += n; for (int i = 1; i <= n; i++) if (gg[i] == g) ans += c[i]; } cout << ans << endl; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; else return (gcd(b, a % b)); } const int N = 1000100; int n, a[N], c[N], gg[N]; bool u[N]; int inc(int x) { if (x + 1 == n) return 0; else return (x + 1); } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) gg[i] = gcd(i, n); long long ans = 0; for (int g = 1; g < n; g++) { if (n % g != 0) continue; for (int i = 0; i < n; i++) { u[i] = false; c[i] = 0; } for (int i = 0; i < g; i++) { int mx = 0; for (int j = i; j < n; j += g) mx = max(mx, a[j]); for (int j = i; j < n; j += g) if (a[j] == mx) u[j] = true; } bool any = false; for (int j = 0; j < n;) { int r = inc(j); if (u[j]) { j++; continue; } any = true; int len = 0; while (u[r]) { len++; r = inc(r); } for (int i = 1; i <= len; i++) c[i] += len - i + 1; if (r <= j) break; j = r; } if (!any) for (int i = 1; i <= n; i++) c[i] += n; for (int i = 1; i <= n; i++) if (gg[i] == g) ans += c[i]; } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; int n, a[maxn], b[maxn << 1], c[maxn], mx[maxn], gc[maxn]; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) gc[i] = gcd(i, n); long long ans = 0; for (int g = 1; g < n; g++) if (n % g == 0) { for (int i = 1; i <= n; i++) c[i] = c[i - 1] + (gc[i] == g); for (int i = 0; i < g; i++) mx[i] = a[i]; for (int i = g; i < n; i++) mx[i % g] = max(mx[i % g], a[i]); for (int i = 0; i < n; i++) b[i] = b[i + n] = (mx[i % g] == a[i]); for (int i = n + n - 2; i >= 0; i--) if (b[i]) b[i] += b[i + 1]; for (int i = 0; i < n; i++) ans += c[min(n - 1, b[i])]; } printf("%lld", ans); return 0; }
### Prompt Please create a solution in cpp to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 200010; int n, a[maxn], b[maxn << 1], c[maxn], mx[maxn], gc[maxn]; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) gc[i] = gcd(i, n); long long ans = 0; for (int g = 1; g < n; g++) if (n % g == 0) { for (int i = 1; i <= n; i++) c[i] = c[i - 1] + (gc[i] == g); for (int i = 0; i < g; i++) mx[i] = a[i]; for (int i = g; i < n; i++) mx[i % g] = max(mx[i % g], a[i]); for (int i = 0; i < n; i++) b[i] = b[i + n] = (mx[i % g] == a[i]); for (int i = n + n - 2; i >= 0; i--) if (b[i]) b[i] += b[i + 1]; for (int i = 0; i < n; i++) ans += c[min(n - 1, b[i])]; } printf("%lld", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; long long m; vector<long long> a; int gcd(int x, int y) { return (y ? gcd(y, x % y) : x); } int main() { ios_base::sync_with_stdio(false); cin >> n; a.resize(n); for (int i = 0; i < n; ++i) cin >> a[i]; m = 0; for (int d = 1; d < n; ++d) { if (n % d == 0) { vector<long long> b(d, 0); for (int i = 0; i < n; ++i) b[i % d] = max(b[i % d], a[i]); vector<long long> c(n, 0); for (int i = 0; i < n; ++i) if (a[i] == b[i % d]) c[i] = 1; bool fl = true; for (int i = 0; i < n; ++i) if (c[i] == 0) fl = false; if (fl) { for (int j = 1; j * d < n; ++j) if (gcd(n, j * d) == d) m += n; } else { vector<long long> w; int be = 0; while (c[be] == 1) ++be; vector<long long> cc; for (int i = be; i < n; ++i) cc.push_back(c[i]); for (int i = 0; i < be; ++i) cc.push_back(c[i]); swap(c, cc); for (int i = 1; i < n; ++i) { if (c[i] == 1 && c[i - 1] == 0) w.push_back(1); else if (c[i] == 1 && c[i - 1] == 1) ++w.back(); } for (int i = 0; i < w.size(); ++i) { for (int j = 1; j * d < n && j * d <= w[i]; ++j) { if (gcd(n, j * d) == d) m += (w[i] - j * d + 1); } } } } } cout << m << endl; return 0; }
### Prompt Please formulate a cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; long long m; vector<long long> a; int gcd(int x, int y) { return (y ? gcd(y, x % y) : x); } int main() { ios_base::sync_with_stdio(false); cin >> n; a.resize(n); for (int i = 0; i < n; ++i) cin >> a[i]; m = 0; for (int d = 1; d < n; ++d) { if (n % d == 0) { vector<long long> b(d, 0); for (int i = 0; i < n; ++i) b[i % d] = max(b[i % d], a[i]); vector<long long> c(n, 0); for (int i = 0; i < n; ++i) if (a[i] == b[i % d]) c[i] = 1; bool fl = true; for (int i = 0; i < n; ++i) if (c[i] == 0) fl = false; if (fl) { for (int j = 1; j * d < n; ++j) if (gcd(n, j * d) == d) m += n; } else { vector<long long> w; int be = 0; while (c[be] == 1) ++be; vector<long long> cc; for (int i = be; i < n; ++i) cc.push_back(c[i]); for (int i = 0; i < be; ++i) cc.push_back(c[i]); swap(c, cc); for (int i = 1; i < n; ++i) { if (c[i] == 1 && c[i - 1] == 0) w.push_back(1); else if (c[i] == 1 && c[i - 1] == 1) ++w.back(); } for (int i = 0; i < w.size(); ++i) { for (int j = 1; j * d < n && j * d <= w[i]; ++j) { if (gcd(n, j * d) == d) m += (w[i] - j * d + 1); } } } } } cout << m << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const long double PI = acos(-1.0); const int MAXN = 200001; int ad[MAXN]; int an[MAXN]; int g[MAXN], gc = 0; int dv[MAXN]; int a[MAXN]; bool b[MAXN]; int gcd(int a, int b) { while (b != 0) { int c = a % b; a = b; b = c; } return a; } int main() { int n; cin >> n; cin >> a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; dv[i] = gcd(i, n); g[gc++] = dv[i]; } sort(g, g + gc); gc = unique(g, g + gc) - g; long long ans = 0; for (int it = 0; it < gc; it++) { int d = g[it]; for (int i = 1; i <= n; i++) ad[i] = 0; for (int i = 0; i < d; i++) { int mx = 0; for (int j = i; j < n; j += d) mx = max(mx, a[j]); for (int j = i; j < n; j += d) b[j] = (a[j] == mx); } int st = -1; for (int i = 0; i < n; i++) { if (!b[i]) st = i; } if (st == -1) { for (int i = 1; i <= n; i++) an[i] = n; } else { int cur = st, ln = 0; do { cur = (cur + 1) % n; if (b[cur]) ln++; else { ad[ln]++; ln = 0; } } while (cur != st); int ss = 0; an[n] = 0; for (int i = n - 1; i >= 1; i--) { ss += ad[i]; an[i] = an[i + 1] + ss; } } for (int i = 1; i <= n; i++) { if (dv[i] == d) ans += an[i]; } } cout << ans << endl; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long double PI = acos(-1.0); const int MAXN = 200001; int ad[MAXN]; int an[MAXN]; int g[MAXN], gc = 0; int dv[MAXN]; int a[MAXN]; bool b[MAXN]; int gcd(int a, int b) { while (b != 0) { int c = a % b; a = b; b = c; } return a; } int main() { int n; cin >> n; cin >> a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; dv[i] = gcd(i, n); g[gc++] = dv[i]; } sort(g, g + gc); gc = unique(g, g + gc) - g; long long ans = 0; for (int it = 0; it < gc; it++) { int d = g[it]; for (int i = 1; i <= n; i++) ad[i] = 0; for (int i = 0; i < d; i++) { int mx = 0; for (int j = i; j < n; j += d) mx = max(mx, a[j]); for (int j = i; j < n; j += d) b[j] = (a[j] == mx); } int st = -1; for (int i = 0; i < n; i++) { if (!b[i]) st = i; } if (st == -1) { for (int i = 1; i <= n; i++) an[i] = n; } else { int cur = st, ln = 0; do { cur = (cur + 1) % n; if (b[cur]) ln++; else { ad[ln]++; ln = 0; } } while (cur != st); int ss = 0; an[n] = 0; for (int i = n - 1; i >= 1; i--) { ss += ad[i]; an[i] = an[i + 1] + ss; } } for (int i = 1; i <= n; i++) { if (dv[i] == d) ans += an[i]; } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 2 * 105000; int n, pn; int a[MAXN]; int curmax[200][MAXN]; vector<int> v; bool ok[MAXN]; int mx; long long ans = 0; int s[MAXN]; int g[MAXN]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } vector<int> lens; int pref[MAXN]; void fact(int n) { for (int i = 1; i < n; i++) { if (n % i == 0) { v.push_back(i); } } } set<int> st; int main() { cin >> n; mx = -5; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] > mx) mx = a[i]; st.insert(a[i]); } for (int i = 1; i < n; i++) { g[i] = gcd(i, n); } fact(n); pn = (int)v.size(); for (int j = 0; j < pn; j++) { int cur = v[j]; if (cur == 1) { for (int i = 0; i < n; i++) { if (a[i] == mx) { ok[i] = true; } else ok[i] = false; } } else { for (int i = 0; i < n; i++) { if (curmax[j][i] == 0) { int mx = a[i]; int go = (i + cur) % n; while (go != i) { if (a[go] > mx) mx = a[go]; go = (go + cur) % n; } go = (i + cur) % n; curmax[j][i] = mx; while (go != i) { curmax[j][go] = mx; go = (go + cur) % n; } } if (a[i] == curmax[j][i]) { ok[i] = true; } else { ok[i] = false; } } } for (int i = 0; i < n; i++) { if (ok[i]) { if (i == 0) pref[i] = 1; else pref[i] = 1 + pref[i - 1]; } else { pref[i] = 0; } } memset(s, 0, sizeof(s)); for (int i = 0; i < n; i++) { int total = pref[i]; if (total == i + 1) { total += pref[n - 1]; if (total >= n) { total = n; } } s[total]++; } for (int i = n; i >= 1; i--) { s[i] += s[i + 1]; } int p = 1; for (int i = 1; i < n; i++) { if (g[i] == cur) { while (p < i) { p++; } ans += s[p]; } } } cout << ans << endl; return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 2 * 105000; int n, pn; int a[MAXN]; int curmax[200][MAXN]; vector<int> v; bool ok[MAXN]; int mx; long long ans = 0; int s[MAXN]; int g[MAXN]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } vector<int> lens; int pref[MAXN]; void fact(int n) { for (int i = 1; i < n; i++) { if (n % i == 0) { v.push_back(i); } } } set<int> st; int main() { cin >> n; mx = -5; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] > mx) mx = a[i]; st.insert(a[i]); } for (int i = 1; i < n; i++) { g[i] = gcd(i, n); } fact(n); pn = (int)v.size(); for (int j = 0; j < pn; j++) { int cur = v[j]; if (cur == 1) { for (int i = 0; i < n; i++) { if (a[i] == mx) { ok[i] = true; } else ok[i] = false; } } else { for (int i = 0; i < n; i++) { if (curmax[j][i] == 0) { int mx = a[i]; int go = (i + cur) % n; while (go != i) { if (a[go] > mx) mx = a[go]; go = (go + cur) % n; } go = (i + cur) % n; curmax[j][i] = mx; while (go != i) { curmax[j][go] = mx; go = (go + cur) % n; } } if (a[i] == curmax[j][i]) { ok[i] = true; } else { ok[i] = false; } } } for (int i = 0; i < n; i++) { if (ok[i]) { if (i == 0) pref[i] = 1; else pref[i] = 1 + pref[i - 1]; } else { pref[i] = 0; } } memset(s, 0, sizeof(s)); for (int i = 0; i < n; i++) { int total = pref[i]; if (total == i + 1) { total += pref[n - 1]; if (total >= n) { total = n; } } s[total]++; } for (int i = n; i >= 1; i--) { s[i] += s[i + 1]; } int p = 1; for (int i = 1; i < n; i++) { if (g[i] == cur) { while (p < i) { p++; } ans += s[p]; } } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 2E5 + 10; int n; int a[MAXN], b[MAXN], c[MAXN]; int val[MAXN]; long long ans; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } void work(int d) { for (int i = 0; i < d; ++i) val[i] = a[i]; for (int i = d; i < n; ++i) val[i % d] = max(val[i % d], a[i]); for (int i = 1; i <= n; ++i) c[i] = c[i - 1] + (b[i] == d); int start = -1; for (int i = 0; i < n; ++i) if (a[i] != val[i % d]) { start = i; break; } if (start == -1) { ans += c[n] * (long long)n; return; } int cnt = 0; for (int i = 0; i < n; ++i) ans += c[cnt = a[(i + start) % n] == val[(i + start) % d] ? cnt + 1 : 0]; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); b[i + 1] = gcd(i + 1, n); } if (n > 1) work(1); for (int i = 2; i * i <= n; ++i) if (n % i == 0) { work(i); if (i * i < n) work(n / i); } printf("%I64d\n", ans); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 2E5 + 10; int n; int a[MAXN], b[MAXN], c[MAXN]; int val[MAXN]; long long ans; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } void work(int d) { for (int i = 0; i < d; ++i) val[i] = a[i]; for (int i = d; i < n; ++i) val[i % d] = max(val[i % d], a[i]); for (int i = 1; i <= n; ++i) c[i] = c[i - 1] + (b[i] == d); int start = -1; for (int i = 0; i < n; ++i) if (a[i] != val[i % d]) { start = i; break; } if (start == -1) { ans += c[n] * (long long)n; return; } int cnt = 0; for (int i = 0; i < n; ++i) ans += c[cnt = a[(i + start) % n] == val[(i + start) % d] ? cnt + 1 : 0]; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); b[i + 1] = gcd(i + 1, n); } if (n > 1) work(1); for (int i = 2; i * i <= n; ++i) if (n % i == 0) { work(i); if (i * i < n) work(n / i); } printf("%I64d\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a[200010], mx[200010], cnt[200010], GCD[200010]; long long ans; inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } inline void cal(int x) { memset(mx, 0, sizeof(mx)); memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < n; i++) mx[i % x] = max(mx[i % x], a[i]); int pos = -1; for (int i = 0; i < n; i++) if (mx[i % x] != a[i]) { pos = i; break; } if (pos == -1) { for (int i = 1; i < n; i++) if (GCD[i] == x) ans += n; return; } for (int l = pos;;) { if (mx[l % x] != a[l]) l = (l + 1) % n; else { int r = l; while (mx[(r + 1) % n % x] == a[(r + 1) % n]) r = (r + 1) % n; int len = (r - l + 1 + n) % n; if (!len) len = n; for (int i = len; i >= 1; i--) cnt[i] += len - i + 1; l = (r + 1) % n; } if (l == pos) break; } for (int i = 1; i < n; i++) if (GCD[i] == x) ans += cnt[i]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) GCD[i] = gcd(i, n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) { if (n % i == 0) cal(i); } printf("%lld\n", ans); return 0; }
### Prompt In Cpp, your task is to solve the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a[200010], mx[200010], cnt[200010], GCD[200010]; long long ans; inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } inline void cal(int x) { memset(mx, 0, sizeof(mx)); memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < n; i++) mx[i % x] = max(mx[i % x], a[i]); int pos = -1; for (int i = 0; i < n; i++) if (mx[i % x] != a[i]) { pos = i; break; } if (pos == -1) { for (int i = 1; i < n; i++) if (GCD[i] == x) ans += n; return; } for (int l = pos;;) { if (mx[l % x] != a[l]) l = (l + 1) % n; else { int r = l; while (mx[(r + 1) % n % x] == a[(r + 1) % n]) r = (r + 1) % n; int len = (r - l + 1 + n) % n; if (!len) len = n; for (int i = len; i >= 1; i--) cnt[i] += len - i + 1; l = (r + 1) % n; } if (l == pos) break; } for (int i = 1; i < n; i++) if (GCD[i] == x) ans += cnt[i]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) GCD[i] = gcd(i, n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) { if (n % i == 0) cal(i); } printf("%lld\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = (int)2e5 + 5; int n, a[maxn], vis[2 * maxn], mx[maxn]; long long ans; vector<int> slist[maxn]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } void doit(int d) { for (int i = 0; i < d; i++) mx[i] = 0; for (int i = 0; i < n; i++) vis[i] = 0; for (int i = 0; i < n; i++) mx[i % d] = max(mx[i % d], a[i]); for (int i = 0; i < n; i++) if (a[i] == mx[i % d]) vis[i] = 1; for (int i = n; i < 2 * n; i++) vis[i] = vis[i - n]; int lst = -1; for (int i = 2 * n - 1; i >= 0; i--) { if (vis[i] && lst == -1) lst = i; else if (!vis[i]) lst = -1; if (lst == -1 || i >= n) continue; int len = lst - i + 1; int idx = upper_bound(slist[d].begin(), slist[d].end(), len) - slist[d].begin(); ans += idx; } } int main() { ios_base::sync_with_stdio(0); cin.clear(); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) slist[gcd(i, n)].push_back(i); for (int i = 1; i * i <= n; i++) { if (n % i) continue; doit(i); if (n / i != i) doit(n / i); } cout << ans; }
### Prompt Your task is to create a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = (int)2e5 + 5; int n, a[maxn], vis[2 * maxn], mx[maxn]; long long ans; vector<int> slist[maxn]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } void doit(int d) { for (int i = 0; i < d; i++) mx[i] = 0; for (int i = 0; i < n; i++) vis[i] = 0; for (int i = 0; i < n; i++) mx[i % d] = max(mx[i % d], a[i]); for (int i = 0; i < n; i++) if (a[i] == mx[i % d]) vis[i] = 1; for (int i = n; i < 2 * n; i++) vis[i] = vis[i - n]; int lst = -1; for (int i = 2 * n - 1; i >= 0; i--) { if (vis[i] && lst == -1) lst = i; else if (!vis[i]) lst = -1; if (lst == -1 || i >= n) continue; int len = lst - i + 1; int idx = upper_bound(slist[d].begin(), slist[d].end(), len) - slist[d].begin(); ans += idx; } } int main() { ios_base::sync_with_stdio(0); cin.clear(); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) slist[gcd(i, n)].push_back(i); for (int i = 1; i * i <= n; i++) { if (n % i) continue; doit(i); if (n / i != i) doit(n / i); } cout << ans; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long powmod(long long a, long long b) { long long res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } int n, divN; vector<int> a; set<int> DIV; map<int, int> p; bool possible[200005][1000]; int big[200006], bigCnt[200006]; map<int, int> lens[100000]; void addDiv(map<int, int>::iterator it, int curr) { if (it == p.end()) return; int t = 1; for (int i = 0; i < (int)it->second + 1; i++) { DIV.insert(curr * t); map<int, int>::iterator it1 = it; it1++; addDiv(it1, curr * t); t *= it->first; } } void setDiv(int n) { while (n % 2 == 0) p[2]++, n /= 2; int eIdx = int(sqrt(double(n))) + 10, idx = 3; while (idx < eIdx && n > 1) { while (n % idx == 0) p[idx]++, n /= idx; idx += 2; } if (n > 1) p[n]++; addDiv(p.begin(), 1); } int getGCD(int a, int b) { if (b == 0) return a; if (a < b) return getGCD(b, a); if (a % b == 0) return b; return getGCD(b, a % b); } int main() { memset(possible, 0, sizeof(possible)); cin >> n; a.resize(n); setDiv(n); divN = DIV.size(); for (int i = 0; i < (int)n; i++) cin >> a[i]; int cnt = 0; for (set<int>::iterator it = DIV.begin(); it != DIV.end(); it++, cnt++) { int t = getGCD(n, *it); if (t == n) break; vector<int> maxVal(t, 0); vector<bool> isMax(n, 0); for (int i = 0; i < (int)n; i++) { int idx = i % t; if (maxVal[idx] < a[i]) maxVal[idx] = a[i]; } for (int i = 0; i < (int)n; i++) { int idx = i % t; if (maxVal[idx] == a[i]) isMax[i] = 1; } int fIdx = -1; for (int i = 0; i < (int)n; i++) { if (!isMax[i]) { fIdx = i; break; } } if (fIdx == -1) { lens[cnt][n]++; ; } else { int cnt1 = 0; for (int i = fIdx; i < (int)fIdx + n; i++) { int idx = i % n; if (isMax[idx]) cnt1++; else { if (cnt1) lens[cnt][cnt1]++; cnt1 = 0; } } if (cnt1) { lens[cnt][cnt1]++; } } } long long ans = 0; for (int k = 1; k < n; k++) { int t = getGCD(n, k); set<int>::iterator it = DIV.lower_bound(t); int idx = distance(DIV.begin(), it); for (map<int, int>::iterator it = lens[idx].begin(); it != lens[idx].end(); it++) { if (it->first == n) { ans += n; break; } if (it->first >= k) { ans += (it->first - k + 1) * it->second; } } } cout << ans << endl; return 0; }
### Prompt Generate a CPP solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long powmod(long long a, long long b) { long long res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } int n, divN; vector<int> a; set<int> DIV; map<int, int> p; bool possible[200005][1000]; int big[200006], bigCnt[200006]; map<int, int> lens[100000]; void addDiv(map<int, int>::iterator it, int curr) { if (it == p.end()) return; int t = 1; for (int i = 0; i < (int)it->second + 1; i++) { DIV.insert(curr * t); map<int, int>::iterator it1 = it; it1++; addDiv(it1, curr * t); t *= it->first; } } void setDiv(int n) { while (n % 2 == 0) p[2]++, n /= 2; int eIdx = int(sqrt(double(n))) + 10, idx = 3; while (idx < eIdx && n > 1) { while (n % idx == 0) p[idx]++, n /= idx; idx += 2; } if (n > 1) p[n]++; addDiv(p.begin(), 1); } int getGCD(int a, int b) { if (b == 0) return a; if (a < b) return getGCD(b, a); if (a % b == 0) return b; return getGCD(b, a % b); } int main() { memset(possible, 0, sizeof(possible)); cin >> n; a.resize(n); setDiv(n); divN = DIV.size(); for (int i = 0; i < (int)n; i++) cin >> a[i]; int cnt = 0; for (set<int>::iterator it = DIV.begin(); it != DIV.end(); it++, cnt++) { int t = getGCD(n, *it); if (t == n) break; vector<int> maxVal(t, 0); vector<bool> isMax(n, 0); for (int i = 0; i < (int)n; i++) { int idx = i % t; if (maxVal[idx] < a[i]) maxVal[idx] = a[i]; } for (int i = 0; i < (int)n; i++) { int idx = i % t; if (maxVal[idx] == a[i]) isMax[i] = 1; } int fIdx = -1; for (int i = 0; i < (int)n; i++) { if (!isMax[i]) { fIdx = i; break; } } if (fIdx == -1) { lens[cnt][n]++; ; } else { int cnt1 = 0; for (int i = fIdx; i < (int)fIdx + n; i++) { int idx = i % n; if (isMax[idx]) cnt1++; else { if (cnt1) lens[cnt][cnt1]++; cnt1 = 0; } } if (cnt1) { lens[cnt][cnt1]++; } } } long long ans = 0; for (int k = 1; k < n; k++) { int t = getGCD(n, k); set<int>::iterator it = DIV.lower_bound(t); int idx = distance(DIV.begin(), it); for (map<int, int>::iterator it = lens[idx].begin(); it != lens[idx].end(); it++) { if (it->first == n) { ans += n; break; } if (it->first >= k) { ans += (it->first - k + 1) * it->second; } } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 1; const double pi = acos(-1.0); const int N = 300010; int n, a[N], b[N], x[N], f[N]; bool isMax[N]; int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) b[i] = gcd(i, n); long long ans = 0; for (int k = 1; k < n; k++) if (n % k == 0) { f[0] = 0; if (b[1] == k) f[1] = 1; else f[1] = 0; for (int j = 2; j <= n; j++) if (b[j] == k) f[j] = f[j - 1] + 1; else f[j] = f[j - 1]; for (int j = 0; j < k; j++) { int mx = 0; for (int i = j; i < n; i += k) { mx = max(mx, a[i]); } for (int i = j; i < n; i += k) { if (a[i] == mx) isMax[i] = true; else isMax[i] = false; } } if (!isMax[n - 1]) x[n - 1] = 0; else { x[n - 1] = 1; for (int i = 0; i < n - 2; i++) if (isMax[i]) x[n - 1]++; else break; } for (int i = n - 2; i >= 0; i--) if (isMax[i]) x[i] = min(n - 1, x[i + 1] + 1); else x[i] = 0; for (int i = 0; i < n; i++) ans += f[x[i]]; } cout << ans; return 0; }
### Prompt Create a solution in cpp for the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 1; const double pi = acos(-1.0); const int N = 300010; int n, a[N], b[N], x[N], f[N]; bool isMax[N]; int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) b[i] = gcd(i, n); long long ans = 0; for (int k = 1; k < n; k++) if (n % k == 0) { f[0] = 0; if (b[1] == k) f[1] = 1; else f[1] = 0; for (int j = 2; j <= n; j++) if (b[j] == k) f[j] = f[j - 1] + 1; else f[j] = f[j - 1]; for (int j = 0; j < k; j++) { int mx = 0; for (int i = j; i < n; i += k) { mx = max(mx, a[i]); } for (int i = j; i < n; i += k) { if (a[i] == mx) isMax[i] = true; else isMax[i] = false; } } if (!isMax[n - 1]) x[n - 1] = 0; else { x[n - 1] = 1; for (int i = 0; i < n - 2; i++) if (isMax[i]) x[n - 1]++; else break; } for (int i = n - 2; i >= 0; i--) if (isMax[i]) x[i] = min(n - 1, x[i + 1] + 1); else x[i] = 0; for (int i = 0; i < n; i++) ans += f[x[i]]; } cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5; int N; int A[MAXN]; int gcd[MAXN]; int is_max[MAXN]; int cnt[MAXN]; int main() { ios_base::sync_with_stdio(0); cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; A[i + N] = A[i]; } long long res = 0; for (int g = 1; g < N; g++) { if (N % g) continue; for (int j = g; j <= N; j += g) { gcd[j - 1] = g; } } for (int g = 1; g < N; g++) { if (N % g) continue; for (int i = 0; i < g; i++) { int m = 0; for (int j = i; j < N; j += g) { m = max(m, A[j]); } for (int j = i; j < N * 2; j += g) { is_max[j] = (A[j] == m); } } for (int j = 1; j < N * 2; j++) { if (is_max[j]) is_max[j] += is_max[j - 1]; } cnt[0] = 0; for (int i = 1; i < N; i++) { cnt[i] = cnt[i - 1] + (gcd[i - 1] == g); } for (int j = N; j < N * 2; j++) { res += cnt[min(is_max[j], N - 1)]; } } cout << res << '\n'; return 0; }
### Prompt Create a solution in CPP for the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 5e5; int N; int A[MAXN]; int gcd[MAXN]; int is_max[MAXN]; int cnt[MAXN]; int main() { ios_base::sync_with_stdio(0); cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; A[i + N] = A[i]; } long long res = 0; for (int g = 1; g < N; g++) { if (N % g) continue; for (int j = g; j <= N; j += g) { gcd[j - 1] = g; } } for (int g = 1; g < N; g++) { if (N % g) continue; for (int i = 0; i < g; i++) { int m = 0; for (int j = i; j < N; j += g) { m = max(m, A[j]); } for (int j = i; j < N * 2; j += g) { is_max[j] = (A[j] == m); } } for (int j = 1; j < N * 2; j++) { if (is_max[j]) is_max[j] += is_max[j - 1]; } cnt[0] = 0; for (int i = 1; i < N; i++) { cnt[i] = cnt[i - 1] + (gcd[i - 1] == g); } for (int j = N; j < N * 2; j++) { res += cnt[min(is_max[j], N - 1)]; } } cout << res << '\n'; return 0; } ```
#include <bits/stdc++.h> int a[423456]; int first[423456]; int next[423456]; int value[423456]; int sz = 0; int b[423456]; int is_max[423456]; int make_list(int start) { int res = 0; while (start != -1) { b[res] = value[start]; start = next[start]; res++; } return res; } int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); } void add(int x, int y) { int tmp = first[x]; first[x] = sz; value[sz] = y; next[sz] = tmp; sz++; } int main() { int n, i, go, value; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i <= n; i++) { first[i] = -1; } for (go = 1; go < n; go++) { int f = gcd(go, n); add(f, go); } long long result = 0; for (value = 1; value < n; value++) { int cnt = make_list(first[value]); if (cnt == 0) { continue; } int j; for (i = 0, j = cnt - 1; i < j; i++, j--) { int tmp = b[i]; b[i] = b[j]; b[j] = tmp; } int start; for (start = 0; start < value; start++) { int ma = 0; int cur; for (cur = start; cur < n; cur += value) { if (a[cur] > ma) ma = a[cur]; } for (cur = start; cur < n; cur += value) { is_max[cur + n] = is_max[cur] = (a[cur] == ma); } } int now_len = 0; for (i = n; i < 2 * n; i++) { if (is_max[i]) { now_len++; } else { break; } } int cur_it = 0; for (start = n - 1; start >= 0; start--) { if (is_max[start]) { now_len++; } else { now_len = 0; } while (cur_it < cnt && b[cur_it] <= now_len) cur_it++; while (cur_it > 0 && b[cur_it - 1] > now_len) cur_it--; result += cur_it; } } printf("%I64d\n", result); return 0; }
### Prompt In Cpp, your task is to solve the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> int a[423456]; int first[423456]; int next[423456]; int value[423456]; int sz = 0; int b[423456]; int is_max[423456]; int make_list(int start) { int res = 0; while (start != -1) { b[res] = value[start]; start = next[start]; res++; } return res; } int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); } void add(int x, int y) { int tmp = first[x]; first[x] = sz; value[sz] = y; next[sz] = tmp; sz++; } int main() { int n, i, go, value; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i <= n; i++) { first[i] = -1; } for (go = 1; go < n; go++) { int f = gcd(go, n); add(f, go); } long long result = 0; for (value = 1; value < n; value++) { int cnt = make_list(first[value]); if (cnt == 0) { continue; } int j; for (i = 0, j = cnt - 1; i < j; i++, j--) { int tmp = b[i]; b[i] = b[j]; b[j] = tmp; } int start; for (start = 0; start < value; start++) { int ma = 0; int cur; for (cur = start; cur < n; cur += value) { if (a[cur] > ma) ma = a[cur]; } for (cur = start; cur < n; cur += value) { is_max[cur + n] = is_max[cur] = (a[cur] == ma); } } int now_len = 0; for (i = n; i < 2 * n; i++) { if (is_max[i]) { now_len++; } else { break; } } int cur_it = 0; for (start = n - 1; start >= 0; start--) { if (is_max[start]) { now_len++; } else { now_len = 0; } while (cur_it < cnt && b[cur_it] <= now_len) cur_it++; while (cur_it > 0 && b[cur_it - 1] > now_len) cur_it--; result += cur_it; } } printf("%I64d\n", result); return 0; } ```
#include <bits/stdc++.h> const int N = 200000; inline int gcd(int a, int b) { while (b) { int c = a % b; a = b; b = c; } return a; } int n; int a[N], b[N], g[N]; long long ans; inline int regular(int x) { if (x >= n) return x - n; return x; } long long cal(int t) { static bool c[N]; for (int i = 0; i < n; i++) c[i] = false; for (int i = 0; i < t; i++) { int maxv = 0; for (int j = i; j < n; j += t) if (maxv < a[j]) maxv = a[j]; for (int j = i; j < n; j += t) if (maxv == a[j]) c[j] = true; else c[j] = false; } static int d[N]; d[0] = 0; for (int i = 1; i < n; i++) { if (g[i] == t) d[i] = d[i - 1] + 1; else d[i] = d[i - 1]; } long long ans = 0; for (int i = 0, j = 0; i < n; i++, j--) { if (j < 0) j = 0; while (j < n - 1 && c[regular(i + j)] == true) j++; ans += d[j]; } return ans; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) b[gcd(i, n)]++; for (int i = 1; i < n; i++) g[i] = gcd(i, n); for (int i = 1; i < n; i++) if (b[i] != 0) { ans += cal(i); } printf("%I64d\n", ans); return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> const int N = 200000; inline int gcd(int a, int b) { while (b) { int c = a % b; a = b; b = c; } return a; } int n; int a[N], b[N], g[N]; long long ans; inline int regular(int x) { if (x >= n) return x - n; return x; } long long cal(int t) { static bool c[N]; for (int i = 0; i < n; i++) c[i] = false; for (int i = 0; i < t; i++) { int maxv = 0; for (int j = i; j < n; j += t) if (maxv < a[j]) maxv = a[j]; for (int j = i; j < n; j += t) if (maxv == a[j]) c[j] = true; else c[j] = false; } static int d[N]; d[0] = 0; for (int i = 1; i < n; i++) { if (g[i] == t) d[i] = d[i - 1] + 1; else d[i] = d[i - 1]; } long long ans = 0; for (int i = 0, j = 0; i < n; i++, j--) { if (j < 0) j = 0; while (j < n - 1 && c[regular(i + j)] == true) j++; ans += d[j]; } return ans; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) b[gcd(i, n)]++; for (int i = 1; i < n; i++) g[i] = gcd(i, n); for (int i = 1; i < n; i++) if (b[i] != 0) { ans += cal(i); } printf("%I64d\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 1000 + 10; int a[MAXN], n, c[MAXN], gg[MAXN]; bool u[MAXN]; int inc(int v) { return (v + 1 == n) ? 0 : (v + 1); } int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) gg[i] = GCD(i, n); long long res = 0; for (int g = 1; g < n; g++) { if (n % g != 0) continue; for (int i = 0; i < n; i++) u[i] = false, c[i] = 0; for (int i = 0; i < g; i++) { int mx = -1; for (int j = i; j < n; j += g) mx = max(mx, a[j]); for (int j = i; j < n; j += g) if (a[j] == mx) u[j] = true; } bool any = false; for (int l = 0; l < n;) { int r = inc(l); if (u[l]) { l++; continue; } any = true; int len = 0; while (u[r]) len++, r = inc(r); for (int i = 1; i <= len; i++) c[i] += len - i + 1; if (r <= l) break; l = r; } if (!any) for (int i = 1; i <= n; i++) c[i] += n; for (int i = 1; i <= n; i++) if (gg[i] == g) res += c[i]; } printf("%lld\n", res); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 1000 + 10; int a[MAXN], n, c[MAXN], gg[MAXN]; bool u[MAXN]; int inc(int v) { return (v + 1 == n) ? 0 : (v + 1); } int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) gg[i] = GCD(i, n); long long res = 0; for (int g = 1; g < n; g++) { if (n % g != 0) continue; for (int i = 0; i < n; i++) u[i] = false, c[i] = 0; for (int i = 0; i < g; i++) { int mx = -1; for (int j = i; j < n; j += g) mx = max(mx, a[j]); for (int j = i; j < n; j += g) if (a[j] == mx) u[j] = true; } bool any = false; for (int l = 0; l < n;) { int r = inc(l); if (u[l]) { l++; continue; } any = true; int len = 0; while (u[r]) len++, r = inc(r); for (int i = 1; i <= len; i++) c[i] += len - i + 1; if (r <= l) break; l = r; } if (!any) for (int i = 1; i <= n; i++) c[i] += n; for (int i = 1; i <= n; i++) if (gg[i] == g) res += c[i]; } printf("%lld\n", res); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int M = 400005; int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } int n; bool mark[M]; int c[M]; int A[M], dp[M], cnt[M]; int Gcd[M]; int main() { cin >> n; for (int i = 0; i <= n; ++i) Gcd[i] = gcd(i, n); for (int i = 0; i < n; ++i) cin >> A[i]; for (int i = n; i < n + n; ++i) A[i] = A[i - n]; long long ans = 0; for (int d = 1; d <= n; ++d) { if (n % d) continue; memset(mark, 0, sizeof(mark)); for (int i = 0; i < d; ++i) { int mx = 0; for (int j = i; j < n + n; j += d) if (A[j] > mx) mx = A[j]; for (int j = i; j < n + n; j += d) if (A[j] == mx) mark[j] = true; } dp[0] = mark[0]; for (int i = 1; i < n + n; ++i) { if (mark[i]) dp[i] = dp[i - 1] + 1; else dp[i] = 0; dp[i] = min(dp[i], n - 1); } cnt[0] = 0; for (int i = 1; i < n / d; ++i) cnt[i] = cnt[i - 1] + (Gcd[i * d] == d); for (int i = n; i < n + n; ++i) ans += cnt[dp[i] / d]; } cout << ans << endl; }
### Prompt Generate a cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int M = 400005; int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } int n; bool mark[M]; int c[M]; int A[M], dp[M], cnt[M]; int Gcd[M]; int main() { cin >> n; for (int i = 0; i <= n; ++i) Gcd[i] = gcd(i, n); for (int i = 0; i < n; ++i) cin >> A[i]; for (int i = n; i < n + n; ++i) A[i] = A[i - n]; long long ans = 0; for (int d = 1; d <= n; ++d) { if (n % d) continue; memset(mark, 0, sizeof(mark)); for (int i = 0; i < d; ++i) { int mx = 0; for (int j = i; j < n + n; j += d) if (A[j] > mx) mx = A[j]; for (int j = i; j < n + n; j += d) if (A[j] == mx) mark[j] = true; } dp[0] = mark[0]; for (int i = 1; i < n + n; ++i) { if (mark[i]) dp[i] = dp[i - 1] + 1; else dp[i] = 0; dp[i] = min(dp[i], n - 1); } cnt[0] = 0; for (int i = 1; i < n / d; ++i) cnt[i] = cnt[i - 1] + (Gcd[i * d] == d); for (int i = n; i < n + n; ++i) ans += cnt[dp[i] / d]; } cout << ans << endl; } ```
#include <bits/stdc++.h> using namespace std; const int LIM = 2e+5 + 100; int get_gcd(int a, int b) { return b ? get_gcd(b, a % b) : a; } int a[LIM], gcd[LIM]; vector<bool> good(LIM); vector<int> len(LIM), _max(LIM); int main() { int n; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) gcd[i] = get_gcd(i, n); long long ans = 0; for (int i = 1; i < n; ++i) { if (n % i) continue; good.assign(n, false); len.assign(n, 0); _max.assign(n, 0); for (int j = 0; j < n; ++j) _max[j % i] = max(_max[j % i], a[j]); for (int j = 0; j < n; ++j) good[j] = _max[j % i] == a[j]; int l = 0; bool all = true; while (l < n) { if (good[l]) { ++l; continue; } all = false; int r = (l + 1) % n; int cur_len = 0; while (good[r]) { ++r; ++cur_len; if (r >= n) r -= n; } for (int k = 1; k <= cur_len; ++k) len[k] += cur_len + 1 - k; if (r <= l) break; l = r; } if (all) for (int j = 1; j <= n; ++j) len[j] = n; for (int j = 1; j <= n; ++j) ans += (gcd[j] == i ? len[j] : 0); } cout << ans; return 0; }
### Prompt Develop a solution in CPP to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int LIM = 2e+5 + 100; int get_gcd(int a, int b) { return b ? get_gcd(b, a % b) : a; } int a[LIM], gcd[LIM]; vector<bool> good(LIM); vector<int> len(LIM), _max(LIM); int main() { int n; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) gcd[i] = get_gcd(i, n); long long ans = 0; for (int i = 1; i < n; ++i) { if (n % i) continue; good.assign(n, false); len.assign(n, 0); _max.assign(n, 0); for (int j = 0; j < n; ++j) _max[j % i] = max(_max[j % i], a[j]); for (int j = 0; j < n; ++j) good[j] = _max[j % i] == a[j]; int l = 0; bool all = true; while (l < n) { if (good[l]) { ++l; continue; } all = false; int r = (l + 1) % n; int cur_len = 0; while (good[r]) { ++r; ++cur_len; if (r >= n) r -= n; } for (int k = 1; k <= cur_len; ++k) len[k] += cur_len + 1 - k; if (r <= l) break; l = r; } if (all) for (int j = 1; j <= n; ++j) len[j] = n; for (int j = 1; j <= n; ++j) ans += (gcd[j] == i ? len[j] : 0); } cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 400010; int n; long long ans; int a[N], v[N], c[N]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n / 2; i++) if (n % i == 0) { for (int j = 1; j <= n; j++) v[j] = 0, c[j] = 0; for (int j = 1; j <= i; j++) { int mx = 0; for (int k = j; k <= n; k += i) mx = max(mx, a[k]); for (int k = j; k <= n; k += i) if (a[k] == mx) v[k] = 1; } int flag = 1, st = 0; for (int j = 1; j <= n; j++) { v[j + n] = v[j]; if (!v[j]) flag = 0; } if (!flag) for (int j = 1; j <= n; j++) if (v[j] == 0) { st = j + 1; break; } for (int j = st, k = 0; j <= st + n - 1; j++) if (v[j]) k++; else c[k]++, k = 0; long long k1 = 0, k2 = 0; if (flag) k2 = n; for (int j = n; j >= i; j--) { k1 += c[j]; k2 += k1; if (k2 && j % i == 0) if (gcd(j, n) == i) ans += k2; } } cout << ans << endl; return 0; }
### Prompt Develop a solution in CPP to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 400010; int n; long long ans; int a[N], v[N], c[N]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n / 2; i++) if (n % i == 0) { for (int j = 1; j <= n; j++) v[j] = 0, c[j] = 0; for (int j = 1; j <= i; j++) { int mx = 0; for (int k = j; k <= n; k += i) mx = max(mx, a[k]); for (int k = j; k <= n; k += i) if (a[k] == mx) v[k] = 1; } int flag = 1, st = 0; for (int j = 1; j <= n; j++) { v[j + n] = v[j]; if (!v[j]) flag = 0; } if (!flag) for (int j = 1; j <= n; j++) if (v[j] == 0) { st = j + 1; break; } for (int j = st, k = 0; j <= st + n - 1; j++) if (v[j]) k++; else c[k]++, k = 0; long long k1 = 0, k2 = 0; if (flag) k2 = n; for (int j = n; j >= i; j--) { k1 += c[j]; k2 += k1; if (k2 && j % i == 0) if (gcd(j, n) == i) ans += k2; } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 400010; int gcds[N], a[N], b[N]; int c[N], mx[N]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i - 1]); gcds[i] = gcd(i, n); } long long ans = 0; for (int i = 1; i < n; i++) { if (n % i) continue; for (int j = 1; j <= n; j++) c[j] = c[j - 1] + (i == gcds[j]); for (int j = 0; j < i; j++) mx[j] = a[j]; for (int j = i; j < n; j++) mx[j % i] = max(mx[j % i], a[j]); for (int j = 0; j < n; j++) b[j] = b[j + n] = mx[j % i] == a[j]; for (int j = 2 * n - 2; j >= 0; j--) if (b[j]) b[j] += b[j + 1]; for (int j = 0; j < n; j++) ans += c[min(n - 1, b[j])]; } cout << ans << endl; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 400010; int gcds[N], a[N], b[N]; int c[N], mx[N]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i - 1]); gcds[i] = gcd(i, n); } long long ans = 0; for (int i = 1; i < n; i++) { if (n % i) continue; for (int j = 1; j <= n; j++) c[j] = c[j - 1] + (i == gcds[j]); for (int j = 0; j < i; j++) mx[j] = a[j]; for (int j = i; j < n; j++) mx[j % i] = max(mx[j % i], a[j]); for (int j = 0; j < n; j++) b[j] = b[j + n] = mx[j % i] == a[j]; for (int j = 2 * n - 2; j >= 0; j--) if (b[j]) b[j] += b[j + 1]; for (int j = 0; j < n; j++) ans += c[min(n - 1, b[j])]; } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { while (y > 0) { int t = y; y = x % y; x = t; } return x; } int main() { int n; scanf("%d", &n); static int a[(int)2e5 + 5]; for (int i = 0; i < n; i++) scanf("%d", &a[i]); static int g[(int)2e5 + 5]; for (int i = 1; i <= n; i++) g[i] = gcd(n, i); long long answer = 0; for (int d = 1; d <= n; d++) if (n % d == 0) { static bool ok[(int)2e5 + 5]; memset(ok, ~0, sizeof ok); for (int r = 0; r < d; r++) { int mx = 0; for (int i = r; i < n; i += d) mx = max(mx, a[i]); for (int i = r; i < n; i += d) ok[i] = a[i] == mx; } if (count(ok, ok + n, true) == n) { for (int v = d; v < n; v += d) if (g[v] == d) answer += n; } else for (int i = 0; i < n; i++) if (!ok[i] && ok[(i + 1) % n]) { int le = 0; for (int at = (i + 1) % n; ok[at]; at = (at + 1) % n) le++; for (int v = d; v <= le; v += d) if (g[v] == d) answer += le - v + 1; } } cout << answer << endl; }
### Prompt Please create a solution in CPP to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { while (y > 0) { int t = y; y = x % y; x = t; } return x; } int main() { int n; scanf("%d", &n); static int a[(int)2e5 + 5]; for (int i = 0; i < n; i++) scanf("%d", &a[i]); static int g[(int)2e5 + 5]; for (int i = 1; i <= n; i++) g[i] = gcd(n, i); long long answer = 0; for (int d = 1; d <= n; d++) if (n % d == 0) { static bool ok[(int)2e5 + 5]; memset(ok, ~0, sizeof ok); for (int r = 0; r < d; r++) { int mx = 0; for (int i = r; i < n; i += d) mx = max(mx, a[i]); for (int i = r; i < n; i += d) ok[i] = a[i] == mx; } if (count(ok, ok + n, true) == n) { for (int v = d; v < n; v += d) if (g[v] == d) answer += n; } else for (int i = 0; i < n; i++) if (!ok[i] && ok[(i + 1) % n]) { int le = 0; for (int at = (i + 1) % n; ok[at]; at = (at + 1) % n) le++; for (int v = d; v <= le; v += d) if (g[v] == d) answer += le - v + 1; } } cout << answer << endl; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1000 * 1000 + 5; int a[N], n, c[N], gg[N]; bool u[N]; inline int inc(int v) { return (v + 1 == n) ? 0 : (v + 1); } inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { assert(scanf("%d", &n) == 1); for (int i = 0; i < n; ++i) assert(scanf("%d", &a[i]) == 1); for (int i = 1; i <= n; ++i) gg[i] = gcd(i, n); long long res = 0; for (int g = 1; g < n; ++g) { if (n % g != 0) continue; for (int i = 0; i < n; ++i) u[i] = false, c[i] = 0; for (int i = 0; i < g; ++i) { int mx = -1; for (int j = i; j < n; j += g) mx = max(mx, a[j]); for (int j = i; j < n; j += g) if (a[j] == mx) u[j] = true; } bool any = false; for (int l = 0; l < n;) { int r = inc(l); if (u[l]) { l++; continue; } any = true; int len = 0; while (u[r]) len++, r = inc(r); for (int i = 1; i <= len; ++i) c[i] += len - i + 1; if (r <= l) break; l = r; } if (!any) for (int i = 1; i <= n; ++i) c[i] += n; for (int i = 1; i <= n; ++i) if (gg[i] == g) { res += c[i]; } } cout << res << endl; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1000 * 1000 + 5; int a[N], n, c[N], gg[N]; bool u[N]; inline int inc(int v) { return (v + 1 == n) ? 0 : (v + 1); } inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { assert(scanf("%d", &n) == 1); for (int i = 0; i < n; ++i) assert(scanf("%d", &a[i]) == 1); for (int i = 1; i <= n; ++i) gg[i] = gcd(i, n); long long res = 0; for (int g = 1; g < n; ++g) { if (n % g != 0) continue; for (int i = 0; i < n; ++i) u[i] = false, c[i] = 0; for (int i = 0; i < g; ++i) { int mx = -1; for (int j = i; j < n; j += g) mx = max(mx, a[j]); for (int j = i; j < n; j += g) if (a[j] == mx) u[j] = true; } bool any = false; for (int l = 0; l < n;) { int r = inc(l); if (u[l]) { l++; continue; } any = true; int len = 0; while (u[r]) len++, r = inc(r); for (int i = 1; i <= len; ++i) c[i] += len - i + 1; if (r <= l) break; l = r; } if (!any) for (int i = 1; i <= n; ++i) c[i] += n; for (int i = 1; i <= n; ++i) if (gg[i] == g) { res += c[i]; } } cout << res << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 211111; int a[N]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int legal[N]; long long sum[N]; bool vis[N * 2]; long long work(int n, int block) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < block; i++) { vector<int> vec; int tmp_max = 0; for (int j = 0; j < n / block; j++) if (tmp_max < a[i + j * block]) tmp_max = a[i + j * block]; for (int j = 0; j < n / block; j++) if (tmp_max == a[i + j * block]) vis[i + j * block] = true; } for (int i = 0; i < n; i++) vis[i + n] = vis[i]; int start = 0; for (int i = 1; i < n; i++) if (vis[i] && !vis[i - 1]) start = i; memset(legal, 0, sizeof(legal)); for (int i = 1; i < N; i++) { legal[i] = legal[i - 1]; if (i % block == 0 && gcd(n, i) == block) legal[i]++; } int tmp = 0; long long ans = 0; for (int i = start; i < start + n; i++) if (vis[i]) { tmp++; ans += legal[tmp]; } else tmp = 0; if (tmp == n) { ans = (long long)n * legal[n]; } return ans; } int main() { ios ::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0; for (int i = 1; i < n; i++) if (n % i == 0) ans += work(n, i); cout << ans << '\n'; return 0; }
### Prompt Develop a solution in CPP to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 211111; int a[N]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int legal[N]; long long sum[N]; bool vis[N * 2]; long long work(int n, int block) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < block; i++) { vector<int> vec; int tmp_max = 0; for (int j = 0; j < n / block; j++) if (tmp_max < a[i + j * block]) tmp_max = a[i + j * block]; for (int j = 0; j < n / block; j++) if (tmp_max == a[i + j * block]) vis[i + j * block] = true; } for (int i = 0; i < n; i++) vis[i + n] = vis[i]; int start = 0; for (int i = 1; i < n; i++) if (vis[i] && !vis[i - 1]) start = i; memset(legal, 0, sizeof(legal)); for (int i = 1; i < N; i++) { legal[i] = legal[i - 1]; if (i % block == 0 && gcd(n, i) == block) legal[i]++; } int tmp = 0; long long ans = 0; for (int i = start; i < start + n; i++) if (vis[i]) { tmp++; ans += legal[tmp]; } else tmp = 0; if (tmp == n) { ans = (long long)n * legal[n]; } return ans; } int main() { ios ::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0; for (int i = 1; i < n; i++) if (n % i == 0) ans += work(n, i); cout << ans << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 500000 + 10; const int oo = 1000000000 + 10; int n, a[maxn], maxv[maxn], rght[maxn]; vector<int> posslen[maxn]; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = n; i < 2 * n; i++) a[i] = a[i - n]; for (int i = 1; i <= n; i++) posslen[gcd(i, n)].push_back(i); long long ans = 0; for (int prd = 1; prd < n; prd++) if (!posslen[prd].empty()) { for (int i = 0; i < n; i++) maxv[i] = -oo; for (int i = 0; i < n; i++) maxv[i % prd] = max(maxv[i % prd], a[i]); rght[2 * n] = 0; for (int i = 2 * n - 1; i >= 0; i--) { if (a[i] == maxv[i % prd]) { rght[i] = rght[i + 1] + 1; } else rght[i] = 0; } for (int i = 0; i < n; i++) { int cur = upper_bound(posslen[prd].begin(), posslen[prd].end(), min(rght[i], n)) - posslen[prd].begin(); ans += cur; } } cout << ans << endl; return 0; }
### Prompt Generate a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 500000 + 10; const int oo = 1000000000 + 10; int n, a[maxn], maxv[maxn], rght[maxn]; vector<int> posslen[maxn]; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = n; i < 2 * n; i++) a[i] = a[i - n]; for (int i = 1; i <= n; i++) posslen[gcd(i, n)].push_back(i); long long ans = 0; for (int prd = 1; prd < n; prd++) if (!posslen[prd].empty()) { for (int i = 0; i < n; i++) maxv[i] = -oo; for (int i = 0; i < n; i++) maxv[i % prd] = max(maxv[i % prd], a[i]); rght[2 * n] = 0; for (int i = 2 * n - 1; i >= 0; i--) { if (a[i] == maxv[i % prd]) { rght[i] = rght[i + 1] + 1; } else rght[i] = 0; } for (int i = 0; i < n; i++) { int cur = upper_bound(posslen[prd].begin(), posslen[prd].end(), min(rght[i], n)) - posslen[prd].begin(); ans += cur; } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 200010; int num[N], g[N], t[N], mx[N], len[N * 2]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int n; scanf("%d", &n); long long res = 0; for (int i = 0; i < n; i++) scanf("%d", &num[i]); for (int i = 1; i < n; i++) g[i] = gcd(i, n); for (int i = 1; i < n; i++) if (n % i == 0) { for (int j = 1; j < n; j++) t[j] = t[j - 1] + (g[j] == i); for (int j = 0; j < i; j++) mx[j] = 0; for (int j = 0; j < n; j++) mx[j % i] = max(mx[j % i], num[j]); for (int j = 2 * n - 1; j >= 0; j--) { if (num[j % n] == mx[j % i]) len[j] = len[j + 1] + 1; else len[j] = 0; } for (int j = 0; j < n; j++) res += t[min(n - 1, len[j])]; } printf("%lld\n", res); }
### Prompt Please create a solution in CPP to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 200010; int num[N], g[N], t[N], mx[N], len[N * 2]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int n; scanf("%d", &n); long long res = 0; for (int i = 0; i < n; i++) scanf("%d", &num[i]); for (int i = 1; i < n; i++) g[i] = gcd(i, n); for (int i = 1; i < n; i++) if (n % i == 0) { for (int j = 1; j < n; j++) t[j] = t[j - 1] + (g[j] == i); for (int j = 0; j < i; j++) mx[j] = 0; for (int j = 0; j < n; j++) mx[j % i] = max(mx[j % i], num[j]); for (int j = 2 * n - 1; j >= 0; j--) { if (num[j % n] == mx[j % i]) len[j] = len[j + 1] + 1; else len[j] = 0; } for (int j = 0; j < n; j++) res += t[min(n - 1, len[j])]; } printf("%lld\n", res); } ```
#include <bits/stdc++.h> using namespace std; const int N = 4E5 + 5; int n, a[N], k, s, t, f[N]; long long ans; vector<int> G[N]; int gcd(int a, int b) { if (!b) return a; else return gcd(b, a % b); } int main() { cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i + n] = a[i]; for (int i = 1; i < n; i++) G[gcd(i, n)].push_back(i); for (int i = 1; i < n; i++) if ((s = G[i].size()) > 0) { memset(f, 0, sizeof(f)); for (int j = 1; j <= 2 * n; j++) f[j % i] = max(f[j % i], a[j]); k = t = 0; for (int j = 1; j <= 2 * n; j++) if (a[j] >= f[j % i]) { t++; if (k < s && G[i][k] <= t) k++; if (j > n) ans += k; } else t = k = 0; } cout << ans; }
### Prompt Your task is to create a cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 4E5 + 5; int n, a[N], k, s, t, f[N]; long long ans; vector<int> G[N]; int gcd(int a, int b) { if (!b) return a; else return gcd(b, a % b); } int main() { cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i + n] = a[i]; for (int i = 1; i < n; i++) G[gcd(i, n)].push_back(i); for (int i = 1; i < n; i++) if ((s = G[i].size()) > 0) { memset(f, 0, sizeof(f)); for (int j = 1; j <= 2 * n; j++) f[j % i] = max(f[j % i], a[j]); k = t = 0; for (int j = 1; j <= 2 * n; j++) if (a[j] >= f[j % i]) { t++; if (k < s && G[i][k] <= t) k++; if (j > n) ans += k; } else t = k = 0; } cout << ans; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> void print(vector<T> v) { for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } long long gcd(long long u, long long v) { while (v != 0) { long long r = u % v; u = v; v = r; } return u; } int main() { int N; cin >> N; vector<int> v; int i; for (i = 0; i < N; i++) { int a; cin >> a; v.push_back(a); } vector<int> gcdtable(N, 0); for (i = 1; i < N; i++) gcdtable[i] = gcd(N, i); long long res = 0; int d; for (d = 1; d < N; d++) { if (N % d != 0) continue; vector<int> g; int x, y; for (x = d; x < N; x += d) if (gcdtable[x] == d) g.push_back(x); vector<int> mark(N, 0); int r; for (r = 0; r < d; r++) { int cmax = 0; for (x = r; x < N; x += d) cmax = max(cmax, v[x]); for (x = r; x < N; x += d) if (v[x] == cmax) mark[x] = 1; } x = 0; while (x < N) { if (mark[x % N] == 0) { x++; continue; } y = x; while (y < 2 * N) { if (mark[y % N] == 1) { y++; continue; } else break; } for (int& z : g) { if (z >= y - x + 1) break; if (y - z < N) res += max(y - x - z + 1, 0); else res += max(N - x, 0); } x = y; } } cout << res << endl; return 0; }
### Prompt Create a solution in Cpp for the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> void print(vector<T> v) { for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } long long gcd(long long u, long long v) { while (v != 0) { long long r = u % v; u = v; v = r; } return u; } int main() { int N; cin >> N; vector<int> v; int i; for (i = 0; i < N; i++) { int a; cin >> a; v.push_back(a); } vector<int> gcdtable(N, 0); for (i = 1; i < N; i++) gcdtable[i] = gcd(N, i); long long res = 0; int d; for (d = 1; d < N; d++) { if (N % d != 0) continue; vector<int> g; int x, y; for (x = d; x < N; x += d) if (gcdtable[x] == d) g.push_back(x); vector<int> mark(N, 0); int r; for (r = 0; r < d; r++) { int cmax = 0; for (x = r; x < N; x += d) cmax = max(cmax, v[x]); for (x = r; x < N; x += d) if (v[x] == cmax) mark[x] = 1; } x = 0; while (x < N) { if (mark[x % N] == 0) { x++; continue; } y = x; while (y < 2 * N) { if (mark[y % N] == 1) { y++; continue; } else break; } for (int& z : g) { if (z >= y - x + 1) break; if (y - z < N) res += max(y - x - z + 1, 0); else res += max(N - x, 0); } x = y; } } cout << res << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5; int N; int A[MAXN]; int gcd[MAXN]; int is_max[MAXN]; int cnt[MAXN]; int main() { ios_base::sync_with_stdio(0); cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; A[i + N] = A[i]; } long long res = 0; for (int g = 1; g <= N; g++) { if (N % g) continue; for (int j = g; j <= N; j += g) { gcd[j] = g; } } for (int g = 1; g <= N; g++) { if (N % g) continue; for (int i = 0; i < g; i++) { int m = 0; for (int j = i; j < N; j += g) { m = max(m, A[j]); } for (int j = i; j < N * 2; j += g) { is_max[j] = (A[j] == m); } } for (int j = 1; j < N * 2; j++) { if (is_max[j]) is_max[j] += is_max[j - 1]; } cnt[0] = 0; for (int i = 1; i < N; i++) { cnt[i] = cnt[i - 1] + (gcd[i] == g); } for (int j = N; j < N * 2; j++) { res += cnt[min(is_max[j], N - 1)]; } } cout << res << '\n'; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 5e5; int N; int A[MAXN]; int gcd[MAXN]; int is_max[MAXN]; int cnt[MAXN]; int main() { ios_base::sync_with_stdio(0); cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; A[i + N] = A[i]; } long long res = 0; for (int g = 1; g <= N; g++) { if (N % g) continue; for (int j = g; j <= N; j += g) { gcd[j] = g; } } for (int g = 1; g <= N; g++) { if (N % g) continue; for (int i = 0; i < g; i++) { int m = 0; for (int j = i; j < N; j += g) { m = max(m, A[j]); } for (int j = i; j < N * 2; j += g) { is_max[j] = (A[j] == m); } } for (int j = 1; j < N * 2; j++) { if (is_max[j]) is_max[j] += is_max[j - 1]; } cnt[0] = 0; for (int i = 1; i < N; i++) { cnt[i] = cnt[i - 1] + (gcd[i] == g); } for (int j = N; j < N * 2; j++) { res += cnt[min(is_max[j], N - 1)]; } } cout << res << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } bool isprime[1000005]; int prime[1000005], prinum; void getprime() { isprime[0] = 1; isprime[1] = 1; for (int i = 2; i < 1000005; i++) { if (!isprime[i]) { prime[prinum++] = i; } for (int j = 0; j < prinum; j++) { if (i * prime[j] > 1000005) break; isprime[i * prime[j]] = 1; if (i % prime[j] == 0) break; } } } int num[200005]; int n; bool mn[205][200005]; int sum[205][200005]; vector<int> vec[205]; int fac[205]; map<int, int> mp; int facnum; int main() { getprime(); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &num[i]); for (int i = 1; i < n; i++) { if (n % i == 0) { fac[facnum] = i; mp[i] = facnum++; } } for (int i = 0; i < facnum; i++) { for (int k = 0; k < fac[i]; k++) { int tmp = 0; for (int j = k; j < n; j += fac[i]) tmp = max(tmp, num[j]); for (int j = k; j < n; j += fac[i]) { if (num[j] == tmp) mn[i][j] = 1; } } } for (int i = 0; i < facnum; i++) { int pos = 0; for (; pos < n; pos++) { if (mn[i][pos] == 0) break; } if (pos == n) continue; else { for (int t = 0; t < n; t++) { if (mn[i][(pos + t) % n] == 1) { int len = 0; while (mn[i][(pos + t + len) % n] == 1) { len++; } vec[i].push_back(len); t += len; } } } } for (int i = 0; i < facnum; i++) { int siz = vec[i].size(); if (siz == 0) continue; else { sort(vec[i].begin(), vec[i].end()); sum[i][0] = vec[i][0]; for (int j = 1; j < siz; j++) { sum[i][j] = vec[i][j] + sum[i][j - 1]; } } } long long ans = 0; for (int i = 1; i < n; i++) { int gd = gcd(i, n); int siz = vec[mp[gd]].size(); if (siz == 0) ans = ans + n; else { int pos = lower_bound(vec[mp[gd]].begin(), vec[mp[gd]].end(), i) - vec[mp[gd]].begin(); ans = ans + sum[mp[gd]][siz - 1] - (pos == 0 ? 0 : sum[mp[gd]][pos - 1]) - (long long)(siz - pos) * (i - 1); } } printf("%I64d\n", ans); return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } bool isprime[1000005]; int prime[1000005], prinum; void getprime() { isprime[0] = 1; isprime[1] = 1; for (int i = 2; i < 1000005; i++) { if (!isprime[i]) { prime[prinum++] = i; } for (int j = 0; j < prinum; j++) { if (i * prime[j] > 1000005) break; isprime[i * prime[j]] = 1; if (i % prime[j] == 0) break; } } } int num[200005]; int n; bool mn[205][200005]; int sum[205][200005]; vector<int> vec[205]; int fac[205]; map<int, int> mp; int facnum; int main() { getprime(); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &num[i]); for (int i = 1; i < n; i++) { if (n % i == 0) { fac[facnum] = i; mp[i] = facnum++; } } for (int i = 0; i < facnum; i++) { for (int k = 0; k < fac[i]; k++) { int tmp = 0; for (int j = k; j < n; j += fac[i]) tmp = max(tmp, num[j]); for (int j = k; j < n; j += fac[i]) { if (num[j] == tmp) mn[i][j] = 1; } } } for (int i = 0; i < facnum; i++) { int pos = 0; for (; pos < n; pos++) { if (mn[i][pos] == 0) break; } if (pos == n) continue; else { for (int t = 0; t < n; t++) { if (mn[i][(pos + t) % n] == 1) { int len = 0; while (mn[i][(pos + t + len) % n] == 1) { len++; } vec[i].push_back(len); t += len; } } } } for (int i = 0; i < facnum; i++) { int siz = vec[i].size(); if (siz == 0) continue; else { sort(vec[i].begin(), vec[i].end()); sum[i][0] = vec[i][0]; for (int j = 1; j < siz; j++) { sum[i][j] = vec[i][j] + sum[i][j - 1]; } } } long long ans = 0; for (int i = 1; i < n; i++) { int gd = gcd(i, n); int siz = vec[mp[gd]].size(); if (siz == 0) ans = ans + n; else { int pos = lower_bound(vec[mp[gd]].begin(), vec[mp[gd]].end(), i) - vec[mp[gd]].begin(); ans = ans + sum[mp[gd]][siz - 1] - (pos == 0 ? 0 : sum[mp[gd]][pos - 1]) - (long long)(siz - pos) * (i - 1); } } printf("%I64d\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m, x, y, z, k, w; int A[200005], B[1005], C[400005], D[200005], g[200005], h[200005]; long long ans; int gcd(int x, int y) { int tmp; while (y != 0) { tmp = y; y = x % y; x = tmp; } return x; } long long f(int x, int y) { if (x < y) return 0; return (long long)(D[x] - D[y - 1]); } int main() { scanf("%d", &n); for (int i = (0); i < (n); i++) scanf("%d", &A[i]); for (int i = (1); i <= (n - 1); i++) g[i] = gcd(n, i); for (int i = (1); i <= (n - 1); i++) { if (n % i == 0) B[w++] = i; else D[i] = 1; } for (int i = (1); i <= (n); i++) D[i] += D[i - 1]; for (int k = (0); k < (w); k++) { for (int i = (1); i < (n); i++) if (g[i] == B[k]) h[i] = 1; else h[i] = 0; for (int i = (1); i < (n); i++) h[i] += h[i - 1]; for (int i = (0); i < (B[k]); i++) { x = 0; for (int j = i; j < n; j += B[k]) x = max(x, A[j]); for (int j = i; j < n; j += B[k]) if (A[j] == x) C[j] = 1; else C[j] = 0; } for (int i = (n); i < (2 * n - 1); i++) C[i] = C[i % n]; for (int i = (1); i < (2 * n - 1); i++) if (C[i] != 0) C[i] = C[i - 1] + C[i]; for (int i = (n - 1); i < (2 * n - 1); i++) { if (C[i] > n - 1) ans += (long long)(h[n - 1]); else ans += (long long)(h[C[i]]); } } printf("%I64d\n", ans); return 0; }
### Prompt Develop a solution in CPP to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, x, y, z, k, w; int A[200005], B[1005], C[400005], D[200005], g[200005], h[200005]; long long ans; int gcd(int x, int y) { int tmp; while (y != 0) { tmp = y; y = x % y; x = tmp; } return x; } long long f(int x, int y) { if (x < y) return 0; return (long long)(D[x] - D[y - 1]); } int main() { scanf("%d", &n); for (int i = (0); i < (n); i++) scanf("%d", &A[i]); for (int i = (1); i <= (n - 1); i++) g[i] = gcd(n, i); for (int i = (1); i <= (n - 1); i++) { if (n % i == 0) B[w++] = i; else D[i] = 1; } for (int i = (1); i <= (n); i++) D[i] += D[i - 1]; for (int k = (0); k < (w); k++) { for (int i = (1); i < (n); i++) if (g[i] == B[k]) h[i] = 1; else h[i] = 0; for (int i = (1); i < (n); i++) h[i] += h[i - 1]; for (int i = (0); i < (B[k]); i++) { x = 0; for (int j = i; j < n; j += B[k]) x = max(x, A[j]); for (int j = i; j < n; j += B[k]) if (A[j] == x) C[j] = 1; else C[j] = 0; } for (int i = (n); i < (2 * n - 1); i++) C[i] = C[i % n]; for (int i = (1); i < (2 * n - 1); i++) if (C[i] != 0) C[i] = C[i - 1] + C[i]; for (int i = (n - 1); i < (2 * n - 1); i++) { if (C[i] > n - 1) ans += (long long)(h[n - 1]); else ans += (long long)(h[C[i]]); } } printf("%I64d\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 4e5 + 10; int a[N], b[N], GCD[N], f[N], va[N]; inline int re() { int x = 0; char c = getchar(); bool p = 0; for (; c < '0' || c > '9'; c = getchar()) p |= c == '-'; for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0'; return p ? -x : x; } int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } inline int maxn(int x, int y) { return x > y ? x : y; } inline int minn(int x, int y) { return x < y ? x : y; } int main() { int i, j, n, m, d; long long s = 0; n = re(); m = n << 1; for (i = 1; i <= n; i++) a[i - 1] = a[i - 1 + n] = re(), GCD[i] = gcd(i, n); for (d = 1; d < n; d++) if (!(n % d)) { for (i = 0; i < d; i++) { b[i] = -1e9; for (j = i; j < m; j += d) b[i] = maxn(b[i], a[j]); for (j = i; j < m; j += d) f[j] = (a[j] == b[i]); } for (i = 1; i < m; i++) if (f[i]) f[i] += f[i - 1]; for (i = 1; i < n; i++) va[i] = va[i - 1] + (GCD[i] == d); for (i = n; i < m; i++) s += va[minn(n - 1, f[i])]; } printf("%I64d", s); return 0; }
### Prompt Generate a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 4e5 + 10; int a[N], b[N], GCD[N], f[N], va[N]; inline int re() { int x = 0; char c = getchar(); bool p = 0; for (; c < '0' || c > '9'; c = getchar()) p |= c == '-'; for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0'; return p ? -x : x; } int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } inline int maxn(int x, int y) { return x > y ? x : y; } inline int minn(int x, int y) { return x < y ? x : y; } int main() { int i, j, n, m, d; long long s = 0; n = re(); m = n << 1; for (i = 1; i <= n; i++) a[i - 1] = a[i - 1 + n] = re(), GCD[i] = gcd(i, n); for (d = 1; d < n; d++) if (!(n % d)) { for (i = 0; i < d; i++) { b[i] = -1e9; for (j = i; j < m; j += d) b[i] = maxn(b[i], a[j]); for (j = i; j < m; j += d) f[j] = (a[j] == b[i]); } for (i = 1; i < m; i++) if (f[i]) f[i] += f[i - 1]; for (i = 1; i < n; i++) va[i] = va[i - 1] + (GCD[i] == d); for (i = n; i < m; i++) s += va[minn(n - 1, f[i])]; } printf("%I64d", s); return 0; } ```
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { for (; y; swap(x %= y, y)) ; return x; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); for (int &x : a) cin >> x; long long res = 0; vector<int> h; vector<vector<int>> ss(n); for (int s = 1; s < n; ++s) ss[gcd(s, n)].push_back(s); for (int x = 1; x < n; ++x) if (n % x == 0) { vector<bool> u(n); for (int i = 0; i < x; ++i) { vector<int> c; int mx = 0; for (int j = i; j < n; j += x) { if (a[j] > mx) { c.clear(); mx = a[j]; } if (a[j] >= mx) c.push_back(j); } for (int j : c) u[j] = true; } for (int i = 0; i < n; ++i) { if (!u[i]) { vector<bool> t(u.begin(), u.begin() + i); u.erase(u.begin(), u.begin() + i); u.insert(u.end(), (t).begin(), (t).end()); break; } } for (int i = 0; i < n; ++i) { int k = 0; for (; i < n && u[i]; ++i, ++k) ; for (int s : ss[x]) { if (s > k) break; if (k < n) { res += k - s + 1; } else { res += n; } } } } cout << res << endl; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { for (; y; swap(x %= y, y)) ; return x; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); for (int &x : a) cin >> x; long long res = 0; vector<int> h; vector<vector<int>> ss(n); for (int s = 1; s < n; ++s) ss[gcd(s, n)].push_back(s); for (int x = 1; x < n; ++x) if (n % x == 0) { vector<bool> u(n); for (int i = 0; i < x; ++i) { vector<int> c; int mx = 0; for (int j = i; j < n; j += x) { if (a[j] > mx) { c.clear(); mx = a[j]; } if (a[j] >= mx) c.push_back(j); } for (int j : c) u[j] = true; } for (int i = 0; i < n; ++i) { if (!u[i]) { vector<bool> t(u.begin(), u.begin() + i); u.erase(u.begin(), u.begin() + i); u.insert(u.end(), (t).begin(), (t).end()); break; } } for (int i = 0; i < n; ++i) { int k = 0; for (; i < n && u[i]; ++i, ++k) ; for (int s : ss[x]) { if (s > k) break; if (k < n) { res += k - s + 1; } else { res += n; } } } } cout << res << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { while (a && b) a > b ? a %= b : b %= a; return a + b; } vector<int> divs(int x) { vector<int> res; for (int i = 1; i <= x; i++) { if (x % i == 0) res.push_back(i); } return res; } int n, ar[1 << 20]; long long ans; int mx[1 << 20]; int total[1 << 20], f[1 << 20]; long long solve(int G) { for (int i = 0; i < G; i++) mx[i] = -1; for (int i = 0; i < n; i++) mx[i % G] = max(mx[i % G], ar[i]); for (int i = 1; i <= n * 2; i++) { total[i] = total[i - 1]; if (f[i] == G) ++total[i]; } long long res = 0; int ptr = -1; for (int i = 0; i < n * 2; i++) { int need = mx[i % G]; if (ar[i] != need) { ptr = i; continue; } if (i < n) continue; int span = i - ptr; res += total[span]; } return res; } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) cin >> ar[i]; for (int i = n; i < n * 2; i++) ar[i] = ar[i - n]; for (int i = 1; i <= n; i++) f[i] = gcd(i, n); vector<int> vals = divs(n); for (int i = 0; i < vals.size(); i++) { ans += solve(vals[i]); } cout << ans - n << endl; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { while (a && b) a > b ? a %= b : b %= a; return a + b; } vector<int> divs(int x) { vector<int> res; for (int i = 1; i <= x; i++) { if (x % i == 0) res.push_back(i); } return res; } int n, ar[1 << 20]; long long ans; int mx[1 << 20]; int total[1 << 20], f[1 << 20]; long long solve(int G) { for (int i = 0; i < G; i++) mx[i] = -1; for (int i = 0; i < n; i++) mx[i % G] = max(mx[i % G], ar[i]); for (int i = 1; i <= n * 2; i++) { total[i] = total[i - 1]; if (f[i] == G) ++total[i]; } long long res = 0; int ptr = -1; for (int i = 0; i < n * 2; i++) { int need = mx[i % G]; if (ar[i] != need) { ptr = i; continue; } if (i < n) continue; int span = i - ptr; res += total[span]; } return res; } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) cin >> ar[i]; for (int i = n; i < n * 2; i++) ar[i] = ar[i - n]; for (int i = 1; i <= n; i++) f[i] = gcd(i, n); vector<int> vals = divs(n); for (int i = 0; i < vals.size(); i++) { ans += solve(vals[i]); } cout << ans - n << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } int ar[400001], tmp[400001]; vector<int> vc, use[200001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = (0); i < (n); ++i) cin >> ar[i]; for (int i = (0); i < (n); ++i) ar[i + n] = ar[i]; for (int i = (1); i < (n); ++i) if (n % i == 0) vc.push_back(i); for (int i = (1); i < (n); ++i) { use[gcd(i, n)].push_back(i); } long long res = 0; for (int i = (0); i < (vc.size()); ++i) { memset(tmp, 0, sizeof tmp); for (int j = (0); j < (vc[i]); ++j) { int mx = 0; for (int k = j; k < n; k += vc[i]) mx = max(mx, ar[k]); for (int k = j; k < n; k += vc[i]) if (ar[k] == mx) tmp[k] = 1; } for (int j = (0); j < (n); ++j) tmp[j + n] = tmp[j]; int st = -1; for (int j = (0); j < (n); ++j) { if (tmp[j] == 0) { st = j; break; } } if (st == -1) { res += (long long)(use[vc[i]].size()) * (long long)n; continue; } int last = st; int cnt = -1; for (int j = st + 1; j < st + n; j++) { if (tmp[j]) { while (cnt < (int)use[vc[i]].size() - 1 && use[vc[i]][cnt + 1] <= j - last) cnt++; res += cnt + 1; } else { last = j; cnt = -1; } } } cout << res << endl; }
### Prompt In cpp, your task is to solve the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } int ar[400001], tmp[400001]; vector<int> vc, use[200001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = (0); i < (n); ++i) cin >> ar[i]; for (int i = (0); i < (n); ++i) ar[i + n] = ar[i]; for (int i = (1); i < (n); ++i) if (n % i == 0) vc.push_back(i); for (int i = (1); i < (n); ++i) { use[gcd(i, n)].push_back(i); } long long res = 0; for (int i = (0); i < (vc.size()); ++i) { memset(tmp, 0, sizeof tmp); for (int j = (0); j < (vc[i]); ++j) { int mx = 0; for (int k = j; k < n; k += vc[i]) mx = max(mx, ar[k]); for (int k = j; k < n; k += vc[i]) if (ar[k] == mx) tmp[k] = 1; } for (int j = (0); j < (n); ++j) tmp[j + n] = tmp[j]; int st = -1; for (int j = (0); j < (n); ++j) { if (tmp[j] == 0) { st = j; break; } } if (st == -1) { res += (long long)(use[vc[i]].size()) * (long long)n; continue; } int last = st; int cnt = -1; for (int j = st + 1; j < st + n; j++) { if (tmp[j]) { while (cnt < (int)use[vc[i]].size() - 1 && use[vc[i]][cnt + 1] <= j - last) cnt++; res += cnt + 1; } else { last = j; cnt = -1; } } } cout << res << endl; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; int n, a[maxn], m[maxn], g[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 1; i < n; i++) { int a = i, b = n; while (a > 0) { int tmp = a; a = b % a; b = tmp; } g[i] = b; } long long res = 0; for (int i = 1; i < n; i++) { if (n % i == 0) { for (int j = 0; j < i; j++) { m[j] = a[j]; } for (int j = i; j < n; j++) { if (a[j] > m[j % i]) { m[j % i] = a[j]; } } for (int j = 0; j < n; j++) { if (a[j] == m[j % i]) { int len = 1; while (len < n && a[(j + len) % n] == m[(j + len) % i]) { ++len; } if (len < n) { for (int k = 1; k <= len; k++) { if (g[k] == i) { res += len - k + 1; } } } else { for (int k = 1; k <= len; k++) { if (g[k] == i) { res += len; } } } j += len - 1; if (j >= n) { int over = j - n + 1; for (int k = 1; k <= over; k++) { if (g[k] == i) { res -= over - k + 1; } } } } } } } cout << res << '\n'; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 200010; int n, a[maxn], m[maxn], g[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 1; i < n; i++) { int a = i, b = n; while (a > 0) { int tmp = a; a = b % a; b = tmp; } g[i] = b; } long long res = 0; for (int i = 1; i < n; i++) { if (n % i == 0) { for (int j = 0; j < i; j++) { m[j] = a[j]; } for (int j = i; j < n; j++) { if (a[j] > m[j % i]) { m[j % i] = a[j]; } } for (int j = 0; j < n; j++) { if (a[j] == m[j % i]) { int len = 1; while (len < n && a[(j + len) % n] == m[(j + len) % i]) { ++len; } if (len < n) { for (int k = 1; k <= len; k++) { if (g[k] == i) { res += len - k + 1; } } } else { for (int k = 1; k <= len; k++) { if (g[k] == i) { res += len; } } } j += len - 1; if (j >= n) { int over = j - n + 1; for (int k = 1; k <= over; k++) { if (g[k] == i) { res -= over - k + 1; } } } } } } } cout << res << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; namespace mainns { template <typename T> void relaxMax(T& relaxWhat, const T& relaxBy) { relaxWhat = max(relaxWhat, relaxBy); } template <typename T> void relaxMin(T& relaxWhat, const T& relaxBy) { relaxWhat = min(relaxWhat, relaxBy); } template <typename T> int64_t sgn(T val) { return (T(0) < val) - (val < T(0)); } namespace { inline uint64_t gcdInternal(uint64_t a, uint64_t b) { if (b == 0) return a; return gcdInternal(b, a % b); } inline uint64_t gcdDecompositionInternal(uint64_t a, uint64_t b, int64_t& x, int64_t& y) { if (b == 0) { x = 1; y = 0; return a; } int64_t xx, yy; auto gcd = gcdDecompositionInternal(b, a % b, xx, yy); x = yy; y = xx - (a / b) * yy; return gcd; } } // namespace inline uint64_t greatestCommonDivisor(int64_t a, int64_t b) { a *= sgn(a); b *= sgn(b); if (a < b) std::swap(a, b); return gcdInternal(a, b); } inline uint64_t gcdDecomposition(int64_t a, int64_t b, int64_t& x, int64_t& y) { auto aUnsigned = a * sgn(a); auto bUnsigned = b * sgn(b); auto gcd = (aUnsigned < bUnsigned) ? gcdDecompositionInternal(aUnsigned, bUnsigned, x, y) : gcdDecompositionInternal(bUnsigned, aUnsigned, y, x); x *= sgn(a); y *= sgn(b); return gcd; } class Solver582C { public: void run(); }; void Solver582C::run() { int64_t n; cin >> n; valarray<int64_t> numbers(n); for (auto& elt : numbers) cin >> elt; if (n == 1) { cout << 0; return; } vector<int64_t> divisorsOfN; for (int64_t d = 1; d * d <= n; ++d) if (n % d == 0) { divisorsOfN.push_back(d); if (n / d > d && d != 1) divisorsOfN.push_back(n / d); } int64_t ans = 0; for (auto& d : divisorsOfN) { vector<int64_t> maximums(d); for (int64_t i = 0; i < d; ++i) maximums[i] = valarray<int64_t>(numbers[slice(i, n / d, d)]).max(); vector<int64_t> consequentMaximumsLengths; bool onConsequentMaximum = false; for (int64_t i = 0; i < n; ++i) { if (numbers[i] == maximums[i % d]) { if (onConsequentMaximum) ++consequentMaximumsLengths.back(); else { consequentMaximumsLengths.push_back(1); onConsequentMaximum = true; } } else onConsequentMaximum = false; } if (onConsequentMaximum && numbers[0] == maximums[0] && consequentMaximumsLengths.size() > 1) { consequentMaximumsLengths.front() += consequentMaximumsLengths.back(); consequentMaximumsLengths.pop_back(); } for (auto l : consequentMaximumsLengths) for (int64_t multiplier = 1; d * multiplier <= l; ++multiplier) { if (greatestCommonDivisor(n / d, multiplier) > 1) continue; if (l == n) ans += l; else ans += l - d * multiplier + 1; } } cout << ans; } } // namespace mainns using CurrentSolver = mainns::Solver582C; int main() { ios::sync_with_stdio(false); CurrentSolver().run(); return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; namespace mainns { template <typename T> void relaxMax(T& relaxWhat, const T& relaxBy) { relaxWhat = max(relaxWhat, relaxBy); } template <typename T> void relaxMin(T& relaxWhat, const T& relaxBy) { relaxWhat = min(relaxWhat, relaxBy); } template <typename T> int64_t sgn(T val) { return (T(0) < val) - (val < T(0)); } namespace { inline uint64_t gcdInternal(uint64_t a, uint64_t b) { if (b == 0) return a; return gcdInternal(b, a % b); } inline uint64_t gcdDecompositionInternal(uint64_t a, uint64_t b, int64_t& x, int64_t& y) { if (b == 0) { x = 1; y = 0; return a; } int64_t xx, yy; auto gcd = gcdDecompositionInternal(b, a % b, xx, yy); x = yy; y = xx - (a / b) * yy; return gcd; } } // namespace inline uint64_t greatestCommonDivisor(int64_t a, int64_t b) { a *= sgn(a); b *= sgn(b); if (a < b) std::swap(a, b); return gcdInternal(a, b); } inline uint64_t gcdDecomposition(int64_t a, int64_t b, int64_t& x, int64_t& y) { auto aUnsigned = a * sgn(a); auto bUnsigned = b * sgn(b); auto gcd = (aUnsigned < bUnsigned) ? gcdDecompositionInternal(aUnsigned, bUnsigned, x, y) : gcdDecompositionInternal(bUnsigned, aUnsigned, y, x); x *= sgn(a); y *= sgn(b); return gcd; } class Solver582C { public: void run(); }; void Solver582C::run() { int64_t n; cin >> n; valarray<int64_t> numbers(n); for (auto& elt : numbers) cin >> elt; if (n == 1) { cout << 0; return; } vector<int64_t> divisorsOfN; for (int64_t d = 1; d * d <= n; ++d) if (n % d == 0) { divisorsOfN.push_back(d); if (n / d > d && d != 1) divisorsOfN.push_back(n / d); } int64_t ans = 0; for (auto& d : divisorsOfN) { vector<int64_t> maximums(d); for (int64_t i = 0; i < d; ++i) maximums[i] = valarray<int64_t>(numbers[slice(i, n / d, d)]).max(); vector<int64_t> consequentMaximumsLengths; bool onConsequentMaximum = false; for (int64_t i = 0; i < n; ++i) { if (numbers[i] == maximums[i % d]) { if (onConsequentMaximum) ++consequentMaximumsLengths.back(); else { consequentMaximumsLengths.push_back(1); onConsequentMaximum = true; } } else onConsequentMaximum = false; } if (onConsequentMaximum && numbers[0] == maximums[0] && consequentMaximumsLengths.size() > 1) { consequentMaximumsLengths.front() += consequentMaximumsLengths.back(); consequentMaximumsLengths.pop_back(); } for (auto l : consequentMaximumsLengths) for (int64_t multiplier = 1; d * multiplier <= l; ++multiplier) { if (greatestCommonDivisor(n / d, multiplier) > 1) continue; if (l == n) ans += l; else ans += l - d * multiplier + 1; } } cout << ans; } } // namespace mainns using CurrentSolver = mainns::Solver582C; int main() { ios::sync_with_stdio(false); CurrentSolver().run(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int N, a[200100], mx[200100], gcd[200100]; int gc(int x, int y) { if (!x) return y; if (!y) return x; return gc(y, x % y); } int main() { scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", &a[i]); } for (int i = 1; i <= N; ++i) { gcd[i] = gc(i, N); } long long ret = 0; for (int d = 1; d < N; ++d) { if (N % d) continue; for (int i = 0; i < d; ++i) { mx[i] = a[i]; } for (int i = 0; i < N; ++i) { if (a[i] > mx[i % d]) { mx[i % d] = a[i]; } } int start = -1; for (int i = 0; i < N; ++i) { if (a[i] < mx[i % d]) { start = i; break; } } if (start == -1) { for (int j = 1; j <= N; ++j) { if (gcd[j] == d) { ret += N; } } continue; } int cnt = 0; for (int i = start; i <= start + N; ++i) { if (a[i % N] != mx[i % d]) { if (!cnt) continue; for (int j = 1; j <= cnt; ++j) { if (gcd[j] == d) { ret += cnt - j + 1; } } cnt = 0; } else { ++cnt; } } } cout << ret << "\n"; return 0; }
### Prompt Create a solution in Cpp for the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int N, a[200100], mx[200100], gcd[200100]; int gc(int x, int y) { if (!x) return y; if (!y) return x; return gc(y, x % y); } int main() { scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", &a[i]); } for (int i = 1; i <= N; ++i) { gcd[i] = gc(i, N); } long long ret = 0; for (int d = 1; d < N; ++d) { if (N % d) continue; for (int i = 0; i < d; ++i) { mx[i] = a[i]; } for (int i = 0; i < N; ++i) { if (a[i] > mx[i % d]) { mx[i % d] = a[i]; } } int start = -1; for (int i = 0; i < N; ++i) { if (a[i] < mx[i % d]) { start = i; break; } } if (start == -1) { for (int j = 1; j <= N; ++j) { if (gcd[j] == d) { ret += N; } } continue; } int cnt = 0; for (int i = start; i <= start + N; ++i) { if (a[i % N] != mx[i % d]) { if (!cnt) continue; for (int j = 1; j <= cnt; ++j) { if (gcd[j] == d) { ret += cnt - j + 1; } } cnt = 0; } else { ++cnt; } } } cout << ret << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; const int M = 10 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-6; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } int n, mx, a[N << 1], cnt[N << 1], f[N << 1]; bool flag[N << 1]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); a[i + n] = a[i]; } long long ans = 0; for (int d = 1; d < n; d++) { if (n % d) continue; memset(flag, false, sizeof(flag)); for (int k = 0; k < d; k++) { mx = 0; for (int i = k; i < 2 * n; i += d) mx = max(mx, a[i]); for (int i = k; i < 2 * n; i += d) flag[i] = (a[i] == mx); } f[0] = flag[0]; for (int i = 1; i < 2 * n; i++) { if (flag[i]) f[i] = f[i - 1] + 1; else f[i] = 0; f[i] = min(f[i], n - 1); } cnt[0] = 0; for (int i = 1; i < n / d; i++) cnt[i] = cnt[i - 1] + (gcd(i, n / d) == 1); for (int i = n; i < n * 2; i++) ans += cnt[f[i] / d]; } printf("%lld\n", ans); return 0; }
### Prompt Please create a solution in CPP to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; const int M = 10 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-6; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } int n, mx, a[N << 1], cnt[N << 1], f[N << 1]; bool flag[N << 1]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); a[i + n] = a[i]; } long long ans = 0; for (int d = 1; d < n; d++) { if (n % d) continue; memset(flag, false, sizeof(flag)); for (int k = 0; k < d; k++) { mx = 0; for (int i = k; i < 2 * n; i += d) mx = max(mx, a[i]); for (int i = k; i < 2 * n; i += d) flag[i] = (a[i] == mx); } f[0] = flag[0]; for (int i = 1; i < 2 * n; i++) { if (flag[i]) f[i] = f[i - 1] + 1; else f[i] = 0; f[i] = min(f[i], n - 1); } cnt[0] = 0; for (int i = 1; i < n / d; i++) cnt[i] = cnt[i - 1] + (gcd(i, n / d) == 1); for (int i = n; i < n * 2; i++) ans += cnt[f[i] / d]; } printf("%lld\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 2; int a[N * 2], b[N]; vector<int> v[N]; int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { int t, n, i, l, x, k; long long ret = 0; scanf("%d", &n); for (i = 0; i < n; ++i) scanf("%d", &a[i]), a[i + n] = a[i]; for (i = 1; i < n; ++i) v[gcd(i, n)].push_back(i); for (t = 1; t < n; ++t) { if (v[t].size() == 0) continue; l = t, x = 0, k = 0; for (i = 0; i < l; ++i) b[i] = a[i]; for (i = l; i < n; ++i) b[i % l] = max(b[i % l], a[i]); for (i = 0; i < n * 2; ++i) { if (a[i] == b[i % l]) { ++k; if (x < v[t].size() && v[t][x] <= k) ++x; } else k = 0, x = 0; if (i >= n) ret += x; } } cout << ret << endl; }
### Prompt Please create a solution in Cpp to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 2; int a[N * 2], b[N]; vector<int> v[N]; int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { int t, n, i, l, x, k; long long ret = 0; scanf("%d", &n); for (i = 0; i < n; ++i) scanf("%d", &a[i]), a[i + n] = a[i]; for (i = 1; i < n; ++i) v[gcd(i, n)].push_back(i); for (t = 1; t < n; ++t) { if (v[t].size() == 0) continue; l = t, x = 0, k = 0; for (i = 0; i < l; ++i) b[i] = a[i]; for (i = l; i < n; ++i) b[i % l] = max(b[i % l], a[i]); for (i = 0; i < n * 2; ++i) { if (a[i] == b[i % l]) { ++k; if (x < v[t].size() && v[t][x] <= k) ++x; } else k = 0, x = 0; if (i >= n) ret += x; } } cout << ret << endl; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; int phi[maxn], G[maxn]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } void phi_table(int n, int g) { phi[0] = 0; for (int i = 1; i < n; i++) if (G[i] != g) phi[i] = phi[i - 1]; else phi[i] = phi[i - 1] + 1; } int a[maxn]; bool use[maxn], ismax[maxn]; int main() { int n; long long ans = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) G[i] = gcd(i, n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) { if (n % i != 0) continue; phi_table(n, i); memset(use, false, sizeof(use)); memset(ismax, false, sizeof(ismax)); for (int j = 0; j < i; j++) { int ma = -1; for (int k = j; k < n; k += i) ma = max(a[k], ma); for (int k = j; k < n; k += i) if (a[k] == ma) ismax[k] = true; } int r = 0; for (int j = 0; j < n; j++) { if (r < j) r = j; while (true) { if (r >= n && (r % n) == j) break; if (ismax[r % n] == false) break; else r++; } int d = min(n - 1, r - j); ans += phi[d]; } } printf("%I64d\n", ans); return 0; }
### Prompt Generate a CPP solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 200005; int phi[maxn], G[maxn]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } void phi_table(int n, int g) { phi[0] = 0; for (int i = 1; i < n; i++) if (G[i] != g) phi[i] = phi[i - 1]; else phi[i] = phi[i - 1] + 1; } int a[maxn]; bool use[maxn], ismax[maxn]; int main() { int n; long long ans = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) G[i] = gcd(i, n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) { if (n % i != 0) continue; phi_table(n, i); memset(use, false, sizeof(use)); memset(ismax, false, sizeof(ismax)); for (int j = 0; j < i; j++) { int ma = -1; for (int k = j; k < n; k += i) ma = max(a[k], ma); for (int k = j; k < n; k += i) if (a[k] == ma) ismax[k] = true; } int r = 0; for (int j = 0; j < n; j++) { if (r < j) r = j; while (true) { if (r >= n && (r % n) == j) break; if (ismax[r % n] == false) break; else r++; } int d = min(n - 1, r - j); ans += phi[d]; } } printf("%I64d\n", ans); return 0; } ```
#include <bits/stdc++.h> int n; int a[200100]; int sup[200100]; int chk[200100]; int cnt[200100]; int mgcd[200100]; long long int ans; int main() { int i, j; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 1; i <= n; i++) { if (n % i == 0) { for (j = i; j <= n; j += i) { mgcd[j] = i; } } } for (i = 1; i < n; i++) { if (n % i == 0) { for (j = 0; j < i; j++) { sup[j] = 0; } for (j = 0; j < n; j++) { if (a[j] > sup[j % i]) sup[j % i] = a[j]; } chk[0] = 0; for (j = 0; j < n; j++) { if (a[j] == sup[j % i]) chk[j]++; else chk[j] = 0; if (chk[j] > n) chk[j] = n; chk[j + 1] = chk[j]; } chk[0] = chk[n]; for (j = 0; j < n; j++) { if (a[j] == sup[j % i]) chk[j]++; else chk[j] = 0; if (chk[j] > n) chk[j] = n; chk[j + 1] = chk[j]; } cnt[0] = 0; for (j = 1; j <= n; j++) { cnt[j] = cnt[j - 1]; if (mgcd[j] == i) cnt[j]++; } for (j = 0; j < n; j++) { ans += cnt[chk[j]]; } } } printf("%I64d", ans); }
### Prompt Your task is to create a cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> int n; int a[200100]; int sup[200100]; int chk[200100]; int cnt[200100]; int mgcd[200100]; long long int ans; int main() { int i, j; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 1; i <= n; i++) { if (n % i == 0) { for (j = i; j <= n; j += i) { mgcd[j] = i; } } } for (i = 1; i < n; i++) { if (n % i == 0) { for (j = 0; j < i; j++) { sup[j] = 0; } for (j = 0; j < n; j++) { if (a[j] > sup[j % i]) sup[j % i] = a[j]; } chk[0] = 0; for (j = 0; j < n; j++) { if (a[j] == sup[j % i]) chk[j]++; else chk[j] = 0; if (chk[j] > n) chk[j] = n; chk[j + 1] = chk[j]; } chk[0] = chk[n]; for (j = 0; j < n; j++) { if (a[j] == sup[j % i]) chk[j]++; else chk[j] = 0; if (chk[j] > n) chk[j] = n; chk[j + 1] = chk[j]; } cnt[0] = 0; for (j = 1; j <= n; j++) { cnt[j] = cnt[j - 1]; if (mgcd[j] == i) cnt[j]++; } for (j = 0; j < n; j++) { ans += cnt[chk[j]]; } } } printf("%I64d", ans); } ```
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { while (b) { a %= b; swap(a, b); } return a; } vector<int> calcMaxLen(const vector<int> &f) { auto zero = find(f.begin(), f.end(), 0); if (zero == f.end()) { return vector<int>(); } const int n = f.size(); vector<int> dp(n); if (f[n - 1]) { for (int i = 0; i < n; ++i) { if (f[i] == 0) { dp[n - 1] = i + 1; break; } } } for (int i = n - 2; i >= 0; --i) { if (f[i]) { dp[i] = dp[i + 1] + 1; } } return dp; } int n; vector<int> a; vector<vector<int> > lengths; vector<int> calcFunc(int period) { vector<int> mx(n); vector<int> res(n); for (int i = 0; i < n; ++i) { if (i - period >= 0) { mx[i] = mx[i - period]; } else { mx[i] = a[i]; for (int j = i; j < n; j += period) { mx[i] = max(mx[i], a[j]); } } res[i] = int(a[i] >= mx[i]); } return res; } int main() { scanf("%d", &n); a.resize(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } lengths.resize(n); for (int len = 1; len < n; ++len) { lengths[gcd(n, len)].push_back(len); } long long ans = 0; for (int period = 1; period < n; ++period) { const auto &lens = lengths[period]; if (lens.size()) { auto f = calcFunc(period); auto maxlen = calcMaxLen(f); if (maxlen.empty()) { for (int len : lens) { ans += n; } } else { vector<int> cnt(n + 100); for (int mxlen : maxlen) { cnt[mxlen]++; } vector<long long> pref(cnt.size() + 1); for (int i = 0; i < cnt.size(); ++i) { pref[i + 1] = pref[i] + cnt[i]; } auto sum = [pref](int l, int r) { return pref[r + 1] - pref[l]; }; for (int len : lens) { ans += sum(len, cnt.size() - 1); } } } } printf("%I64d\n", ans); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { while (b) { a %= b; swap(a, b); } return a; } vector<int> calcMaxLen(const vector<int> &f) { auto zero = find(f.begin(), f.end(), 0); if (zero == f.end()) { return vector<int>(); } const int n = f.size(); vector<int> dp(n); if (f[n - 1]) { for (int i = 0; i < n; ++i) { if (f[i] == 0) { dp[n - 1] = i + 1; break; } } } for (int i = n - 2; i >= 0; --i) { if (f[i]) { dp[i] = dp[i + 1] + 1; } } return dp; } int n; vector<int> a; vector<vector<int> > lengths; vector<int> calcFunc(int period) { vector<int> mx(n); vector<int> res(n); for (int i = 0; i < n; ++i) { if (i - period >= 0) { mx[i] = mx[i - period]; } else { mx[i] = a[i]; for (int j = i; j < n; j += period) { mx[i] = max(mx[i], a[j]); } } res[i] = int(a[i] >= mx[i]); } return res; } int main() { scanf("%d", &n); a.resize(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } lengths.resize(n); for (int len = 1; len < n; ++len) { lengths[gcd(n, len)].push_back(len); } long long ans = 0; for (int period = 1; period < n; ++period) { const auto &lens = lengths[period]; if (lens.size()) { auto f = calcFunc(period); auto maxlen = calcMaxLen(f); if (maxlen.empty()) { for (int len : lens) { ans += n; } } else { vector<int> cnt(n + 100); for (int mxlen : maxlen) { cnt[mxlen]++; } vector<long long> pref(cnt.size() + 1); for (int i = 0; i < cnt.size(); ++i) { pref[i + 1] = pref[i] + cnt[i]; } auto sum = [pref](int l, int r) { return pref[r + 1] - pref[l]; }; for (int len : lens) { ans += sum(len, cnt.size() - 1); } } } } printf("%I64d\n", ans); return 0; } ```
#include <bits/stdc++.h> static const int MAXN = 200003; int n; int a[MAXN]; std::vector<int> gcd_with_n[MAXN]; int len_ct[MAXN]; bool avail[MAXN]; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) gcd_with_n[gcd(n, i)].push_back(i); long long ans = 0; for (int g = 1; g < n; ++g) if (n % g == 0) { memset(len_ct, 0, sizeof len_ct); memset(avail, false, sizeof avail); for (int i = 0; i < g; ++i) { int max = -1; for (int j = i; j < n; j += g) if (max < a[j]) max = a[j]; for (int j = i; j < n; j += g) if (max == a[j]) avail[j] = true; } bool all_avail = true; for (int i = 0; i < n; ++i) if (!avail[i]) { all_avail = false; break; } if (all_avail) { for (int i = 1; i <= n; ++i) len_ct[i] = n; } else { int i = 0, j; while (avail[i]) ++i; while (1) { j = (i + 1) % n; int len = 0; while (avail[j]) { ++len; j = (j + 1) % n; } for (int i = 1; i <= len; ++i) len_ct[i] += len - i + 1; if (j <= i) break; i = j; } } for (std::vector<int>::iterator i = gcd_with_n[g].begin(); i != gcd_with_n[g].end(); ++i) { ans += len_ct[*i]; } } printf("%I64d\n", ans); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> static const int MAXN = 200003; int n; int a[MAXN]; std::vector<int> gcd_with_n[MAXN]; int len_ct[MAXN]; bool avail[MAXN]; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) gcd_with_n[gcd(n, i)].push_back(i); long long ans = 0; for (int g = 1; g < n; ++g) if (n % g == 0) { memset(len_ct, 0, sizeof len_ct); memset(avail, false, sizeof avail); for (int i = 0; i < g; ++i) { int max = -1; for (int j = i; j < n; j += g) if (max < a[j]) max = a[j]; for (int j = i; j < n; j += g) if (max == a[j]) avail[j] = true; } bool all_avail = true; for (int i = 0; i < n; ++i) if (!avail[i]) { all_avail = false; break; } if (all_avail) { for (int i = 1; i <= n; ++i) len_ct[i] = n; } else { int i = 0, j; while (avail[i]) ++i; while (1) { j = (i + 1) % n; int len = 0; while (avail[j]) { ++len; j = (j + 1) % n; } for (int i = 1; i <= len; ++i) len_ct[i] += len - i + 1; if (j <= i) break; i = j; } } for (std::vector<int>::iterator i = gcd_with_n[g].begin(); i != gcd_with_n[g].end(); ++i) { ans += len_ct[*i]; } } printf("%I64d\n", ans); return 0; } ```
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; int factor[1005], tot, total, val[200005], _max[200005], g[200005]; long long ans; void calc(int num, int k, bool mark) { for (int i = k; i <= num; i += k) if (g[i] == k) if (mark) ans += num - i + 1; else ans += num; } int main() { scanf("%d", &total); for (int i = 0; i < total; i++) scanf("%d", val + i); for (int i = 1; i * i <= total; i++) { if (total % i == 0) { factor[tot++] = i; if (i * i != total) factor[tot++] = total / i; } } sort(factor, factor + tot); for (int i = 0; i < tot; i++) { int k = factor[i]; for (int j = k; j <= total; j += k) g[j] = k; } for (int i = 0; i < tot - 1; i++) { int k = factor[i]; for (int j = total - 1; j >= 0; j--) if (total - j <= k) _max[j] = val[j]; else _max[j] = max(val[j], _max[j + k]); int cur = 0, from = 0, to = total - 1; while (val[from] == _max[from % k]) from++; while (val[to] == _max[to % k]) to--; if (from <= to) cur = from + (total - to - 1); else { calc(total, k, 0); continue; } calc(cur, k, 1); cur = 0; for (int j = from; j <= to; j++) if (val[j] == _max[j % k]) cur++; else { calc(cur, k, 1); cur = 0; } } cout << ans; return 0; }
### Prompt Develop a solution in CPP to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; int factor[1005], tot, total, val[200005], _max[200005], g[200005]; long long ans; void calc(int num, int k, bool mark) { for (int i = k; i <= num; i += k) if (g[i] == k) if (mark) ans += num - i + 1; else ans += num; } int main() { scanf("%d", &total); for (int i = 0; i < total; i++) scanf("%d", val + i); for (int i = 1; i * i <= total; i++) { if (total % i == 0) { factor[tot++] = i; if (i * i != total) factor[tot++] = total / i; } } sort(factor, factor + tot); for (int i = 0; i < tot; i++) { int k = factor[i]; for (int j = k; j <= total; j += k) g[j] = k; } for (int i = 0; i < tot - 1; i++) { int k = factor[i]; for (int j = total - 1; j >= 0; j--) if (total - j <= k) _max[j] = val[j]; else _max[j] = max(val[j], _max[j + k]); int cur = 0, from = 0, to = total - 1; while (val[from] == _max[from % k]) from++; while (val[to] == _max[to % k]) to--; if (from <= to) cur = from + (total - to - 1); else { calc(total, k, 0); continue; } calc(cur, k, 1); cur = 0; for (int j = from; j <= to; j++) if (val[j] == _max[j % k]) cur++; else { calc(cur, k, 1); cur = 0; } } cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> std::istream& operator>>(std::istream& i, pair<T, U>& p) { i >> p.first >> p.second; return i; } template <typename T> std::istream& operator>>(std::istream& i, vector<T>& t) { for (auto& v : t) { i >> v; } return i; } template <typename T, typename U> std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { o << p.first << ' ' << p.second; return o; } template <typename T> std::ostream& operator<<(std::ostream& o, const vector<T>& t) { if (t.empty()) o << '\n'; for (size_t i = 0; i < t.size(); ++i) { o << t[i] << " \n"[i == t.size() - 1]; } return o; } template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>; template <typename T> bool in(T a, T b, T c) { return a <= b && b < c; } unsigned int logceil(int first) { return 8 * sizeof(int) - __builtin_clz(first); } namespace std { template <typename T, typename U> struct hash<pair<T, U>> { hash<T> t; hash<U> u; size_t operator()(const pair<T, U>& p) const { return t(p.first) ^ (u(p.second) << 7); } }; } // namespace std template <typename T, typename F> T bsh(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { l = m + 1; r = m; } else { h = m - 1; } } return r; } template <typename F> double bshd(double l, double h, const F& f, double p = 1e-9) { unsigned int r = 3 + (unsigned int)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { l = m; } else { h = m; } } return (l + h) / 2; } template <typename T, typename F> T bsl(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { h = m - 1; r = m; } else { l = m + 1; } } return r; } template <typename F> double bsld(double l, double h, const F& f, double p = 1e-9) { unsigned int r = 3 + (unsigned int)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { h = m; } else { l = m; } } return (l + h) / 2; } template <typename T> T gcd(T a, T b) { if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } template <typename T> class vector2 : public vector<vector<T>> { public: vector2() {} vector2(size_t a, size_t b, T t = T()) : vector<vector<T>>(a, vector<T>(b, t)) {} }; template <typename T> class vector3 : public vector<vector2<T>> { public: vector3() {} vector3(size_t a, size_t b, size_t c, T t = T()) : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} }; template <typename T> class vector4 : public vector<vector3<T>> { public: vector4() {} vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} }; template <typename T> class vector5 : public vector<vector4<T>> { public: vector5() {} vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} }; class superiorperiodicsubarrays { public: void solve(istream& cin, ostream& cout) { int N; cin >> N; long long ans = 0; vector<int> A(N, 1); cin >> A; vector<bool> R(2 * N); for (int s = 1; s < N; ++s) { if (N % s) continue; for (int i = 0; i < s; ++i) { int m = A[i]; for (int j = i + s; j < N; j += s) m = max(m, A[j]); for (int j = i; j < N; j += s) R[j + N] = R[j] = A[j] >= m; } vector<int> CP(N / s + 1, 0); CP[1] = 1; for (int i = 2; i <= N / s; ++i) CP[i] = CP[i - 1] + (gcd(N / s, i) == 1); int e = 0; for (int b = 0; b < N; ++b) { if (e < b) e++; while (e < b + N && R[e]) ++e; int l = e - b; ans += CP[l / s]; } } cout << ans << '\n'; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); superiorperiodicsubarrays solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
### Prompt Generate a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T, typename U> std::istream& operator>>(std::istream& i, pair<T, U>& p) { i >> p.first >> p.second; return i; } template <typename T> std::istream& operator>>(std::istream& i, vector<T>& t) { for (auto& v : t) { i >> v; } return i; } template <typename T, typename U> std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { o << p.first << ' ' << p.second; return o; } template <typename T> std::ostream& operator<<(std::ostream& o, const vector<T>& t) { if (t.empty()) o << '\n'; for (size_t i = 0; i < t.size(); ++i) { o << t[i] << " \n"[i == t.size() - 1]; } return o; } template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>; template <typename T> bool in(T a, T b, T c) { return a <= b && b < c; } unsigned int logceil(int first) { return 8 * sizeof(int) - __builtin_clz(first); } namespace std { template <typename T, typename U> struct hash<pair<T, U>> { hash<T> t; hash<U> u; size_t operator()(const pair<T, U>& p) const { return t(p.first) ^ (u(p.second) << 7); } }; } // namespace std template <typename T, typename F> T bsh(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { l = m + 1; r = m; } else { h = m - 1; } } return r; } template <typename F> double bshd(double l, double h, const F& f, double p = 1e-9) { unsigned int r = 3 + (unsigned int)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { l = m; } else { h = m; } } return (l + h) / 2; } template <typename T, typename F> T bsl(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { h = m - 1; r = m; } else { l = m + 1; } } return r; } template <typename F> double bsld(double l, double h, const F& f, double p = 1e-9) { unsigned int r = 3 + (unsigned int)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { h = m; } else { l = m; } } return (l + h) / 2; } template <typename T> T gcd(T a, T b) { if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } template <typename T> class vector2 : public vector<vector<T>> { public: vector2() {} vector2(size_t a, size_t b, T t = T()) : vector<vector<T>>(a, vector<T>(b, t)) {} }; template <typename T> class vector3 : public vector<vector2<T>> { public: vector3() {} vector3(size_t a, size_t b, size_t c, T t = T()) : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} }; template <typename T> class vector4 : public vector<vector3<T>> { public: vector4() {} vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} }; template <typename T> class vector5 : public vector<vector4<T>> { public: vector5() {} vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} }; class superiorperiodicsubarrays { public: void solve(istream& cin, ostream& cout) { int N; cin >> N; long long ans = 0; vector<int> A(N, 1); cin >> A; vector<bool> R(2 * N); for (int s = 1; s < N; ++s) { if (N % s) continue; for (int i = 0; i < s; ++i) { int m = A[i]; for (int j = i + s; j < N; j += s) m = max(m, A[j]); for (int j = i; j < N; j += s) R[j + N] = R[j] = A[j] >= m; } vector<int> CP(N / s + 1, 0); CP[1] = 1; for (int i = 2; i <= N / s; ++i) CP[i] = CP[i - 1] + (gcd(N / s, i) == 1); int e = 0; for (int b = 0; b < N; ++b) { if (e < b) e++; while (e < b + N && R[e]) ++e; int l = e - b; ans += CP[l / s]; } } cout << ans << '\n'; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); superiorperiodicsubarrays solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> d; bool mark[200010] = {0}; int a[200010]; bool f[200010]; vector<int> b; int dp[200010]; long long ans = 0; int main() { int n, i, j, k; scanf("%d", &n); for (i = 0; i < n; ++i) scanf("%d", &a[i]); for (i = n - 1; i >= 1; --i) if (n % i == 0) d.push_back(i); for (i = 0; i < d.size(); ++i) { memset(f, 0, sizeof(f)); for (j = 0; j < d[i]; ++j) { int maxx = 0; for (k = j; k < n; k += d[i]) maxx = max(maxx, a[k]); for (k = j; k < n; k += d[i]) if (a[k] == maxx) f[k] = 1; } b.clear(); for (j = d[i]; j < n; j += d[i]) if (!mark[j]) { mark[j] = 1; b.push_back(j); } for (j = n - 1; j >= 0; --j) { if (!f[j]) dp[j] = 0; else if (j == n - 1) dp[j] = 1; else dp[j] = 1 + dp[j + 1]; } for (j = 0; j < n; ++j) { int val = dp[j]; if (dp[j] == (n - j) && dp[0]) val += dp[0]; int pos = upper_bound(b.begin(), b.end(), min(n - 1, val)) - b.begin(); if (pos >= 0) ans += pos; } } printf("%lld\n", ans); }
### Prompt Please formulate a CPP solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> d; bool mark[200010] = {0}; int a[200010]; bool f[200010]; vector<int> b; int dp[200010]; long long ans = 0; int main() { int n, i, j, k; scanf("%d", &n); for (i = 0; i < n; ++i) scanf("%d", &a[i]); for (i = n - 1; i >= 1; --i) if (n % i == 0) d.push_back(i); for (i = 0; i < d.size(); ++i) { memset(f, 0, sizeof(f)); for (j = 0; j < d[i]; ++j) { int maxx = 0; for (k = j; k < n; k += d[i]) maxx = max(maxx, a[k]); for (k = j; k < n; k += d[i]) if (a[k] == maxx) f[k] = 1; } b.clear(); for (j = d[i]; j < n; j += d[i]) if (!mark[j]) { mark[j] = 1; b.push_back(j); } for (j = n - 1; j >= 0; --j) { if (!f[j]) dp[j] = 0; else if (j == n - 1) dp[j] = 1; else dp[j] = 1 + dp[j + 1]; } for (j = 0; j < n; ++j) { int val = dp[j]; if (dp[j] == (n - j) && dp[0]) val += dp[0]; int pos = upper_bound(b.begin(), b.end(), min(n - 1, val)) - b.begin(); if (pos >= 0) ans += pos; } } printf("%lld\n", ans); } ```
#include <bits/stdc++.h> const int maxn = 200005; using namespace std; int rmax[maxn], l[maxn], a[maxn]; vector<int> co[maxn]; int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); } long long count(int g, int n) { fill(rmax, rmax + g, 0); for (int i = 0; i < n; i++) rmax[i % g] = max(rmax[i % g], a[i]); l[0] = 0; for (int t = 0; t < 2; t++) for (int i = n - 1; i >= 0; i--) l[i] = min(n - 1, (rmax[i % g] == a[i] ? l[(i + 1) % n] + 1 : 0)); long long ans = 0; for (int i = 0; i < n; i++) ans += upper_bound(co[g].begin(), co[g].end(), l[i] / g) - co[g].begin(); return ans; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) co[gcd(i, n)].push_back(i / gcd(i, n)); long long ans = 0; for (int i = 1; i < n; i++) if (n % i == 0) ans += count(i, n); cout << ans << endl; return 0; }
### Prompt Generate a cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> const int maxn = 200005; using namespace std; int rmax[maxn], l[maxn], a[maxn]; vector<int> co[maxn]; int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); } long long count(int g, int n) { fill(rmax, rmax + g, 0); for (int i = 0; i < n; i++) rmax[i % g] = max(rmax[i % g], a[i]); l[0] = 0; for (int t = 0; t < 2; t++) for (int i = n - 1; i >= 0; i--) l[i] = min(n - 1, (rmax[i % g] == a[i] ? l[(i + 1) % n] + 1 : 0)); long long ans = 0; for (int i = 0; i < n; i++) ans += upper_bound(co[g].begin(), co[g].end(), l[i] / g) - co[g].begin(); return ans; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) co[gcd(i, n)].push_back(i / gcd(i, n)); long long ans = 0; for (int i = 1; i < n; i++) if (n % i == 0) ans += count(i, n); cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> inline bool RD(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1, ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } template <class T> inline void PT(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) PT(x / 10); putchar(x % 10 + '0'); } const int N = 1e6 + 100; int a[N]; int c[N], g[N]; int GCD[N]; bool vis[N]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int n; RD(n); for (int i = 1; i <= int(n); i++) GCD[i] = gcd(i, n); for (int i = 0; i < int(n); i++) RD(a[i]); long long ans = 0; for (int len = 1; len <= int(n - 1); len++) { if (n % len) continue; for (int i = 0; i < int(n + 1); i++) vis[i] = c[i] = 0; for (int p = 0; p < int(len); p++) { int mx = 0; for (int i = p; i < n; i += len) mx = max(mx, a[i]); for (int i = p; i < n; i += len) vis[i] = a[i] == mx; } bool ok = 0; for (int lb = 0; lb < n;) { int ub = (lb + 1) % n; if (vis[lb]) { lb++; continue; } ok = 1; int cnt = 0; while (vis[ub]) ub = (ub + 1) % n, cnt++; for (int i = 1; i <= int(cnt); i++) c[i] += cnt - i + 1; if (ub <= lb) break; lb = ub; } if (ok == 0) for (int i = 1; i <= int(n); i++) c[i] = n; for (int i = 1; i <= n; i++) if (GCD[i] == len) ans += c[i]; } cout << ans << endl; }
### Prompt Please provide a cpp coded solution to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> inline bool RD(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1, ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } template <class T> inline void PT(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) PT(x / 10); putchar(x % 10 + '0'); } const int N = 1e6 + 100; int a[N]; int c[N], g[N]; int GCD[N]; bool vis[N]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int n; RD(n); for (int i = 1; i <= int(n); i++) GCD[i] = gcd(i, n); for (int i = 0; i < int(n); i++) RD(a[i]); long long ans = 0; for (int len = 1; len <= int(n - 1); len++) { if (n % len) continue; for (int i = 0; i < int(n + 1); i++) vis[i] = c[i] = 0; for (int p = 0; p < int(len); p++) { int mx = 0; for (int i = p; i < n; i += len) mx = max(mx, a[i]); for (int i = p; i < n; i += len) vis[i] = a[i] == mx; } bool ok = 0; for (int lb = 0; lb < n;) { int ub = (lb + 1) % n; if (vis[lb]) { lb++; continue; } ok = 1; int cnt = 0; while (vis[ub]) ub = (ub + 1) % n, cnt++; for (int i = 1; i <= int(cnt); i++) c[i] += cnt - i + 1; if (ub <= lb) break; lb = ub; } if (ok == 0) for (int i = 1; i <= int(n); i++) c[i] = n; for (int i = 1; i <= n; i++) if (GCD[i] == len) ans += c[i]; } cout << ans << endl; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 2 * 1e5 + 100; long long n, a[N], t[N], b[N], w, sum[N], ans; vector<long long> g[N]; long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } inline long long read() { long long f = 1, x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); } return x * f; } void solve(long long d) { memset(t, 0, (n + 10) * sizeof(long long)); for (long long i = 0; i < d; i++) { long long MAX = 0; for (long long j = i; j < n; j += d) MAX = max(MAX, a[j]); for (long long j = i; j < n; j += d) if (a[j] == MAX) t[j] = 1; } w = 0; for (long long i = 0; i < n;) { long long j = i; while (j < n && t[i] == t[j]) j++; if (t[i]) b[++w] = j - i; i = j; } if (b[1] == n) { for (long long i = 0; i < (long long)g[d].size(); i++) ans += n; return; } if (t[0] && t[n - 1]) b[1] += b[w--]; sort(b + 1, b + 1 + w); sum[w + 1] = 0; for (long long i = w; i >= 1; i--) sum[i] = sum[i + 1] + b[i]; for (long long i = 0, j = 1; i < (long long)g[d].size(); i++) { while (b[j] < g[d][i] && j <= w) j++; ans += sum[j] - (g[d][i] - 1) * (w - j + 1); } } signed main() { n = read(); for (long long i = 0; i < n; i++) a[i] = read(); for (long long i = 1; i < n; i++) g[gcd(i, n)].push_back(i); for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { solve(i); if (i * i != n) solve(n / i); } } printf("%lld\n", ans); }
### Prompt Please provide a CPP coded solution to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 2 * 1e5 + 100; long long n, a[N], t[N], b[N], w, sum[N], ans; vector<long long> g[N]; long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } inline long long read() { long long f = 1, x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); } return x * f; } void solve(long long d) { memset(t, 0, (n + 10) * sizeof(long long)); for (long long i = 0; i < d; i++) { long long MAX = 0; for (long long j = i; j < n; j += d) MAX = max(MAX, a[j]); for (long long j = i; j < n; j += d) if (a[j] == MAX) t[j] = 1; } w = 0; for (long long i = 0; i < n;) { long long j = i; while (j < n && t[i] == t[j]) j++; if (t[i]) b[++w] = j - i; i = j; } if (b[1] == n) { for (long long i = 0; i < (long long)g[d].size(); i++) ans += n; return; } if (t[0] && t[n - 1]) b[1] += b[w--]; sort(b + 1, b + 1 + w); sum[w + 1] = 0; for (long long i = w; i >= 1; i--) sum[i] = sum[i + 1] + b[i]; for (long long i = 0, j = 1; i < (long long)g[d].size(); i++) { while (b[j] < g[d][i] && j <= w) j++; ans += sum[j] - (g[d][i] - 1) * (w - j + 1); } } signed main() { n = read(); for (long long i = 0; i < n; i++) a[i] = read(); for (long long i = 1; i < n; i++) g[gcd(i, n)].push_back(i); for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { solve(i); if (i * i != n) solve(n / i); } } printf("%lld\n", ans); } ```
#include <bits/stdc++.h> using namespace std; map<int, vector<int> > m; int gcd(int a, int b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } int main() { int n; cin >> n; int a[n]; bool allSame = true; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] != a[0]) { allSame = false; } } if (allSame) { cout << ((long long)n * (n - 1)) << endl; return 0; } int largest[n]; for (int i = 1; 2 * i <= n; i++) { if (n % i) continue; m[i] = vector<int>(); memset(largest, 0, sizeof(largest)); for (int j = 0; j < n; j++) { largest[j % i] = max(largest[j % i], a[j]); } int ct = 0; for (int j = 0; j < n; j++) { if (a[j] < largest[j % i]) { if (ct > 0) { m[i].push_back(ct); ct = 0; } } else { ct++; } } if (ct > 0 && m[i].size() > 0 && a[0] >= largest[0]) { m[i][0] += ct; } else if (ct > 0) { m[i].push_back(ct); } sort(m[i].begin(), m[i].end()); reverse(m[i].begin(), m[i].end()); } long long ans = 0; for (int i = 1; i < n; i++) { int d = gcd(n, i); for (auto len : m[d]) { if (len < i) { break; } if (len == n) { ans += n; } else { ans += len - i + 1; } } } cout << ans << endl; return 0; }
### Prompt Please formulate a CPP solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; map<int, vector<int> > m; int gcd(int a, int b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } int main() { int n; cin >> n; int a[n]; bool allSame = true; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] != a[0]) { allSame = false; } } if (allSame) { cout << ((long long)n * (n - 1)) << endl; return 0; } int largest[n]; for (int i = 1; 2 * i <= n; i++) { if (n % i) continue; m[i] = vector<int>(); memset(largest, 0, sizeof(largest)); for (int j = 0; j < n; j++) { largest[j % i] = max(largest[j % i], a[j]); } int ct = 0; for (int j = 0; j < n; j++) { if (a[j] < largest[j % i]) { if (ct > 0) { m[i].push_back(ct); ct = 0; } } else { ct++; } } if (ct > 0 && m[i].size() > 0 && a[0] >= largest[0]) { m[i][0] += ct; } else if (ct > 0) { m[i].push_back(ct); } sort(m[i].begin(), m[i].end()); reverse(m[i].begin(), m[i].end()); } long long ans = 0; for (int i = 1; i < n; i++) { int d = gcd(n, i); for (auto len : m[d]) { if (len < i) { break; } if (len == n) { ans += n; } else { ans += len - i + 1; } } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long ans; int n, a[2 * 200010]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } bool ok[200010 * 2]; int flag[200010]; long long sum[200010]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int k = 1; k < n; k++) { if (n % k != 0) continue; for (int j = k; j <= n; j += k) if (gcd(j, n) == k) flag[j] = k; for (int j = 1; j <= n; j++) { if (flag[j] == k) sum[j] = sum[j - 1] + 1; else sum[j] = sum[j - 1]; } memset(ok, 0, sizeof(bool) * n * 2); for (int j = 0; j < k; j++) { int mx = a[j]; for (int t = j + k; t < n; t += k) mx = max(mx, a[t]); for (int t = j; t < n; t += k) { if (a[t] == mx) { ok[t + n] = ok[t] = true; } } } int pl = 0, pr = 0; while (pr <= 2 * n) { if (pl >= n) break; if (!ok[pr]) pl = pr = pr + 1; else { pr++; int len = pr - pl; if (pr > n && pl < n) ans = ans + sum[pr - pl] - sum[pr - n]; else ans = ans + sum[len]; if (len >= n) pl++; } } } cout << ans << endl; return 0; }
### Prompt Develop a solution in CPP to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long ans; int n, a[2 * 200010]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } bool ok[200010 * 2]; int flag[200010]; long long sum[200010]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int k = 1; k < n; k++) { if (n % k != 0) continue; for (int j = k; j <= n; j += k) if (gcd(j, n) == k) flag[j] = k; for (int j = 1; j <= n; j++) { if (flag[j] == k) sum[j] = sum[j - 1] + 1; else sum[j] = sum[j - 1]; } memset(ok, 0, sizeof(bool) * n * 2); for (int j = 0; j < k; j++) { int mx = a[j]; for (int t = j + k; t < n; t += k) mx = max(mx, a[t]); for (int t = j; t < n; t += k) { if (a[t] == mx) { ok[t + n] = ok[t] = true; } } } int pl = 0, pr = 0; while (pr <= 2 * n) { if (pl >= n) break; if (!ok[pr]) pl = pr = pr + 1; else { pr++; int len = pr - pl; if (pr > n && pl < n) ans = ans + sum[pr - pl] - sum[pr - n]; else ans = ans + sum[len]; if (len >= n) pl++; } } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,mmx,sse,sse2") using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; } template <typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline ostream& operator<<(ostream& os, const set<T>& v) { for (auto el : v) cout << el << " "; return os; } template <typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S>& v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline ostream& operator<<(ostream& os, const multiset<T>& v) { for (auto el : v) cout << el << " "; return os; } namespace ___debug { struct DebugStream { private: bool is_first; public: DebugStream(bool _is_first) : is_first(_is_first) {} template <typename T> DebugStream operator<<(const T& value) const { assert(0); if (is_first) cout << "[DBG] "; cout << value; return DebugStream(false); }; template <typename T> DebugStream printArray(T* l, T* r) { assert(0); if (is_first) cout << "[DBG] "; while (l != r) { cout << (*l); ++l; if (l == r) { cout << '\n'; } else { cout << ' '; } } return DebugStream(false); } }; DebugStream instance(true); }; // namespace ___debug const int INT_INF = (int)(2e9); const ll LL_INF = (ll)(2e18); const int NIL = -1; static mt19937 _g(time(nullptr)); inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); } inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }; template <typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); } template <typename T> inline T fetch() { T ret; cin >> ret; return ret; } template <typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; } int GCD(int x, int y) { return x ? GCD(y % x, x) : y; } const int MAXN = (int)2e5 + 77; int a[2 * MAXN], b[2 * MAXN], n; int pref[MAXN], mx[MAXN], gcdWithN[MAXN]; void solve() { cin >> n; for (int i = 0; i <= n; ++i) gcdWithN[i] = GCD(i, n); for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = n; i < 2 * n; ++i) a[i] = a[i - n]; ll answ = 0; for (int g = 1; g < n; ++g) if (n % g == 0) { 42; ; pref[0] = 0; for (int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + (gcdWithN[i] == g); for (int i = 0; i < g; ++i) mx[i] = 0; int ptr = 0; for (int i = 0; i < n; ++i) { mx[ptr] = max(mx[ptr], a[i]); ++ptr; if (ptr >= g) ptr -= g; } ptr = 0; for (int i = 0; i < 2 * n; ++i) { b[i] = mx[ptr]; ++ptr; if (ptr >= g) ptr -= g; } int sz = 0; for (int i = 0; i < n; ++i) { while (sz < n && b[i + sz] <= a[i + sz]) ++sz; if (sz) { answ += pref[sz]; --sz; } } } cout << answ << '\n'; } int main() { fast_io(); solve(); return 0; }
### Prompt Create a solution in CPP for the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,mmx,sse,sse2") using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; } template <typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline ostream& operator<<(ostream& os, const set<T>& v) { for (auto el : v) cout << el << " "; return os; } template <typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S>& v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline ostream& operator<<(ostream& os, const multiset<T>& v) { for (auto el : v) cout << el << " "; return os; } namespace ___debug { struct DebugStream { private: bool is_first; public: DebugStream(bool _is_first) : is_first(_is_first) {} template <typename T> DebugStream operator<<(const T& value) const { assert(0); if (is_first) cout << "[DBG] "; cout << value; return DebugStream(false); }; template <typename T> DebugStream printArray(T* l, T* r) { assert(0); if (is_first) cout << "[DBG] "; while (l != r) { cout << (*l); ++l; if (l == r) { cout << '\n'; } else { cout << ' '; } } return DebugStream(false); } }; DebugStream instance(true); }; // namespace ___debug const int INT_INF = (int)(2e9); const ll LL_INF = (ll)(2e18); const int NIL = -1; static mt19937 _g(time(nullptr)); inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); } inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }; template <typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); } template <typename T> inline T fetch() { T ret; cin >> ret; return ret; } template <typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; } int GCD(int x, int y) { return x ? GCD(y % x, x) : y; } const int MAXN = (int)2e5 + 77; int a[2 * MAXN], b[2 * MAXN], n; int pref[MAXN], mx[MAXN], gcdWithN[MAXN]; void solve() { cin >> n; for (int i = 0; i <= n; ++i) gcdWithN[i] = GCD(i, n); for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = n; i < 2 * n; ++i) a[i] = a[i - n]; ll answ = 0; for (int g = 1; g < n; ++g) if (n % g == 0) { 42; ; pref[0] = 0; for (int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + (gcdWithN[i] == g); for (int i = 0; i < g; ++i) mx[i] = 0; int ptr = 0; for (int i = 0; i < n; ++i) { mx[ptr] = max(mx[ptr], a[i]); ++ptr; if (ptr >= g) ptr -= g; } ptr = 0; for (int i = 0; i < 2 * n; ++i) { b[i] = mx[ptr]; ++ptr; if (ptr >= g) ptr -= g; } int sz = 0; for (int i = 0; i < n; ++i) { while (sz < n && b[i + sz] <= a[i + sz]) ++sz; if (sz) { answ += pref[sz]; --sz; } } } cout << answ << '\n'; } int main() { fast_io(); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; int A[202020]; int no[404040]; int fac[202020]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { cin.sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 0; i < n; ++i) { cin >> A[i]; } long long ret = 0; for (int s = 1; s < n; ++s) { if (n % s != 0) continue; fill(no, no + 404040, -1); fill(fac, fac + 202020, 0); int xd = n / s; for (int i = 2; i <= xd; ++i) { if (xd % i != 0) continue; for (int j = i; j <= xd; j += i) { fac[j] = 1; } } for (int i = 1; i <= xd; ++i) { fac[i] += fac[i - 1]; } for (int i = 0; i < s; ++i) { int m = -1; for (int j = i; j < n; j += s) { m = max(m, A[j]); } for (int j = i; j < n; j += s) { if (A[j] != m) { no[j] = 0; no[j + n] = 0; } } } for (int i = 404040 - 2; i >= 0; --i) { if (no[i] == -1) no[i] = no[i + 1] + 1; } int ns = n / s - 1; for (int i = 0; i < n; ++i) { int ASD = min(no[i] / s, ns); ret += ASD - fac[ASD]; } } cout << ret << '\n'; return 0; }
### Prompt In Cpp, your task is to solve the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; int A[202020]; int no[404040]; int fac[202020]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { cin.sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 0; i < n; ++i) { cin >> A[i]; } long long ret = 0; for (int s = 1; s < n; ++s) { if (n % s != 0) continue; fill(no, no + 404040, -1); fill(fac, fac + 202020, 0); int xd = n / s; for (int i = 2; i <= xd; ++i) { if (xd % i != 0) continue; for (int j = i; j <= xd; j += i) { fac[j] = 1; } } for (int i = 1; i <= xd; ++i) { fac[i] += fac[i - 1]; } for (int i = 0; i < s; ++i) { int m = -1; for (int j = i; j < n; j += s) { m = max(m, A[j]); } for (int j = i; j < n; j += s) { if (A[j] != m) { no[j] = 0; no[j + n] = 0; } } } for (int i = 404040 - 2; i >= 0; --i) { if (no[i] == -1) no[i] = no[i + 1] + 1; } int ns = n / s - 1; for (int i = 0; i < n; ++i) { int ASD = min(no[i] / s, ns); ret += ASD - fac[ASD]; } } cout << ret << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; pair<int, int> li[200050]; int v[200050], n, m, s[200050], cnt[200050]; bool b[200050]; long long ans; inline int Read() { int x = 0; char y; do y = getchar(); while (y < '0' || y > '9'); do x = x * 10 + y - '0', y = getchar(); while (y >= '0' && y <= '9'); return x; } int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } void Solve() { for (int i = 1; i < n;) { int q = li[i].first, tot = 0; memset(b, 0, sizeof(b)); memset(s, 0, sizeof(s)); memset(cnt, 0, sizeof(cnt)); for (int j = 1; j <= q; j++) { int ma = 0; for (int k = j; k <= n; k += q) ma = max(ma, v[k]); for (int k = j; k <= n; k += q) if (ma == v[k]) b[k] = true, tot++; } int now = 0, st = 0, it = 1; for (int j = 1; j <= n; j++) if (!b[j]) now = 0; else { st += now == 0; cnt[st]++; now++; } if (b[n] && b[1] && st != 1) cnt[1] += cnt[st--]; for (int j = 1; j <= st; j++) s[cnt[j]]++; while (i <= n && li[i].first == q) { while (it < li[i].second) tot -= s[it] * it, st -= s[it], it++; if (tot == n) ans += n; else ans += tot - li[i].second * st + st; i++; } } } int main() { n = Read(); for (int i = 1; i <= n; i++) v[i] = Read(); for (int i = 1; i < n; i++) li[i].first = gcd(i, n), li[i].second = i; sort(li + 1, li + n); Solve(); cout << ans << endl; return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; pair<int, int> li[200050]; int v[200050], n, m, s[200050], cnt[200050]; bool b[200050]; long long ans; inline int Read() { int x = 0; char y; do y = getchar(); while (y < '0' || y > '9'); do x = x * 10 + y - '0', y = getchar(); while (y >= '0' && y <= '9'); return x; } int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } void Solve() { for (int i = 1; i < n;) { int q = li[i].first, tot = 0; memset(b, 0, sizeof(b)); memset(s, 0, sizeof(s)); memset(cnt, 0, sizeof(cnt)); for (int j = 1; j <= q; j++) { int ma = 0; for (int k = j; k <= n; k += q) ma = max(ma, v[k]); for (int k = j; k <= n; k += q) if (ma == v[k]) b[k] = true, tot++; } int now = 0, st = 0, it = 1; for (int j = 1; j <= n; j++) if (!b[j]) now = 0; else { st += now == 0; cnt[st]++; now++; } if (b[n] && b[1] && st != 1) cnt[1] += cnt[st--]; for (int j = 1; j <= st; j++) s[cnt[j]]++; while (i <= n && li[i].first == q) { while (it < li[i].second) tot -= s[it] * it, st -= s[it], it++; if (tot == n) ans += n; else ans += tot - li[i].second * st + st; i++; } } } int main() { n = Read(); for (int i = 1; i <= n; i++) v[i] = Read(); for (int i = 1; i < n; i++) li[i].first = gcd(i, n), li[i].second = i; sort(li + 1, li + n); Solve(); cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> int n, a[400200], p[400200], m, fl[400200]; long long ans; int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", a + i); for (int i = 1; i * i <= n; i++) { if (n % i == 0) { p[m++] = i; if (i * i != n && i != 1) p[m++] = n / i; } } for (int i = 0; i < m; i++) { int d = p[i], r = n / p[i]; for (int j = 0; j < n; j++) fl[j] = 0; for (int j = 0; j < d; j++) { int mx = 0; for (int k = 0; k < r; k++) { int tp = k * d + j; if (mx < a[tp]) mx = a[tp]; } for (int k = 0; k < r; k++) { int tp = k * d + j; if (mx == a[tp]) fl[tp] = 1; } } int cnt = 0, tt; for (int j = 0; j < n; j++) { if (!fl[j]) { if (cnt >= d) { tt = cnt / d; for (int k = 1; k <= tt; k++) if (gcd(k, r) == 1) ans += cnt - d * k + 1; } cnt = 0; } else cnt++; } if (cnt) { int j; for (j = 0; j < n; j++) if (!fl[j]) break; tt = (cnt + j) / d; for (int k = 1; k <= tt && k < r; k++) if (gcd(k, r) == 1) { int tp = d * k; if (j + 1 >= tp) ans += cnt; else { ans += cnt + j - tp + 1; } } } } printf("%I64d\n", ans); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> int n, a[400200], p[400200], m, fl[400200]; long long ans; int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", a + i); for (int i = 1; i * i <= n; i++) { if (n % i == 0) { p[m++] = i; if (i * i != n && i != 1) p[m++] = n / i; } } for (int i = 0; i < m; i++) { int d = p[i], r = n / p[i]; for (int j = 0; j < n; j++) fl[j] = 0; for (int j = 0; j < d; j++) { int mx = 0; for (int k = 0; k < r; k++) { int tp = k * d + j; if (mx < a[tp]) mx = a[tp]; } for (int k = 0; k < r; k++) { int tp = k * d + j; if (mx == a[tp]) fl[tp] = 1; } } int cnt = 0, tt; for (int j = 0; j < n; j++) { if (!fl[j]) { if (cnt >= d) { tt = cnt / d; for (int k = 1; k <= tt; k++) if (gcd(k, r) == 1) ans += cnt - d * k + 1; } cnt = 0; } else cnt++; } if (cnt) { int j; for (j = 0; j < n; j++) if (!fl[j]) break; tt = (cnt + j) / d; for (int k = 1; k <= tt && k < r; k++) if (gcd(k, r) == 1) { int tp = d * k; if (j + 1 >= tp) ans += cnt; else { ans += cnt + j - tp + 1; } } } } printf("%I64d\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[200010], b[200010], c[200010], d[200010], ma[200010]; vector<int> e[200010]; int read() { char ch; for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-';) ch = getchar(); int d = 0, t = 1; if (ch == '-') { t = -1; ch = getchar(); } for (; ch >= '0' && ch <= '9'; ch = getchar()) d = d * 10 + ch - 48; return d * t; } int gcd(int x, int y) { while (y) { int r = x % y; x = y; y = r; } return x; } int main() { int n = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) e[gcd(i, n)].push_back(i); long long ans = 0; for (int i = 1; i < n; i++) if (n % i == 0) { for (int j = 0; j < i; j++) ma[j] = 0; for (int j = 1; j <= n; j++) ma[j % i] = max(ma[j % i], a[j]); int cnt = 0; for (int j = 1; j <= n; j++) if (ma[j % i] == a[j]) b[++cnt] = j; int m = 0; if (cnt == n) { for (vector<int>::iterator p = e[i].begin(); p != e[i].end(); p++) ans = ans + n; } else { int l = 0; for (int j = 1; j < cnt; j++) if (b[j] + 1 != b[j + 1]) d[++l] = j; if (b[1] != 1 || b[cnt] != n) d[++l] = cnt; m = l; for (int j = 1; j < m; j++) c[j] = d[j + 1] - d[j]; c[m] = d[1] + cnt - d[m]; sort(c + 1, c + m + 1); int k = 1; for (vector<int>::iterator p = e[i].begin(); p != e[i].end(); p++) { while (k <= m && (*p) > c[k]) k++; for (int j = k; j <= m; j++) ans = ans + c[j] - (*p) + 1; } } } cout << ans << endl; return 0; }
### Prompt Create a solution in cpp for the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[200010], b[200010], c[200010], d[200010], ma[200010]; vector<int> e[200010]; int read() { char ch; for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-';) ch = getchar(); int d = 0, t = 1; if (ch == '-') { t = -1; ch = getchar(); } for (; ch >= '0' && ch <= '9'; ch = getchar()) d = d * 10 + ch - 48; return d * t; } int gcd(int x, int y) { while (y) { int r = x % y; x = y; y = r; } return x; } int main() { int n = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) e[gcd(i, n)].push_back(i); long long ans = 0; for (int i = 1; i < n; i++) if (n % i == 0) { for (int j = 0; j < i; j++) ma[j] = 0; for (int j = 1; j <= n; j++) ma[j % i] = max(ma[j % i], a[j]); int cnt = 0; for (int j = 1; j <= n; j++) if (ma[j % i] == a[j]) b[++cnt] = j; int m = 0; if (cnt == n) { for (vector<int>::iterator p = e[i].begin(); p != e[i].end(); p++) ans = ans + n; } else { int l = 0; for (int j = 1; j < cnt; j++) if (b[j] + 1 != b[j + 1]) d[++l] = j; if (b[1] != 1 || b[cnt] != n) d[++l] = cnt; m = l; for (int j = 1; j < m; j++) c[j] = d[j + 1] - d[j]; c[m] = d[1] + cnt - d[m]; sort(c + 1, c + m + 1); int k = 1; for (vector<int>::iterator p = e[i].begin(); p != e[i].end(); p++) { while (k <= m && (*p) > c[k]) k++; for (int j = k; j <= m; j++) ans = ans + c[j] - (*p) + 1; } } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } int mxG[200][200005]; int main() { int n; scanf("%d", &n); int a[200005]; for (int i = 0; i < n; i++) scanf("%d", &a[i]); vector<int> gs; vector<int> gmap[200005]; for (int i = 1; i < n; i++) { int g = gcd(i, n); gmap[g].push_back(i); } for (int i = 1; i < n; i++) { if (gmap[i].size() > 0) { gs.push_back(i); } } for (int i = 0; i < n; i++) { for (int j = 0; j < gs.size(); j++) { int x = i % gs[j]; mxG[j][x] = max(mxG[j][x], a[i]); } } long long cnt = 0; vector<int> len(gs.size(), 0); for (int i = 0; i < n; i++) { for (int j = 0; j < gs.size(); j++) { if (a[i] == mxG[j][i % gs[j]]) { while (len[j] < n && a[(i + len[j]) % n] == mxG[j][((i + len[j]) % n) % gs[j]]) len[j]++; cnt += upper_bound(gmap[gs[j]].begin(), gmap[gs[j]].end(), len[j]) - gmap[gs[j]].begin(); len[j]--; } } } cout << cnt << endl; }
### Prompt Construct a cpp code solution to the problem outlined: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } int mxG[200][200005]; int main() { int n; scanf("%d", &n); int a[200005]; for (int i = 0; i < n; i++) scanf("%d", &a[i]); vector<int> gs; vector<int> gmap[200005]; for (int i = 1; i < n; i++) { int g = gcd(i, n); gmap[g].push_back(i); } for (int i = 1; i < n; i++) { if (gmap[i].size() > 0) { gs.push_back(i); } } for (int i = 0; i < n; i++) { for (int j = 0; j < gs.size(); j++) { int x = i % gs[j]; mxG[j][x] = max(mxG[j][x], a[i]); } } long long cnt = 0; vector<int> len(gs.size(), 0); for (int i = 0; i < n; i++) { for (int j = 0; j < gs.size(); j++) { if (a[i] == mxG[j][i % gs[j]]) { while (len[j] < n && a[(i + len[j]) % n] == mxG[j][((i + len[j]) % n) % gs[j]]) len[j]++; cnt += upper_bound(gmap[gs[j]].begin(), gmap[gs[j]].end(), len[j]) - gmap[gs[j]].begin(); len[j]--; } } } cout << cnt << endl; } ```
#include <bits/stdc++.h> using namespace std; const long long maxn = 200005; long long n; long long a[maxn]; long long Gcd[maxn]; long long c[maxn], ismax[maxn]; long long gcd(long long a, long long b) { if (!b) return a; else return gcd(b, a % b); } long long inc(long long v) { v++; return v % n; } int main() { while (scanf("%I64d", &n) == 1) { for (long long i = 0; i < n; i++) scanf("%I64d", &a[i]); for (long long i = 1; i <= n; i++) Gcd[i] = gcd(i, n); long long res = 0; for (long long GCD = 1; GCD < n; GCD++) { if (n % GCD != 0) continue; for (long long i = 0; i < n; i++) ismax[i] = 0, c[i] = 0; for (long long i = 0; i < GCD; i++) { long long Max = -1; for (long long j = i; j < n; j += GCD) Max = max(Max, a[j]); for (long long j = i; j < n; j += GCD) if (a[j] == Max) ismax[j] = 1; } bool any = false; for (long long l = 0; l < n;) { long long r = inc(l); if (ismax[l]) { l++; continue; } any = true; long long len = 0; while (ismax[r]) { r = inc(r); len++; } for (long long i = 1; i <= len; i++) c[i] += len - i + 1; if (r <= l) break; l = r; } if (!any) for (long long i = 1; i <= n; i++) c[i] += n; for (long long i = 1; i <= n; i++) if (Gcd[i] == GCD) res += c[i]; } printf("%I64d\n", res); } return 0; }
### Prompt Generate a cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long maxn = 200005; long long n; long long a[maxn]; long long Gcd[maxn]; long long c[maxn], ismax[maxn]; long long gcd(long long a, long long b) { if (!b) return a; else return gcd(b, a % b); } long long inc(long long v) { v++; return v % n; } int main() { while (scanf("%I64d", &n) == 1) { for (long long i = 0; i < n; i++) scanf("%I64d", &a[i]); for (long long i = 1; i <= n; i++) Gcd[i] = gcd(i, n); long long res = 0; for (long long GCD = 1; GCD < n; GCD++) { if (n % GCD != 0) continue; for (long long i = 0; i < n; i++) ismax[i] = 0, c[i] = 0; for (long long i = 0; i < GCD; i++) { long long Max = -1; for (long long j = i; j < n; j += GCD) Max = max(Max, a[j]); for (long long j = i; j < n; j += GCD) if (a[j] == Max) ismax[j] = 1; } bool any = false; for (long long l = 0; l < n;) { long long r = inc(l); if (ismax[l]) { l++; continue; } any = true; long long len = 0; while (ismax[r]) { r = inc(r); len++; } for (long long i = 1; i <= len; i++) c[i] += len - i + 1; if (r <= l) break; l = r; } if (!any) for (long long i = 1; i <= n; i++) c[i] += n; for (long long i = 1; i <= n; i++) if (Gcd[i] == GCD) res += c[i]; } printf("%I64d\n", res); } return 0; } ```
#include <bits/stdc++.h> const int MAXN = 400005; int n, a[MAXN], maxv[MAXN], res[MAXN], len[MAXN], G[MAXN]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } inline int min(int a, int b) { return a > b ? b : a; } long long int solve(int g) { int i; long long int ret = 0LL; memset(maxv, -1, sizeof(maxv)); for (i = 1; i <= n; ++i) if (a[i] > maxv[i % g]) maxv[i % g] = a[i]; for (i = (n << 1); i; --i) if (a[i] == maxv[i % g]) len[i] = len[i + 1] + 1; else len[i] = 0; for (i = 1; i <= n; ++i) res[i] = res[i - 1] + int(G[i] == g); for (i = 1; i <= n; ++i) ret += res[min(n - 1, len[i])]; return ret; } int main() { int i; long long int ans = 0LL; scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%d", &a[i]); a[n + i] = a[i]; G[i] = gcd(i, n); } for (i = 2; i <= n / i; ++i) if (n % i == 0) { ans += solve(i); if (i != n / i) ans += solve(n / i); } printf("%I64d\n", ans + solve(1)); return 0; }
### Prompt Develop a solution in CPP to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> const int MAXN = 400005; int n, a[MAXN], maxv[MAXN], res[MAXN], len[MAXN], G[MAXN]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } inline int min(int a, int b) { return a > b ? b : a; } long long int solve(int g) { int i; long long int ret = 0LL; memset(maxv, -1, sizeof(maxv)); for (i = 1; i <= n; ++i) if (a[i] > maxv[i % g]) maxv[i % g] = a[i]; for (i = (n << 1); i; --i) if (a[i] == maxv[i % g]) len[i] = len[i + 1] + 1; else len[i] = 0; for (i = 1; i <= n; ++i) res[i] = res[i - 1] + int(G[i] == g); for (i = 1; i <= n; ++i) ret += res[min(n - 1, len[i])]; return ret; } int main() { int i; long long int ans = 0LL; scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%d", &a[i]); a[n + i] = a[i]; G[i] = gcd(i, n); } for (i = 2; i <= n / i; ++i) if (n % i == 0) { ans += solve(i); if (i != n / i) ans += solve(n / i); } printf("%I64d\n", ans + solve(1)); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1234567; int a[N], gcd[N], is_max[N], cnt[N]; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; a[i + n] = a[i]; } for (int i = 1; i <= n; ++i) { if (n % i == 0) { for (int j = i; j <= n; j += i) { gcd[j] = i; } } } long long ans = 0ll; for (int i = 1; i <= n; ++i) { if (n % i == 0) { for (int j = 0; j < i; ++j) { int m = 0; for (int q = j; q < n; q += i) { m = max(m, a[q]); } for (int q = j; q < n * 2; q += i) { is_max[q] = a[q] == m; } } for (int j = 1; j < n * 2; ++j) { if (is_max[j]) { is_max[j] += is_max[j - 1]; } } cnt[0] = 0; for (int j = 1; j < n; ++j) { cnt[j] = cnt[j - 1] + (gcd[j] == i); } for (int j = n; j < n * 2; ++j) { ans += cnt[min(is_max[j], n - 1)]; } } } cout << ans << endl; return 0; }
### Prompt Please create a solution in Cpp to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1234567; int a[N], gcd[N], is_max[N], cnt[N]; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; a[i + n] = a[i]; } for (int i = 1; i <= n; ++i) { if (n % i == 0) { for (int j = i; j <= n; j += i) { gcd[j] = i; } } } long long ans = 0ll; for (int i = 1; i <= n; ++i) { if (n % i == 0) { for (int j = 0; j < i; ++j) { int m = 0; for (int q = j; q < n; q += i) { m = max(m, a[q]); } for (int q = j; q < n * 2; q += i) { is_max[q] = a[q] == m; } } for (int j = 1; j < n * 2; ++j) { if (is_max[j]) { is_max[j] += is_max[j - 1]; } } cnt[0] = 0; for (int j = 1; j < n; ++j) { cnt[j] = cnt[j - 1] + (gcd[j] == i); } for (int j = n; j < n * 2; ++j) { ans += cnt[min(is_max[j], n - 1)]; } } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[200005 << 1], res[200005]; vector<int> E[200005]; int gcd(int x, int y) { if (x == 0) return y; return gcd(y % x, x); } int main() { int n, i, k, j; long long ans = 0; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]), a[i + n] = a[i]; for (i = 1; i < n; i++) E[gcd(i, n)].push_back(i); for (i = 1; i < n; i++) { if (!E[i].size()) continue; memset(res, 0, sizeof(res)); for (k = 1; k <= n * 2; k++) res[k % i] = max(res[k % i], a[k]); int t = 0, cnt = 0; for (k = 1; k <= n * 2; k++) { if (a[k] >= res[k % i]) { cnt++; if (t < E[i].size() && cnt >= E[i][t]) t++; if (k > n) ans += t; } else t = 0, cnt = 0; } } cout << ans << endl; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[200005 << 1], res[200005]; vector<int> E[200005]; int gcd(int x, int y) { if (x == 0) return y; return gcd(y % x, x); } int main() { int n, i, k, j; long long ans = 0; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]), a[i + n] = a[i]; for (i = 1; i < n; i++) E[gcd(i, n)].push_back(i); for (i = 1; i < n; i++) { if (!E[i].size()) continue; memset(res, 0, sizeof(res)); for (k = 1; k <= n * 2; k++) res[k % i] = max(res[k % i], a[k]); int t = 0, cnt = 0; for (k = 1; k <= n * 2; k++) { if (a[k] >= res[k % i]) { cnt++; if (t < E[i].size() && cnt >= E[i][t]) t++; if (k > n) ans += t; } else t = 0, cnt = 0; } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> const int INF = 1e9; const long long lINF = 1e18; const double EPS = 1e-12; using namespace std; const int N = 2e5 + 100; int n; int a[N]; long long ans[N]; int g[N]; int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { g[i] = gcd(i, n); } for (int i = 0; i < n; i++) { scanf("%d", a + i); } vector<int> dels; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { dels.push_back(i); if (n != i * i) { dels.push_back(n / i); } } } sort((dels).begin(), (dels).end()); for (auto cur : dels) { vector<int> mx(cur, 0); for (int j = 0; j < cur; j++) { for (int i = j; i < n; i += cur) { mx[j] = max(mx[j], a[i]); } } vector<int> v; int r = 0; for (int i = 0; i < n; i++) { if (a[i] == mx[i % cur]) { r++; } else { if (r > 0) { v.push_back(r); r = 0; } } } if (r > 0) { if (((int)((v).size())) >= 1 && a[0] == mx[0]) { v[0] += r; } else { v.push_back(r); } } if (v[0] != n) { for (auto i : v) { for (int j = cur; j <= i; j += cur) { if (g[j] == cur) { ans[j] += i - j + 1; } } } } else { for (int j = cur; j <= v[0]; j += cur) { if (g[j] == cur) { ans[j] += v[0]; } } } } long long res = 0; for (int i = 0; i < n; i++) { res += ans[i]; } printf( "%lld" "\n", res); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> const int INF = 1e9; const long long lINF = 1e18; const double EPS = 1e-12; using namespace std; const int N = 2e5 + 100; int n; int a[N]; long long ans[N]; int g[N]; int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { g[i] = gcd(i, n); } for (int i = 0; i < n; i++) { scanf("%d", a + i); } vector<int> dels; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { dels.push_back(i); if (n != i * i) { dels.push_back(n / i); } } } sort((dels).begin(), (dels).end()); for (auto cur : dels) { vector<int> mx(cur, 0); for (int j = 0; j < cur; j++) { for (int i = j; i < n; i += cur) { mx[j] = max(mx[j], a[i]); } } vector<int> v; int r = 0; for (int i = 0; i < n; i++) { if (a[i] == mx[i % cur]) { r++; } else { if (r > 0) { v.push_back(r); r = 0; } } } if (r > 0) { if (((int)((v).size())) >= 1 && a[0] == mx[0]) { v[0] += r; } else { v.push_back(r); } } if (v[0] != n) { for (auto i : v) { for (int j = cur; j <= i; j += cur) { if (g[j] == cur) { ans[j] += i - j + 1; } } } } else { for (int j = cur; j <= v[0]; j += cur) { if (g[j] == cur) { ans[j] += v[0]; } } } } long long res = 0; for (int i = 0; i < n; i++) { res += ans[i]; } printf( "%lld" "\n", res); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int M = 400005; int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } int n; bool mark[M]; int c[M]; int A[M], dp[M], cnt[M]; int Gcd[M]; int main() { cin >> n; for (int i = 0; i <= n; ++i) Gcd[i] = gcd(i, n); for (int i = 0; i < n; ++i) cin >> A[i]; for (int i = n; i < n + n; ++i) A[i] = A[i - n]; long long ans = 0; for (int d = 1; d <= n; ++d) { if (n % d) continue; memset(mark, 0, sizeof(mark)); for (int i = 0; i < d; ++i) { int mx = 0; for (int j = i; j < n + n; j += d) if (A[j] > mx) mx = A[j]; for (int j = i; j < n + n; j += d) if (A[j] == mx) mark[j] = true; } dp[0] = mark[0]; for (int i = 1; i < n + n; ++i) { if (mark[i]) dp[i] = dp[i - 1] + 1; else dp[i] = 0; dp[i] = min(dp[i], n - 1); } cnt[0] = 0; for (int i = 1; i < n; ++i) cnt[i] = cnt[i - 1] + (Gcd[i] == d); for (int i = n; i < n + n; ++i) ans += cnt[dp[i]]; } cout << ans << endl; }
### Prompt Please formulate a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int M = 400005; int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } int n; bool mark[M]; int c[M]; int A[M], dp[M], cnt[M]; int Gcd[M]; int main() { cin >> n; for (int i = 0; i <= n; ++i) Gcd[i] = gcd(i, n); for (int i = 0; i < n; ++i) cin >> A[i]; for (int i = n; i < n + n; ++i) A[i] = A[i - n]; long long ans = 0; for (int d = 1; d <= n; ++d) { if (n % d) continue; memset(mark, 0, sizeof(mark)); for (int i = 0; i < d; ++i) { int mx = 0; for (int j = i; j < n + n; j += d) if (A[j] > mx) mx = A[j]; for (int j = i; j < n + n; j += d) if (A[j] == mx) mark[j] = true; } dp[0] = mark[0]; for (int i = 1; i < n + n; ++i) { if (mark[i]) dp[i] = dp[i - 1] + 1; else dp[i] = 0; dp[i] = min(dp[i], n - 1); } cnt[0] = 0; for (int i = 1; i < n; ++i) cnt[i] = cnt[i - 1] + (Gcd[i] == d); for (int i = n; i < n + n; ++i) ans += cnt[dp[i]]; } cout << ans << endl; } ```
#include <bits/stdc++.h> using namespace std; void solve(int test_number); int main() { cout.setf(ios::fixed); cout.precision(9); cerr.setf(ios::fixed); cerr.precision(3); int n = 1; for (int i = 0; i < n; i++) { solve(i + 1); } return 0; } const int MAXN = 200100; const int BUBEN = 800; int n; int a[MAXN]; int d[MAXN]; int nd; bool used[BUBEN][MAXN]; int sum[BUBEN]; vector<int> q[MAXN]; int st[MAXN]; inline int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } void solve(int test_number) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { d[nd++] = i; int j = n / i; if (i != j && j != n) d[nd++] = j; } } sort(d, d + nd); for (int j = 0; j < nd; j++) { int step = d[j]; for (int from = 0; from < step; from++) { int mx = a[from]; for (int i = from; i < n; i += step) { mx = max(mx, a[i]); } for (int i = from; i < n; i += step) { if (mx == a[i]) { used[j][i] = true; } } } int last = -1; int pref = 0; for (int i = 0; i < n; i++) if (used[j][i]) { if (last == -1) last = i; } else if (last != -1) { if (last == 0) { pref = i - last; } else { q[j].push_back(i - last); sum[j] += i - last; } last = -1; } if (last != -1) { pref += n - last; } q[j].push_back(pref); sum[j] += pref; sort(q[j].begin(), q[j].end()); } long long res = 0; for (int len = 1; len < n; len++) { int step = gcd(len, n); long long j = lower_bound(d, d + nd, step) - d; while ((int)q[j].size() != st[j] && q[j][st[j]] < len) { sum[j] -= q[j][st[j]]; st[j]++; } if (sum[j] == n) { res += (long long)n; } else { res += (long long)sum[j] - (long long)(len - 1) * (long long)(q[j].size() - st[j]); } } cout << res << endl; }
### Prompt Your task is to create a CPP solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve(int test_number); int main() { cout.setf(ios::fixed); cout.precision(9); cerr.setf(ios::fixed); cerr.precision(3); int n = 1; for (int i = 0; i < n; i++) { solve(i + 1); } return 0; } const int MAXN = 200100; const int BUBEN = 800; int n; int a[MAXN]; int d[MAXN]; int nd; bool used[BUBEN][MAXN]; int sum[BUBEN]; vector<int> q[MAXN]; int st[MAXN]; inline int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } void solve(int test_number) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { d[nd++] = i; int j = n / i; if (i != j && j != n) d[nd++] = j; } } sort(d, d + nd); for (int j = 0; j < nd; j++) { int step = d[j]; for (int from = 0; from < step; from++) { int mx = a[from]; for (int i = from; i < n; i += step) { mx = max(mx, a[i]); } for (int i = from; i < n; i += step) { if (mx == a[i]) { used[j][i] = true; } } } int last = -1; int pref = 0; for (int i = 0; i < n; i++) if (used[j][i]) { if (last == -1) last = i; } else if (last != -1) { if (last == 0) { pref = i - last; } else { q[j].push_back(i - last); sum[j] += i - last; } last = -1; } if (last != -1) { pref += n - last; } q[j].push_back(pref); sum[j] += pref; sort(q[j].begin(), q[j].end()); } long long res = 0; for (int len = 1; len < n; len++) { int step = gcd(len, n); long long j = lower_bound(d, d + nd, step) - d; while ((int)q[j].size() != st[j] && q[j][st[j]] < len) { sum[j] -= q[j][st[j]]; st[j]++; } if (sum[j] == n) { res += (long long)n; } else { res += (long long)sum[j] - (long long)(len - 1) * (long long)(q[j].size() - st[j]); } } cout << res << endl; } ```
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { while (a && b) a > b ? a %= b : b %= a; return a + b; } vector<int> divs(int x) { vector<int> res; for (int i = 1; i <= x; i++) { if (x % i == 0) res.push_back(i); } return res; } int n, ar[1 << 20]; long long ans; int mx[1 << 20]; int total[1 << 20], f[1 << 20]; long long solve(int G) { for (int i = 0; i < G; i++) mx[i] = -1; for (int i = 0; i < n; i++) mx[i % G] = max(mx[i % G], ar[i]); for (int i = 1; i <= n * 2; i++) { total[i] = total[i - 1]; if (f[i] == G) ++total[i]; } long long res = 0; int ptr = -1; for (int i = 0; i < n * 2; i++) { int need = mx[i % G]; if (ar[i] != need) { ptr = i; continue; } if (i < n) continue; int span = i - ptr; res += total[span]; } return res; } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) cin >> ar[i]; for (int i = n; i < n * 2; i++) ar[i] = ar[i - n]; for (int i = 1; i <= n; i++) f[i] = gcd(i, n); vector<int> vals = divs(n); for (int i = 0; i < vals.size(); i++) { ans += solve(vals[i]); } cout << ans - n << endl; return 0; }
### Prompt Please create a solution in Cpp to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { while (a && b) a > b ? a %= b : b %= a; return a + b; } vector<int> divs(int x) { vector<int> res; for (int i = 1; i <= x; i++) { if (x % i == 0) res.push_back(i); } return res; } int n, ar[1 << 20]; long long ans; int mx[1 << 20]; int total[1 << 20], f[1 << 20]; long long solve(int G) { for (int i = 0; i < G; i++) mx[i] = -1; for (int i = 0; i < n; i++) mx[i % G] = max(mx[i % G], ar[i]); for (int i = 1; i <= n * 2; i++) { total[i] = total[i - 1]; if (f[i] == G) ++total[i]; } long long res = 0; int ptr = -1; for (int i = 0; i < n * 2; i++) { int need = mx[i % G]; if (ar[i] != need) { ptr = i; continue; } if (i < n) continue; int span = i - ptr; res += total[span]; } return res; } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) cin >> ar[i]; for (int i = n; i < n * 2; i++) ar[i] = ar[i - n]; for (int i = 1; i <= n; i++) f[i] = gcd(i, n); vector<int> vals = divs(n); for (int i = 0; i < vals.size(); i++) { ans += solve(vals[i]); } cout << ans - n << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; int a[400000]; int gg[400001]; int inc(int v) { if (v + 1 == n) return 0; return v + 1; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } bool u[400001]; int c[400001]; int main(void) { scanf("%d", &n); for (int i = 1; i <= n; i++) { gg[i] = gcd(n, i); } for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } long long res = 0; for (int g = 1; g < n; g++) { if (n % g != 0) continue; memset(u, false, sizeof(u)); memset(c, 0, sizeof(c)); for (int i = 0; i < g; i++) { int mx = -1; for (int j = i; j < n; j += g) { mx = max(mx, a[j]); } for (int j = i; j < n; j += g) { if (a[j] == mx) u[j] = true; } } bool flag = false; for (int l = 0; l < n;) { int r = inc(l); if (u[l]) { l++; continue; } flag = true; int len = 0; while (u[r]) len++, r = inc(r); for (int i = 1; i <= len; i++) { c[i] += len - i + 1; } if (r <= l) break; l = r; } if (!flag) { for (int i = 1; i <= n; i++) { c[i] += n; } } for (int i = 1; i <= n; i++) { if (gg[i] == g) res += c[i]; } } cout << res << endl; return 0; }
### Prompt Your task is to create a cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; int a[400000]; int gg[400001]; int inc(int v) { if (v + 1 == n) return 0; return v + 1; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } bool u[400001]; int c[400001]; int main(void) { scanf("%d", &n); for (int i = 1; i <= n; i++) { gg[i] = gcd(n, i); } for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } long long res = 0; for (int g = 1; g < n; g++) { if (n % g != 0) continue; memset(u, false, sizeof(u)); memset(c, 0, sizeof(c)); for (int i = 0; i < g; i++) { int mx = -1; for (int j = i; j < n; j += g) { mx = max(mx, a[j]); } for (int j = i; j < n; j += g) { if (a[j] == mx) u[j] = true; } } bool flag = false; for (int l = 0; l < n;) { int r = inc(l); if (u[l]) { l++; continue; } flag = true; int len = 0; while (u[r]) len++, r = inc(r); for (int i = 1; i <= len; i++) { c[i] += len - i + 1; } if (r <= l) break; l = r; } if (!flag) { for (int i = 1; i <= n; i++) { c[i] += n; } } for (int i = 1; i <= n; i++) { if (gg[i] == g) res += c[i]; } } cout << res << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, i, j, k, a[500010], vis[500010], flag[500010], t, tmp, len, typ, g[500010]; long long ans; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 1; i <= n; i++) g[i] = gcd(i, n); for (i = 1; i < n; i++) if (n % i == 0) { for (j = 0; j < 2 * n; j++) { vis[j] = 0; flag[j] = 0; } for (j = 0; j < n; j++) if (vis[j] == 0) { t = j; tmp = 0; while (vis[t] == 0) { vis[t] = 1; if (a[t] > tmp) tmp = a[t]; t = (t + i) % n; } t = j; while (vis[t] != 2) { vis[t] = 2; if (a[t] == tmp) { flag[t] = 1; flag[n + t] = 1; } t = (t + i) % n; } } for (j = 0; j < 2 * n; j++) if (flag[j] == 0) break; if (j != 2 * n) { len = 0; typ = 0; for (k = j; k < j + n; k++) { if (flag[k]) { len++; if (g[len] == i) typ++; } else { typ = 0; len = 0; } ans = ans + typ; } } else { for (k = 1; k <= n; k++) if (g[k] == i) ans = ans + n; } } printf("%I64d\n", ans); }
### Prompt In CPP, your task is to solve the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, i, j, k, a[500010], vis[500010], flag[500010], t, tmp, len, typ, g[500010]; long long ans; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 1; i <= n; i++) g[i] = gcd(i, n); for (i = 1; i < n; i++) if (n % i == 0) { for (j = 0; j < 2 * n; j++) { vis[j] = 0; flag[j] = 0; } for (j = 0; j < n; j++) if (vis[j] == 0) { t = j; tmp = 0; while (vis[t] == 0) { vis[t] = 1; if (a[t] > tmp) tmp = a[t]; t = (t + i) % n; } t = j; while (vis[t] != 2) { vis[t] = 2; if (a[t] == tmp) { flag[t] = 1; flag[n + t] = 1; } t = (t + i) % n; } } for (j = 0; j < 2 * n; j++) if (flag[j] == 0) break; if (j != 2 * n) { len = 0; typ = 0; for (k = j; k < j + n; k++) { if (flag[k]) { len++; if (g[len] == i) typ++; } else { typ = 0; len = 0; } ans = ans + typ; } } else { for (k = 1; k <= n; k++) if (g[k] == i) ans = ans + n; } } printf("%I64d\n", ans); } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; const int MM = 1e9 + 7; int gcd(int p, int q) { return q ? gcd(q, p % q) : p; } int a[MAXN]; int vis[MAXN]; int fac[MAXN]; long long ans[200010]; int f[MAXN]; vector<int> E[2000]; int main() { int n; cin >> n; int mx = 0; int cnt = 0; for (int i = 0; i < n; i++) scanf("%d", &a[i]), mx = max(mx, a[i]); for (int i = 0; i < n; i++) cnt += (a[i] == a[mx]); for (int i = 1; i < n; i++) fac[i] = gcd(n, i); sort(fac + 1, fac + n); int m = unique(fac + 1, fac + n) - fac - 1; map<int, int> mp; for (int k = 1; k <= m; k++) mp[fac[k]] = k; for (int i = 1; i < n; i++) { int tmp = gcd(n, i); int k = mp[tmp]; E[k].push_back(i); } long long Umi = 0; for (int k = 1; k <= m; k++) { int p = fac[k]; for (int i = 0; i < n; i++) { vis[i] = 0; } for (int i = 0; i < p; i++) { int tt = 0; for (int j = i; j < n; j += p) tt = max(tt, a[j]); for (int j = i; j < n; j += p) if (tt == a[j]) vis[j] = 1; } int tot = 0; for (int i = 0; i < n; i++) tot += vis[i]; if (tot == n) { for (int i = 1; i <= n; i++) ans[i] = n; } else { for (int i = 1; i <= n; i++) f[i] = 0; int o = 0; for (; vis[o]; o++) ; int now = 0; for (int i = 0; i <= n; i++) { if (!vis[(i + o) % n]) { if (now != 0) f[now]++; now = 0; } else now++; } int P = 0, Q = 0; for (int i = n; i >= 1; i--) { P += (long long)i * f[i]; Q += f[i]; ans[i] = P - (i - 1) * Q; } } for (int i = 0; i < E[k].size(); i++) Umi += ans[E[k][i]]; } cout << Umi << endl; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; const int MM = 1e9 + 7; int gcd(int p, int q) { return q ? gcd(q, p % q) : p; } int a[MAXN]; int vis[MAXN]; int fac[MAXN]; long long ans[200010]; int f[MAXN]; vector<int> E[2000]; int main() { int n; cin >> n; int mx = 0; int cnt = 0; for (int i = 0; i < n; i++) scanf("%d", &a[i]), mx = max(mx, a[i]); for (int i = 0; i < n; i++) cnt += (a[i] == a[mx]); for (int i = 1; i < n; i++) fac[i] = gcd(n, i); sort(fac + 1, fac + n); int m = unique(fac + 1, fac + n) - fac - 1; map<int, int> mp; for (int k = 1; k <= m; k++) mp[fac[k]] = k; for (int i = 1; i < n; i++) { int tmp = gcd(n, i); int k = mp[tmp]; E[k].push_back(i); } long long Umi = 0; for (int k = 1; k <= m; k++) { int p = fac[k]; for (int i = 0; i < n; i++) { vis[i] = 0; } for (int i = 0; i < p; i++) { int tt = 0; for (int j = i; j < n; j += p) tt = max(tt, a[j]); for (int j = i; j < n; j += p) if (tt == a[j]) vis[j] = 1; } int tot = 0; for (int i = 0; i < n; i++) tot += vis[i]; if (tot == n) { for (int i = 1; i <= n; i++) ans[i] = n; } else { for (int i = 1; i <= n; i++) f[i] = 0; int o = 0; for (; vis[o]; o++) ; int now = 0; for (int i = 0; i <= n; i++) { if (!vis[(i + o) % n]) { if (now != 0) f[now]++; now = 0; } else now++; } int P = 0, Q = 0; for (int i = n; i >= 1; i--) { P += (long long)i * f[i]; Q += f[i]; ans[i] = P - (i - 1) * Q; } } for (int i = 0; i < E[k].size(); i++) Umi += ans[E[k][i]]; } cout << Umi << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 200015; int a[N], g[N]; bool good[N]; vector<int> divp; long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); } int main() { int n; while (~scanf("%d", &n)) { divp.clear(); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } for (int i = 1; i < n; ++i) if (n % i == 0) { divp.push_back(i); } g[0] = 0; for (int i = 1; i <= n; ++i) { g[i] = gcd(i, n); } long long ans = 0; for (int i = 0; i < divp.size(); ++i) { int s = divp[i]; for (int j = 0; j < n; ++j) good[j] = 0; for (int j = 0; j < s; ++j) { int maxv = 0; for (int p = j; p < n; p += s) { maxv = max(a[p], maxv); } for (int p = j; p < n; p += s) { if (a[p] == maxv) { good[p] = true; } } } int nn = 2 * n, L = 0, accm = 0; bool beg = good[n - 1] ? true : false; for (int p = 0; p < nn; ++p) { int j = p >= n ? p - n : p; if (good[j]) { L++; if (g[L] == s) ++accm; } else { L = 0; accm = 0; beg = false; if (p >= n - 1) break; } if (L >= n) break; if (!beg) ans += accm; } if (beg) { ans += 1LL * accm * n; } } cout << ans << endl; } return 0; }
### Prompt Please create a solution in cpp to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 200015; int a[N], g[N]; bool good[N]; vector<int> divp; long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); } int main() { int n; while (~scanf("%d", &n)) { divp.clear(); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } for (int i = 1; i < n; ++i) if (n % i == 0) { divp.push_back(i); } g[0] = 0; for (int i = 1; i <= n; ++i) { g[i] = gcd(i, n); } long long ans = 0; for (int i = 0; i < divp.size(); ++i) { int s = divp[i]; for (int j = 0; j < n; ++j) good[j] = 0; for (int j = 0; j < s; ++j) { int maxv = 0; for (int p = j; p < n; p += s) { maxv = max(a[p], maxv); } for (int p = j; p < n; p += s) { if (a[p] == maxv) { good[p] = true; } } } int nn = 2 * n, L = 0, accm = 0; bool beg = good[n - 1] ? true : false; for (int p = 0; p < nn; ++p) { int j = p >= n ? p - n : p; if (good[j]) { L++; if (g[L] == s) ++accm; } else { L = 0; accm = 0; beg = false; if (p >= n - 1) break; } if (L >= n) break; if (!beg) ans += accm; } if (beg) { ans += 1LL * accm * n; } } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline int gcd(int a, int b) { while (b > 0) { int t = a; a = b; b = t % b; } return a; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; { int i; for (i = 1; i < n; ++i) if (a[i] != a[i - 1]) break; if (i == n) { cout << (int64_t)n * (n - 1) << endl; return 0; } } int64_t ans = 0; for (int divisor = 1; divisor < n; ++divisor) { if (n % divisor > 0) continue; vector<int> max_of_rem(a.begin(), a.begin() + divisor); for (int i = divisor; i < n; ++i) if (a[i] > max_of_rem[i % divisor]) max_of_rem[i % divisor] = a[i]; int lim = n; for (int left = 0; left < lim;) { int offs; for (offs = 0; offs < n - 1 && a[(left + offs) % n] == max_of_rem[(left + offs) % divisor]; ++offs) ; if (offs == 0) { ++left; continue; } int goffs = offs; if (left == 0) { while (left + offs < lim && a[lim - 1] == max_of_rem[(lim - 1) % divisor]) --lim; offs += n - lim; } for (int i = divisor; i <= offs; i += divisor) { if (gcd(n, i) == divisor) { ans += offs == n ? n : offs - i + 1; } } left += goffs; } } cout << ans << endl; }
### Prompt Develop a solution in CPP to the problem described below: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int gcd(int a, int b) { while (b > 0) { int t = a; a = b; b = t % b; } return a; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; { int i; for (i = 1; i < n; ++i) if (a[i] != a[i - 1]) break; if (i == n) { cout << (int64_t)n * (n - 1) << endl; return 0; } } int64_t ans = 0; for (int divisor = 1; divisor < n; ++divisor) { if (n % divisor > 0) continue; vector<int> max_of_rem(a.begin(), a.begin() + divisor); for (int i = divisor; i < n; ++i) if (a[i] > max_of_rem[i % divisor]) max_of_rem[i % divisor] = a[i]; int lim = n; for (int left = 0; left < lim;) { int offs; for (offs = 0; offs < n - 1 && a[(left + offs) % n] == max_of_rem[(left + offs) % divisor]; ++offs) ; if (offs == 0) { ++left; continue; } int goffs = offs; if (left == 0) { while (left + offs < lim && a[lim - 1] == max_of_rem[(lim - 1) % divisor]) --lim; offs += n - lim; } for (int i = divisor; i <= offs; i += divisor) { if (gcd(n, i) == divisor) { ans += offs == n ? n : offs - i + 1; } } left += goffs; } } cout << ans << endl; } ```
#include <bits/stdc++.h> long long gcd(long long a, long long b) { while (a && b) if (a > b) a %= b; else b %= a; return a + b; } using namespace std; int main() { long long n; cin >> n; long long a[n], qw[n]; long long c[n]; for (int i = 0; i < n; i++) cin >> a[i]; bool f[n]; long long ans = 0; long long g[n + 1]; for (long long i = 1; i <= n; i++) g[i] = gcd(i, n); for (int j = 1; j <= n / 2; j++) { if (n % j != 0) continue; for (int i = 0; i < n; i++) f[i] = false, qw[i] = 0; for (int i = 0; i < j; i++) { long long mx = -1; for (int t = i; t < n; t += j) mx = max(mx, a[t]); for (int t = i; t < n; t += j) if (a[t] == mx) f[t] = true; } for (int i = 0; i < n; i++) c[i] = 0; int y = n - 1; if (f[0]) { c[0] = 1; if (g[c[0]] == j) qw[0]++; while (c[0] + 1 < n && f[y]) { c[0]++; y--; if (g[c[0]] == j) qw[0]++; } } for (int i = 1; i < n; i++) if (f[i]) { qw[i] = qw[i - 1]; c[i] = min(c[i - 1] + 1, (long long)(n - 1)); if (c[i] != c[i - 1] && g[c[i]] == j) qw[i]++; } for (int i = 0; i < n; i++) ans += qw[i]; } cout << ans; }
### Prompt Generate a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> long long gcd(long long a, long long b) { while (a && b) if (a > b) a %= b; else b %= a; return a + b; } using namespace std; int main() { long long n; cin >> n; long long a[n], qw[n]; long long c[n]; for (int i = 0; i < n; i++) cin >> a[i]; bool f[n]; long long ans = 0; long long g[n + 1]; for (long long i = 1; i <= n; i++) g[i] = gcd(i, n); for (int j = 1; j <= n / 2; j++) { if (n % j != 0) continue; for (int i = 0; i < n; i++) f[i] = false, qw[i] = 0; for (int i = 0; i < j; i++) { long long mx = -1; for (int t = i; t < n; t += j) mx = max(mx, a[t]); for (int t = i; t < n; t += j) if (a[t] == mx) f[t] = true; } for (int i = 0; i < n; i++) c[i] = 0; int y = n - 1; if (f[0]) { c[0] = 1; if (g[c[0]] == j) qw[0]++; while (c[0] + 1 < n && f[y]) { c[0]++; y--; if (g[c[0]] == j) qw[0]++; } } for (int i = 1; i < n; i++) if (f[i]) { qw[i] = qw[i - 1]; c[i] = min(c[i - 1] + 1, (long long)(n - 1)); if (c[i] != c[i - 1] && g[c[i]] == j) qw[i]++; } for (int i = 0; i < n; i++) ans += qw[i]; } cout << ans; } ```
#include <bits/stdc++.h> using namespace std; const int MN = 2e5 + 100; int t[MN]; int cnt[MN]; int ma[MN]; int best[MN]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> t[i]; cnt[i + 1] = cnt[i] + (gcd(i + 1, n) == 1); } long long ans = 0; for (int i = 1; i < n; ++i) { if (n % i == 0) { int z = i; for (int j = 0; j < n; ++j) { ma[j] = 0; best[j] = 0; } for (int j = 1; j <= n / z; ++j) { cnt[j] = cnt[j - 1] + (gcd(j, n / z) == 1); } for (int j = 0; j < n; ++j) { ma[j % i] = max(ma[j % z], t[j]); } for (int j = 0; j < n; ++j) { if (best[j % z] == j / z && t[j] == ma[j % z]) ++best[j % z]; } for (int j = n - 1; j >= 0; --j) { if (t[j] == ma[j % z]) best[j] = best[(j + z) % n] + 1; best[j] = min(best[j], n / z); } deque<int> q; for (int j = 0; j < i; ++j) { while (q.size() && q.back() > best[j]) q.pop_back(); q.push_back(best[j]); } for (int j = 0; j < n; ++j) { ans += cnt[q.front()]; if (best[j] == q.front()) { q.pop_front(); } while (q.size() && q.back() > best[(j + z) % n]) q.pop_back(); q.push_back(best[(j + z) % n]); } } } cout << ans << '\n'; }
### Prompt Your challenge is to write a cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MN = 2e5 + 100; int t[MN]; int cnt[MN]; int ma[MN]; int best[MN]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> t[i]; cnt[i + 1] = cnt[i] + (gcd(i + 1, n) == 1); } long long ans = 0; for (int i = 1; i < n; ++i) { if (n % i == 0) { int z = i; for (int j = 0; j < n; ++j) { ma[j] = 0; best[j] = 0; } for (int j = 1; j <= n / z; ++j) { cnt[j] = cnt[j - 1] + (gcd(j, n / z) == 1); } for (int j = 0; j < n; ++j) { ma[j % i] = max(ma[j % z], t[j]); } for (int j = 0; j < n; ++j) { if (best[j % z] == j / z && t[j] == ma[j % z]) ++best[j % z]; } for (int j = n - 1; j >= 0; --j) { if (t[j] == ma[j % z]) best[j] = best[(j + z) % n] + 1; best[j] = min(best[j], n / z); } deque<int> q; for (int j = 0; j < i; ++j) { while (q.size() && q.back() > best[j]) q.pop_back(); q.push_back(best[j]); } for (int j = 0; j < n; ++j) { ans += cnt[q.front()]; if (best[j] == q.front()) { q.pop_front(); } while (q.size() && q.back() > best[(j + z) % n]) q.pop_back(); q.push_back(best[(j + z) % n]); } } } cout << ans << '\n'; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/stack:32000000") using namespace std; const int INF = 2147483647; const long long LLINF = 9223372036854775807LL; int gcd(int x, int y) { return x ? gcd(y % x, x) : y; } long long solve(const vector<int>& a, int len) { vector<int> best(len); int rem = 0; for (int i = 0; i < int((a).size()); ++i) { best[rem] = max(best[rem], a[i]); ++rem; if (rem == len) rem = 0; } int bestcnt = int((a).size()) / len - 1; vector<int> f(bestcnt + 1); for (int i = 1; i < int((f).size()); ++i) f[i] = gcd(i, int((a).size()) / len) == 1; for (int i = 0; i < int((f).size()) - 1; ++i) f[i + 1] += f[i]; long long ans = 0; rem = 0; int curi = 0; int curlen = 0; for (int ii = 0; ii < 2 * int((a).size()); ++ii) { if (a[curi] == best[rem]) curlen++; else curlen = 0; if (ii >= int((a).size())) ans += f[min(curlen / len, bestcnt)]; curi++; if (curi == int((a).size())) curi = 0; ++rem; if (rem == len) rem = 0; } return ans; } int main() { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); bool same = true; for (int i = 0; i < n; ++i) same &= a[i] == a[0]; if (same) { long long ans = n * (long long)(n - 1); cout << ans << endl; return 0; } long long ans = 0; for (int i = 1; i < n; ++i) { if (n % i == 0) { long long tans = solve(a, i); ans += tans; } } cout << ans << endl; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/stack:32000000") using namespace std; const int INF = 2147483647; const long long LLINF = 9223372036854775807LL; int gcd(int x, int y) { return x ? gcd(y % x, x) : y; } long long solve(const vector<int>& a, int len) { vector<int> best(len); int rem = 0; for (int i = 0; i < int((a).size()); ++i) { best[rem] = max(best[rem], a[i]); ++rem; if (rem == len) rem = 0; } int bestcnt = int((a).size()) / len - 1; vector<int> f(bestcnt + 1); for (int i = 1; i < int((f).size()); ++i) f[i] = gcd(i, int((a).size()) / len) == 1; for (int i = 0; i < int((f).size()) - 1; ++i) f[i + 1] += f[i]; long long ans = 0; rem = 0; int curi = 0; int curlen = 0; for (int ii = 0; ii < 2 * int((a).size()); ++ii) { if (a[curi] == best[rem]) curlen++; else curlen = 0; if (ii >= int((a).size())) ans += f[min(curlen / len, bestcnt)]; curi++; if (curi == int((a).size())) curi = 0; ++rem; if (rem == len) rem = 0; } return ans; } int main() { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); bool same = true; for (int i = 0; i < n; ++i) same &= a[i] == a[0]; if (same) { long long ans = n * (long long)(n - 1); cout << ans << endl; return 0; } long long ans = 0; for (int i = 1; i < n; ++i) { if (n % i == 0) { long long tans = solve(a, i); ans += tans; } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 9; const int INF = 1e9; int n; int a[MAXN]; bool ok[MAXN]; vector<int> fac; vector<int> Max[2000]; vector<int> len[2000]; vector<int> p[2000]; int q[MAXN]; long long ans; inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= n; ++i) if (n % i == 0) { fac.push_back(i); q[i] = fac.size() - 1; } for (int i = 0; i < (int)fac.size(); ++i) { int d = fac[i], p = n / d; for (int j = 0; j < d; ++j) { int val = -INF; for (int k = 0; k < p; ++k) val = max(val, a[k * d + j + 1]); Max[i].push_back(val); } } for (int i = 0; i < (int)fac.size(); ++i) { int d = fac[i]; bool all = true; memset(ok, 0, sizeof ok); for (int j = 1; j <= n; ++j) if (a[j] == Max[i][(j - 1) % d]) ok[j] = true; else all = false; if (all) len[i].push_back(n); else { int l = 0, r = n + 1; if (ok[1] == ok[n]) { while (ok[l + 1]) l++; while (ok[r - 1]) r--; len[i].push_back(l + n - r + 1); } l++; while (l < r) { int last; while (!ok[l] && l < r) l++; last = l; if (l >= r) break; while (ok[l + 1] && l < r) l++; l++; len[i].push_back(l - last); } } } for (int i = 1; i < n; ++i) p[q[gcd(i, n)]].push_back(i / gcd(i, n)); for (int i = 0; i < (int)fac.size(); ++i) { int last = 0; long long sum = 0; sort(len[i].begin(), len[i].end()); for (int j = 0; j < (int)len[i].size(); ++j) sum += len[i][j]; for (int j = 0; j < (int)p[i].size(); ++j) { if (len[i].size() == 1 && sum == n) { ans += n; continue; } while (last < (int)len[i].size() && len[i][last] - p[i][j] * fac[i] + 1 <= 0) { sum -= len[i][last]; last++; } ans += sum - p[i][j] * fac[i] * (len[i].size() - last) + (len[i].size() - last); } } cout << ans << endl; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 9; const int INF = 1e9; int n; int a[MAXN]; bool ok[MAXN]; vector<int> fac; vector<int> Max[2000]; vector<int> len[2000]; vector<int> p[2000]; int q[MAXN]; long long ans; inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= n; ++i) if (n % i == 0) { fac.push_back(i); q[i] = fac.size() - 1; } for (int i = 0; i < (int)fac.size(); ++i) { int d = fac[i], p = n / d; for (int j = 0; j < d; ++j) { int val = -INF; for (int k = 0; k < p; ++k) val = max(val, a[k * d + j + 1]); Max[i].push_back(val); } } for (int i = 0; i < (int)fac.size(); ++i) { int d = fac[i]; bool all = true; memset(ok, 0, sizeof ok); for (int j = 1; j <= n; ++j) if (a[j] == Max[i][(j - 1) % d]) ok[j] = true; else all = false; if (all) len[i].push_back(n); else { int l = 0, r = n + 1; if (ok[1] == ok[n]) { while (ok[l + 1]) l++; while (ok[r - 1]) r--; len[i].push_back(l + n - r + 1); } l++; while (l < r) { int last; while (!ok[l] && l < r) l++; last = l; if (l >= r) break; while (ok[l + 1] && l < r) l++; l++; len[i].push_back(l - last); } } } for (int i = 1; i < n; ++i) p[q[gcd(i, n)]].push_back(i / gcd(i, n)); for (int i = 0; i < (int)fac.size(); ++i) { int last = 0; long long sum = 0; sort(len[i].begin(), len[i].end()); for (int j = 0; j < (int)len[i].size(); ++j) sum += len[i][j]; for (int j = 0; j < (int)p[i].size(); ++j) { if (len[i].size() == 1 && sum == n) { ans += n; continue; } while (last < (int)len[i].size() && len[i][last] - p[i][j] * fac[i] + 1 <= 0) { sum -= len[i][last]; last++; } ans += sum - p[i][j] * fac[i] * (len[i].size() - last) + (len[i].size() - last); } } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; int a[maxn]; int maxnum[maxn]; int f[maxn]; int n, d; int gcd(int a, int b) { int t; while (b) { t = a; a = b; b = t % b; } return a; } long long cal(int cnt) { long long ans = 0; for (int i = 1; i <= cnt / d; i++) if (gcd(n / d, i) == 1) ans += cnt - i * d + 1; return ans; } long long work() { memset(maxnum, 0, sizeof(int) * d); for (int i = 0; i < n; i++) maxnum[i % d] = max(maxnum[i % d], a[i]); for (int i = 0; i < n; i++) f[i] = (a[i] == maxnum[i % d]); int op = 0, ed = n - 1; while (f[op] && op < n) op++; while (f[ed] && ed >= 0) ed--; if (op == n) { long long ans = 0; for (int i = 1; i < n / d; i++) if (gcd(n / d, i) == 1) ans += n; return ans; } long long ans = 0; long long cnt = 0; ans += cal(op + n - 1 - ed); for (int i = op; i <= ed; i++) { if (f[i]) cnt++; else { if (cnt) ans += cal(cnt); cnt = 0; } } return ans; } int main() { long long ans = 0; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (d = 1; d < n; d++) if (n % d == 0) ans += work(); printf("%I64d\n", ans); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 200010; int a[maxn]; int maxnum[maxn]; int f[maxn]; int n, d; int gcd(int a, int b) { int t; while (b) { t = a; a = b; b = t % b; } return a; } long long cal(int cnt) { long long ans = 0; for (int i = 1; i <= cnt / d; i++) if (gcd(n / d, i) == 1) ans += cnt - i * d + 1; return ans; } long long work() { memset(maxnum, 0, sizeof(int) * d); for (int i = 0; i < n; i++) maxnum[i % d] = max(maxnum[i % d], a[i]); for (int i = 0; i < n; i++) f[i] = (a[i] == maxnum[i % d]); int op = 0, ed = n - 1; while (f[op] && op < n) op++; while (f[ed] && ed >= 0) ed--; if (op == n) { long long ans = 0; for (int i = 1; i < n / d; i++) if (gcd(n / d, i) == 1) ans += n; return ans; } long long ans = 0; long long cnt = 0; ans += cal(op + n - 1 - ed); for (int i = op; i <= ed; i++) { if (f[i]) cnt++; else { if (cnt) ans += cal(cnt); cnt = 0; } } return ans; } int main() { long long ans = 0; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (d = 1; d < n; d++) if (n % d == 0) ans += work(); printf("%I64d\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; inline void readi(int &x); const int maxn = 200005; int n; long long ans; int a[maxn * 2], c[maxn * 2], b[maxn]; vector<int> v[maxn]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int calc(int g, int l) { int low = -1, high = v[g].size(), mid; while (low + 1 < high) { mid = low + high >> 1; if (c[l + v[g][mid]] - c[l] == 0) low = mid; else high = mid; } return low + 1; } int main() { readi(n); for (int i = 0; i < n; i++) readi(a[i]), a[i + n] = a[i]; a[n + n] = a[0]; for (int i = 1; i < n; i++) v[gcd(n, i)].push_back(i); for (int g = 1; g < n; g++) if (!v[g].empty()) { memset(b, 0, n + 1 << 2); for (int i = 0; i < n; i++) b[i % g] = max(b[i % g], a[i]); c[0] = 0; for (int i = 0; i <= n + n; i++) { if (a[i] >= b[i % g]) c[i + 1] = c[i]; else c[i + 1] = c[i] + 1; } for (int i = 0; i < n; i++) ans += calc(g, i); } cout << ans << endl; return 0; } inline void readi(int &x) { char c; while (c = getchar(), c < '0' || c > '9') ; x = c ^ '0'; while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + (c ^ '0'); }
### Prompt Construct a Cpp code solution to the problem outlined: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline void readi(int &x); const int maxn = 200005; int n; long long ans; int a[maxn * 2], c[maxn * 2], b[maxn]; vector<int> v[maxn]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int calc(int g, int l) { int low = -1, high = v[g].size(), mid; while (low + 1 < high) { mid = low + high >> 1; if (c[l + v[g][mid]] - c[l] == 0) low = mid; else high = mid; } return low + 1; } int main() { readi(n); for (int i = 0; i < n; i++) readi(a[i]), a[i + n] = a[i]; a[n + n] = a[0]; for (int i = 1; i < n; i++) v[gcd(n, i)].push_back(i); for (int g = 1; g < n; g++) if (!v[g].empty()) { memset(b, 0, n + 1 << 2); for (int i = 0; i < n; i++) b[i % g] = max(b[i % g], a[i]); c[0] = 0; for (int i = 0; i <= n + n; i++) { if (a[i] >= b[i % g]) c[i + 1] = c[i]; else c[i + 1] = c[i] + 1; } for (int i = 0; i < n; i++) ans += calc(g, i); } cout << ans << endl; return 0; } inline void readi(int &x) { char c; while (c = getchar(), c < '0' || c > '9') ; x = c ^ '0'; while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + (c ^ '0'); } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 4e5 + 5; array<int, 200000> a, m, sum, g; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) g[i] = gcd(i, n); long long ans = 0; for (int i = 1; i < n; i++) { if (n % i == 0) { m.fill(0); for (int j = 0; j < n; j++) m[j % i] = max(m[j % i], a[j]); int l = 0; while (l < n && a[l] == m[l % i]) l++; sum.fill(0); if (l == n) for (int j = 1; j < n; j++) sum[j] += n; while (l < n) { while (l < n && a[l] != m[l % i]) l++; int r = l; while (a[r % n] == m[r % i]) r++; for (int j = l; j < r; j++) sum[j - l + 1] += r - j; l = r; } for (int j = 1; j < n; j++) if (g[j] == i) ans += sum[j]; } } cout << ans << endl; return 0; }
### Prompt Please create a solution in cpp to the following problem: You are given an infinite periodic array a0, a1, ..., an - 1, ... with the period of length n. Formally, <image>. A periodic subarray (l, s) (0 ≀ l < n, 1 ≀ s < n) of array a is an infinite periodic array with a period of length s that is a subsegment of array a, starting with position l. A periodic subarray (l, s) is superior, if when attaching it to the array a, starting from index l, any element of the subarray is larger than or equal to the corresponding element of array a. An example of attaching is given on the figure (top β€” infinite array a, bottom β€” its periodic subarray (l, s)): <image> Find the number of distinct pairs (l, s), corresponding to the superior periodic arrays. Input The first line contains number n (1 ≀ n ≀ 2Β·105). The second line contains n numbers a0, a1, ..., an - 1 (1 ≀ ai ≀ 106), separated by a space. Output Print a single integer β€” the sought number of pairs. Examples Input 4 7 1 2 3 Output 2 Input 2 2 1 Output 1 Input 3 1 1 1 Output 6 Note In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as a0 β‰₯ a0, a0 β‰₯ a1, a0 β‰₯ a2, a0 β‰₯ a3, a0 β‰₯ a0, ... Subarray (3, 2) is superior a3 β‰₯ a3, a0 β‰₯ a0, a3 β‰₯ a1, a0 β‰₯ a2, a3 β‰₯ a3, ... In the third sample any pair of (l, s) corresponds to a superior subarray as all the elements of an array are distinct. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 4e5 + 5; array<int, 200000> a, m, sum, g; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) g[i] = gcd(i, n); long long ans = 0; for (int i = 1; i < n; i++) { if (n % i == 0) { m.fill(0); for (int j = 0; j < n; j++) m[j % i] = max(m[j % i], a[j]); int l = 0; while (l < n && a[l] == m[l % i]) l++; sum.fill(0); if (l == n) for (int j = 1; j < n; j++) sum[j] += n; while (l < n) { while (l < n && a[l] != m[l % i]) l++; int r = l; while (a[r % n] == m[r % i]) r++; for (int j = l; j < r; j++) sum[j - l + 1] += r - j; l = r; } for (int j = 1; j < n; j++) if (g[j] == i) ans += sum[j]; } } cout << ans << endl; return 0; } ```