题目链接:https://codeforces.com/contest/1364/problem/A
找出大小为 $n$ 的数组 $a$ 的最长连续子数组,其元素和不被 $x$ 整除。
#include <bits/stdc++.h> using namespace std; void solve() { int n, x; cin >> n >> x; int a[n] = {}, sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; a[i] %= x, sum += a[i]; } if (sum == 0) cout << -1 << "\n"; else if (sum % x) cout << n << "\n"; else { int l = 0, r = n - 1; while (a[l] == 0) ++l; while (a[r] == 0) --r; cout << max(n - 1 - l, r) << "\n"; } } int main() { int t; cin >> t; while (t--) solve(); }
Codeforces Round #649 (Div. 2) A. XXXXX
原文:https://www.cnblogs.com/Kanoon/p/13125654.html