Source Code
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 9;
const int mod = 1e9 + 7;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int t;
    cin >> t;
    while(t--){
        int n , k;
        cin >> n >> k;
        map<int ,int> mp;
        int arr[n];
        int sum = 0;
        for(int i = 0; i < n; i++){
            cin >> arr[i];
        }
        for(int i = n - 1; i >= 0; i--){
            sum += arr[i];
            sum %= k;
            mp[sum]++;
        }
        long long ans = 0;
        long long num = 0;
        for(int i = 0; i < n; i++){
            mp[sum]--;
            sum -= arr[i];
            sum = (sum%k + k) % k;
            num += arr[i];
            num %= k;
            int e = 0;
            if(num != 0) e = k - num;
            ans += mp[e];

        }
        cout << ans << "\n";
    }
    return 0;
}  
Copy
Number of Ways AbduSaber
GNU G++17
794 ms
19.8 MB
Accepted