結果

問題 No.917 Make One With GCD
ユーザー Mister
提出日時 2020-04-26 13:26:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 1,315 bytes
コンパイル時間 1,676 ms
コンパイル使用メモリ 96,112 KB
最終ジャッジ日時 2025-01-10 01:56:38
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <numeric>
#include <algorithm>
#include <vector>
#include <map>

template <class T>
std::vector<T> divisors(T n) {
    std::vector<T> ret;
    for (T p = 1; p * p <= n; ++p) {
        if (n % p != 0) continue;
        ret.push_back(p);
        if (n / p == p) continue;
        ret.push_back(n / p);
    }
    return ret;
}

template <class T>
std::map<T, int> compress(std::vector<T>& v) {
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());

    std::map<T, int> rev;
    for (int i = 0; i < (int)v.size(); ++i) rev[v[i]] = i;
    return rev;
}

using lint = long long;

void solve() {
    int n;
    std::cin >> n;

    std::vector<int> xs(n);
    std::vector<int> ps{0};
    for (auto& x : xs) {
        std::cin >> x;
        for (auto p : divisors(x)) {
            ps.push_back(p);
        }
    }

    auto revp = compress(ps);
    int m = ps.size();

    std::vector<lint> dp(m, 0);
    dp[0] = 1;
    auto ndp = dp;

    for (auto x : xs) {
        for (int i = 0; i < m; ++i) {
            int j = revp[std::gcd(x, ps[i])];
            ndp[j] += dp[i];
        }
        dp = ndp;
    }

    std::cout << dp[1] << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0