Source Code
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int OO = 1e9+7 ,N=2e5+5;

int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 }; // dir array
int dy[] = { 1, -1, 0, 0, 1, -1, -1, 1 };
void File(){
#ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
#endif
}

int solve(){
    int n,k;cin>>n>>k;
    vector<int>a(n+1) , pre(n+1) , suf(n+2);
    map<int,int>mp;
    ll res=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        pre[i]=(a[i] +pre[i-1])%k;
    }
    for(int i=n;i>=1;i--){
        suf[i] = (a[i]+ suf[i+1])%k;
        mp[suf[i]]++;
    }
    for(int i=1;i<=n;i++){
        mp[suf[i]]--;
        res += mp[(k-pre[i])%k];
    }
    cout<<res<<endl;
    return 0;
}
int main() {
    File();
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t,i=1;cin>>t;
    while(t--)
        solve();

}
//Solve on paper first
//Overflooooooow
//Reverse Thinking
//If there's an equation, transform it into an easy one
//READ ALL PROBLEMS
//Verify Your thought before Coding
Copy
Number of Ways ahmes
GNU G++17
823 ms
21.4 MB
Accepted