Source Code
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    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 Mohamedmaher
GNU G++17
596 ms
20.5 MB
Accepted