其他分享
首页 > 其他分享> > A. XXXXX

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;
}

标签:int,sum,cin,flag,倍数,答案,XXXXX
来源: https://www.cnblogs.com/incra/p/16387663.html