結果

問題 No.2260 Adic Sum
ユーザー mtrhmtrh
提出日時 2023-04-07 22:10:50
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,906 ms / 2,000 ms
コード長 1,049 bytes
コンパイル時間 2,400 ms
コンパイル使用メモリ 219,516 KB
実行使用メモリ 50,304 KB
最終ジャッジ日時 2024-10-06 07:17:39
合計ジャッジ時間 14,346 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    int N;
    ll P;
    cin >> N >> P;
    vector<ll> A(N);
    vector<pair<int, ll>> B(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        int k = 0;
        while (A[i] % P == 0) {
            k++;
            A[i] /= P;
        }
        B[i] = {k, A[i]};
    }
    sort(B.begin(), B.end());
    ll ans = 0;
    vector<map<ll, int>> mp;
    ll now = 1;
    while (now < 1e9) {
        map<ll, int> tmp;
        mp.push_back(tmp);
        now *= P;
    }
    ll prek = -1;
    for (int i = 0; i < N; i++) {
        ll k = B[i].first, m = B[i].second;
        ans += k * (N - i - 1);
        if (k != prek) {
            for (int i = 0; i < mp.size(); i++) {
                mp[i].clear();
            }
        }
        ll p = P;
        int j = 0;
        while (p < 1e9) {
            ans += mp[j][m % p];
            mp[j][m % p]++;
            j++;
            p *= P;
        }
        prek = k;
    }
    cout << ans << endl;
    return 0;
}
0