Source Code
#include<bits/stdc++.h>
#define cin(vec) for(auto& i : vec) cin >> i
#define cout(vec) for(auto& i : vec) cout << i 
#define ll long long
#define author_mohamed cin.tie(0)->sync_with_stdio(0);cin.exceptions(cin.failbit);
#define ss short
#define umset unordered_multiset
#define uset unordered_set
#define fx(b)  fixed << setprecision(b)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define vi vector
#define vis vector<string>
#define t   int t;cin >> t;while(t--)
#define vi vector
#define st string
#define mod  1000000007
#define mp   make_pair
using namespace std;
ll gcd(ll a, ll b) {if (a < b)swap(a, b);while (a != 0 && b != 0) {ll r = a % b;a = b;b = r;}return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
unsigned ll nCr(int n, int r) {if (r > n)return 0;r = max(r, n - r); unsigned ll ans = 1, div = 1, i = r + 1;while (i <= n) {ans *= i;i++;ans /= div;div++;}return ans;}
 unsigned ll npr(int n,int r){if(r>n)return 0;unsigned ll a=1,i=n-r+1; while(i<=n){a*=i;i++;}return a;}
int xc[4] = {-1, 1, 0, 0};
int yc[4] = {0, 0, -1, 1};
 


void solve(){
    int n,k;cin>>n>>k;
    vi<ll>x(n),a(n);
    for(int i=0;i<n;i++){
        cin>>x[i];
        a[i]=x[i];
    }
    reverse(all(a));
    for(int i=1;i<n;i++){
        x[i]+=x[i-1];
        a[i]+=a[i-1];
    }
    map<int,int>f;
    for(int i=0;i<n;i++){
        x[i]=x[i]%k;
        a[i]=a[i]%k;
        if(i<n-1)f[x[i]]++;
    }
    int r=n-2;
    ll ans=0;
    for(int i=0;i<n-1;i++){
        ans+=f[(k-a[i])%k];
        f[x[r]]--;
        r--;
    }
    cout<<ans<<'\n';
}
int main()
{
author_mohamed
//freopen("in.txt", "r", stdin);
 //freopen("out.txt", "w", stdout);
t{
    solve();
}
 return 0;
}
Copy
Number of Ways mohamed
GNU G++17
748 ms
22.1 MB
Accepted