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+1);
		vector<ll>fo(n+1);
		

		to[0] = -1;
		fo[0] = -1;
		ll sum = 0;
		for (int i = 0; i < n; i++) {
			cin >> v[i];
			sum += v[i];
			to[i+1] = sum%k;
			
		}
		
		for (int i = 1; i < n; i++) {
			sum -= v[i - 1];
			fo[n-i] = sum%k;
		}
		ll s = 0;
		for (int i = 1; i <= n; i++) {
			ll x = (k - to[i])%k;
			s += count(fo.begin(), fo.end() - i, x);
			ll y = (k - fo[i])%k;
			s += count(to.begin(), to.end()-i, y);
		}
		cout << s/2 << "\n";

	}
	return 0;
}
Copy
Number of Ways Ayman_Attili
GNU G++17
2033 ms
948 KB
Time Limit Exceeded