/
3.cpp
74 lines (59 loc) · 1.37 KB
/
3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
const int INF = 1e9 + 7;
int n;
vector<ll> a;
void input() {
cin >> n;
a.resize(n);
for (auto& i : a) cin >> i;
}
int getGroup2Ans() { // O(nsqrt(n))
vector<int> di;
for (int i = 1; i <= sqrt(n) + 0.5; i++) {
if (n % i) continue;
di.push_back(i);
if (i != n / i) di.push_back(n / i);
}
sort(all(di));
for (auto& d : di) {
int isValid = 1;
for (int i = 0; i < n; i++) {
if (a[i] != a[(i + d) % n]) {
isValid = 0; break;
}
}
if (isValid) return d;
}
return n;
}
ll sumOfAll() {
ll ret = 0;
for (auto& i : a) ret += i;
return ret;
}
void a_few_moment_later() {
for (int i = 2 * n; i >= 0; i--) {
if (a[i % n] <= 1) continue;
a[(i + n - 1) % n] += a[i % n] - 1;
a[i % n] = 1;
}
}
int main() {
cin.tie(NULL); cout.tie(NULL);
ios_base::sync_with_stdio(false);
int tc; cin >> tc;
for (int tt = 1; tt <= tc; tt++) {
cout << "Case #" << tt << '\n';
input();
if (n <= sumOfAll()) cout << 1 << '\n';
else {
a_few_moment_later();
cout << getGroup2Ans() << '\n';
}
cout << flush;
}
}