Source Code
#include<bits/stdc++.h>
#include <cmath>
#include <complex>
using namespace std;
//freopen("window.in","r",stdin);
#define ll long long
//#define endl '\n';
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define dcm(a) setprecision(a)<<fixed
ll const MOD= 1e9+7;
#define clr(memo, a) memset(memo,a,sizeof(memo))
#define PI acos(-1)
typedef complex<double> point;
double  EPS=1e-9;
int dcmp(double x, double y) {	return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;	}
#define X real()
#define Y imag()
#define angle(a)                (atan2((a).imag(), (a).real()))
#define vec(a,b)                ((b)-(a))
#define same(p1,p2)             (dp(vec(p1,p2),vec(p1,p2)) < EPS)
//#define dp(a,b)                 ( (conj(a)*(b)).real() )	// a*b cos(T), if zero -> prep
#define cp(a,b)                 ( (conj(a)*(b)).imag() )	// a*b sin(T), if zero -> parllel
#define length(a)               (hypot((a).imag(), (a).real()))
#define normalize(a)            (a)/length(a)
#define rotateO(p,ang)          ((p)*exp(point(0,ang)))
#define rotateA(p,ang,about)  (rotateO(vec(about,p),ang)+about)
#define reflectO(v,m)  (conj((v)/(m))*(m))

void solve(int t){

int n;
cin>>n;
vector<ll> a(n);
vector<ll> md(n);
ll k;
cin>>k;
ll sum=0;
map<ll,int> cnt;
for(int i=0;i<n;i++){
    ll x;
    cin>>x;
    sum+=x;
    a[i]=x;
    md[i]=sum%k;
    cnt[md[i]]++;
}
ll ans=0;
ll sum2=0;

for(int i=n-1;i>0;i--){
        cnt[md[i]]--;
        sum2+=a[i];
        ll v=sum2%k;
        ans+=cnt[(k-v)%k];
}
cout<<ans<<endl;
}






int main(){


int t=1;
cin>>t;
for(int i=1;i<=t;i++)
    solve(i);
cout<<endl;
return 0;
}
Copy
Number of Ways Go8
GNU G++17
969 ms
28.5 MB
Accepted