Source Code
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <math.h>
#include <vector>
#include <set>
#include <map>
using namespace std;
long long a[200002];
int main() {
	int t;
	long long ans, sum, x, n, k;
	cin >> t;
	while (t--) {
		cin >> n >> k;
		ans = sum = x = 0;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			sum += a[i];
		}
		if (sum % k == 0)ans += (n - 1);
		bool f = 0;
		for (int i = 1; i < n - 1; i++) {
			if ((sum - a[i]) % k == 0)ans++;
			x += a[i];
			if ((sum - x ) % k == 0 && f)ans++;
			f = 1;
		}
		f = 0;
		x = 0;
		for (int i = n - 2; i > 0; i--){
			x += a[i];
			if ((sum - x) % k == 0 && f)ans++;
			f = 1;
		}
		cout << ans << endl;
	}
	return 0;
}






Copy
Number of Ways Sarah
GNU G++17
58 ms
288 KB
Wrong Answer