A. XXXXX

当总和不是x的倍数时,答案就是n;当数组里所有数是x的倍数时,答案是-1;否则从两侧一直往中间收缩,答案就是max (n-l,r-1)

#include <iostream>
using namespace std;
const int N = 100010;
int n,x,sum;
int a[N];
int main () {
	int T;
	cin >> T;
	while (T--) {
		sum = 0;
		bool flag = true;
		cin >> n >> x;
		for (int i = 1;i <= n;i++) {
			cin >> a[i];
			sum += a[i];
			if (a[i]%x) flag = false;
		}
		if (sum%x) cout << n << endl;
		else if (flag) cout << -1 << endl;
		else {
			int l = 1,r = n;
			while (a[l]%x == 0) l++;
			while (a[r]%x == 0) r--;
			cout << max (n-l,r-1) << endl;
		}
	}
	return 0;
}
posted @ 2022-06-18 09:12  incra  阅读(122)  评论(0)    收藏  举报