#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
long long n;
cin >> n;
vector<long long> amber(n);
long long sum = 0;
for(int i = 0; i < n; i++) {
cin >> amber[i];
sum += amber[i];
}
long long result = 1;
stack<long long> candidates;
stack<long long> higher_candidates;
for(long long i = 1; i * i <= sum; i++) {
if(sum % i == 0) {
candidates.push(i);
if(i*i != sum) {
higher_candidates.push(sum / i);
}
}
}
while(!higher_candidates.empty()) {
candidates.push(higher_candidates.top());
higher_candidates.pop();
}
while (!candidates.empty()) {
long long wave_length = candidates.top();
candidates.pop();
if(wave_length>n) {
continue;
}
long long wave_count = 0;
vector<long long> wave_start_counter(n, 0);
for(long long i = 0; i < n; i++) {
wave_start_counter[i]= amber[i] - wave_count;
wave_count = amber[i];
if(wave_start_counter[i] < 0) {
wave_count = -1;
break;
}
if(i >= wave_length-1) {
wave_count -= wave_start_counter[i-wave_length+1];
}
}
if(wave_count == 0) {
result = wave_length;
break;
}
}
cout << result << endl;
}
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 | #include<iostream> #include<vector> #include<stack> #include<queue> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long n; cin >> n; vector<long long> amber(n); long long sum = 0; for(int i = 0; i < n; i++) { cin >> amber[i]; sum += amber[i]; } long long result = 1; stack<long long> candidates; stack<long long> higher_candidates; for(long long i = 1; i * i <= sum; i++) { if(sum % i == 0) { candidates.push(i); if(i*i != sum) { higher_candidates.push(sum / i); } } } while(!higher_candidates.empty()) { candidates.push(higher_candidates.top()); higher_candidates.pop(); } while (!candidates.empty()) { long long wave_length = candidates.top(); candidates.pop(); if(wave_length>n) { continue; } long long wave_count = 0; vector<long long> wave_start_counter(n, 0); for(long long i = 0; i < n; i++) { wave_start_counter[i]= amber[i] - wave_count; wave_count = amber[i]; if(wave_start_counter[i] < 0) { wave_count = -1; break; } if(i >= wave_length-1) { wave_count -= wave_start_counter[i-wave_length+1]; } } if(wave_count == 0) { result = wave_length; break; } } cout << result << endl; } |
English