#include <bits/stdc++.h>

using i64 = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int N, P;
    std::cin >> N >> P;
    
    std::vector<int> A(N);
    for (int i = 0; i < N; i++) {
        std::cin >> A[i];
    }
    
    i64 ans = 0;
    for (int t = P; ; t *= P) {
        std::vector<int> a(N);
        for (int i = 0; i < N; i++) {
            a[i] = A[i] % t;
        }
        std::sort(a.begin(), a.end());
        for (int i = 0, j = 0; i < N; i = j) {
            while (j < N && a[i] == a[j]) {
                j++;
            }
            ans += 1LL * (j - i) * (j - i - 1) / 2;
        }
        if (1LL * t * P > 1E9) {
            break;
        }
    }
    std::cout << ans << "\n";
    
    return 0;
}