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);
		


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

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