給定 n 個數 (可能重複),對數組排序去重。對於去重後的數組 b[1..m],若 b[i] 的左右鄰居 (視為環狀) 滿足 `(b[i-1] + b[i]) % MOD == b[i+1]` (MOD = 2^31-1),則計入原數組中 b[i] 的出現次數。求總計數。
#include
using namespace std;
typedef long long LL;
const LL MOD = 2147483647; // 2^31 - 1
int main() {
int n, icase = 1;
while (~scanf("%d", &n)) {
map cnt;
vector a(n);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
cnt[a[i]]++;
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
int m = a.size();
printf("Case #%d: ", icase++);
if (m == 1) { puts("-1"); continue; }
int ans = 0;
for (int i = 0; i < m; i++) {
LL left = a[(i - 1 + m) % m];
LL right = a[(i + 1) % m];
if ((left + a[i]) % MOD == right) ans += cnt[a[i]];
}
printf("%d\n", ans);
}
return 0;
}