A. XXXXX(codeforces)

A. XXXXX
problem: Given an array a, length n, and an integer b. Find the longest subinterval whose sum is not a multiple of b.

If: all numbers are multiples of b, then the sum of the sub-intervals of a is a multiple of b.
If: the sum of a is not a multiple of b, then the answer is the length of the entire array.
If: the sum of a is a multiple of b, then find the subscript x of the first number that is not a multiple of b from left to right, and find the subscript y of the first number that is not a multiple of b from right to left, then Take max (n − len ([1, x]), len ([1, y))) max(n-len([1,x]), len([1,y)))max(nl e n ( [ 1 ,x]),l e n ( [ 1 ,y ) ) )

Explain that the sum of the third a is a multiple of b, and subtract a number that is not a multiple of b, and the rest is not a multiple of b.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5+10;
int a[N];
int main() {
    
    
	ll t, n, x, sum, flag;
	scanf("%lld", &t);
	while(t--) {
    
    
		sum = 0, flag = 0;
		scanf("%lld%lld", &n, &x);
		for(int i=1; i<=n; i++) {
    
    
			scanf("%lld", &a[i]);
			sum += a[i];
			if(a[i] % x) flag = 1;
		}
		if(sum % x) printf("%lld\n", n);//总和不是b的倍数。
		else {
    
    
			if(!flag) printf("-1\n");//全是b的倍数
			else {
    
    
				ll i = 1, j = n;
				while(a[j] % x == 0) j--;//从右往左找
				while(a[i] % x == 0) i++;//从左往右找
				printf("%lld\n", max(j-1, n-i));
			}
		}
	}
	
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_45363113/article/details/106797393