Source Code
#include <vector>
#include <string>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <iterator>
#include <deque>
#include <set>
#include <map>
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <fstream>
#include <functional>
#include <iomanip>
#include <cctype>
#include <utility>
#include <bitset>
#include <tuple>
#include <sstream>
#include <math.h>

using namespace std;
#define ll long long
#define lll long long int
#define PI 3.1415926535897
#define ld long double
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(X) ((ll)(X).size())
#define pll pair<ll, ll>
#define pi pair<int, int>
#define pb push_back
#define in insert
#define al(it) it.fr << " " << it.sc << "\n"
#define _cout(v)  for(auto f : v ) cout << f << " " ;
#define _cin(v)   for(auto &it : v)cin >> it ;
int main()
{
	ll t; cin >> t;
	while (t--) {
		ll n, k; cin >> n >> k;
		vector<ll>v(n);
		vector<ll>to(n);
		vector<ll>fo(n);
		set<ll>sto;
		set<ll>fto;

		ll sum = 0;
		for (int i = 0; i < n; i++) {
			cin >> v[i];
			sum += v[i];
			to[i] = sum;
			if (to[i] % k == 0) {
				sto.insert(i);
			}
		}
		fo[0] = sum;
		if (fo[0] % k == 0) {
			fto.insert(0);
		}
		for (int i = 1; i < n; i++) {
			fo[i] = fo[i - 1] - v[i - 1];
			if (fo[i] % k == 0) {
				fto.insert(i);
			}
		}
		ll s = 0;
		for (int x : sto) {
			for (int y : fto) {
				if (x + y > n) {
					break;
				}
				s++;
			}
		}
		cout << s << "\n";

	}
	return 0;
}
Copy
Number of Ways Ayman_Attili
GNU G++17
62 ms
288 KB
Wrong Answer