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[100001];
int main() {
	int t;
	long long ans, n, k;
	vector <long long> suf, pre;
	cin >> t;
	while (t--) {
		cin >> n >> k;
		ans = 0;
		suf.push_back(0);
		pre.push_back(0);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			if (i < n - 1) pre.push_back(pre[i] + a[i]);
		}
		int index = 0;
		for (int i = n - 1; i > 0; i--) {
			suf.push_back(suf[index] + a[i]);
			index++;
		}
		for (int i = 1; i < pre.size(); i++) {
			for (int j = suf.size() - i; j > 0; j--) {
				if ((suf[j] + pre[i]) % k == 0)ans++;
			}
		}
		cout << ans << endl;
		suf.clear();
		pre.clear();
	}
	return 0;
}






Copy
Number of Ways Sarah
GNU G++17
2093 ms
668 KB
Time Limit Exceeded