Source Code
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include<functional>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include<iterator>
#include <stdio.h>
#include <string.h>
#define T int t=0;cin>>t;while(t--)
#define F(i,n) for(ll i=0;i<n;i++)
#define fo(i,a) for(auto & i : a)
#define all(a) a.begin(),a.end()
#define mem(x,y) memset(x,y,sizeof(x))
#define ll long long
#define ld long double
#define endl "\n"
#define vl vector<ll>
#define vi vector<int>
#define co first.first
#define co1 first.second
#define fr second.first
#define to second.second
#define sz(X) X.size()
using namespace std;
const ll mod = 1000000007;
void code();
///*-*-*-*-*-*-*-*-*-*-*-*-*-*-*>> # str main # <<*-*-*-*-*-*-*-*-*-*-*-*-*-*-*///
int main()
{
    code();
    T
    {
        int n,k;
        cin>>n>>k;
        map<int,ll>an;
        vi a(n);
        ll sum1 = 0, sum2 = 0, ans = 0;
        fo(i,a)
        {
            cin>>i;
            sum1+=i;
            an[sum1%k]++;
        }
        for(int i=n-1;i;i--)
        {
            an[sum1%k]--;
            sum1-=a[i];
            sum2+=a[i];
            if(sum2%k)ans+=an[k-(sum2%k)];
            else ans+=an[0];
        }
        cout<<ans<<endl;

    }
}
///*-*-*-*-*-*-*-*-*-*-*-*-*-*-*>> # end main # <<*-*-*-*-*-*-*-*-*-*-*-*-*-*-*///
void code()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(6);
}
Copy
Number of Ways ABD_ALMABOUD
GNU G++17
987 ms
26.2 MB
Accepted