結果

問題 No.2264 Gear Coloring
ユーザー sotanishysotanishy
提出日時 2023-03-08 18:54:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 3,805 bytes
コンパイル時間 2,256 ms
コンパイル使用メモリ 212,516 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 06:01:35
合計ジャッジ時間 3,161 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 15 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 15 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 29 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 58 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 4 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T>
bool chmax(T& a, const T& b) {
    return a < b ? (a = b, 1) : 0;
}
template <typename T>
bool chmin(T& a, const T& b) {
    return a > b ? (a = b, 1) : 0;
}

template <int mod>
class Modint {
    using mint = Modint;
    static_assert(mod > 0, "Modulus must be positive");

   public:
    static constexpr int get_mod() noexcept { return mod; }

    constexpr Modint(long long y = 0) noexcept
        : x(y >= 0 ? y % mod : (y % mod + mod) % mod) {}

    constexpr int value() const noexcept { return x; }

    constexpr mint& operator+=(const mint& r) noexcept {
        if ((x += r.x) >= mod) x -= mod;
        return *this;
    }
    constexpr mint& operator-=(const mint& r) noexcept {
        if ((x += mod - r.x) >= mod) x -= mod;
        return *this;
    }
    constexpr mint& operator*=(const mint& r) noexcept {
        x = static_cast<int>(1LL * x * r.x % mod);
        return *this;
    }
    constexpr mint& operator/=(const mint& r) noexcept {
        *this *= r.inv();
        return *this;
    }

    constexpr mint operator-() const noexcept { return mint(-x); }

    constexpr mint operator+(const mint& r) const noexcept {
        return mint(*this) += r;
    }
    constexpr mint operator-(const mint& r) const noexcept {
        return mint(*this) -= r;
    }
    constexpr mint operator*(const mint& r) const noexcept {
        return mint(*this) *= r;
    }
    constexpr mint operator/(const mint& r) const noexcept {
        return mint(*this) /= r;
    }

    constexpr bool operator==(const mint& r) const noexcept { return x == r.x; }
    constexpr bool operator!=(const mint& r) const noexcept { return x != r.x; }

    constexpr mint inv() const noexcept {
        int a = x, b = mod, u = 1, v = 0;
        while (b > 0) {
            int t = a / b;
            std::swap(a -= t * b, b);
            std::swap(u -= t * v, v);
        }
        return mint(u);
    }

    constexpr mint pow(long long n) const noexcept {
        mint ret(1), mul(x);
        while (n > 0) {
            if (n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend std::ostream& operator<<(std::ostream& os, const mint& r) {
        return os << r.x;
    }

    friend std::istream& operator>>(std::istream& is, mint& r) {
        long long t;
        is >> t;
        r = mint(t);
        return is;
    }

   private:
    int x;
};

using mint = Modint<998244353>;

std::vector<long long> divisor(long long n) {
    std::vector<long long> ret1, ret2;
    for (long long i = 1; i * i <= n; ++i) {
        if (i * i == n) {
            ret1.push_back(i);
        } else if (n % i == 0) {
            ret1.push_back(i);
            ret2.push_back(n / i);
        }
    }
    ret1.insert(ret1.end(), ret2.rbegin(), ret2.rend());
    return ret1;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, M;
    cin >> N >> M;
    vector<ll> A(N);
    ll lcm = 1;
    for (auto& x : A) {
        cin >> x;
        lcm = lcm / gcd(lcm, x) * x;
    }
    auto div = divisor(lcm);

    vector<mint> val(div.size());
    rep(i, 0, div.size()) {
        ll d = div[i];
        ll s = 0;
        for (auto a : A) s += gcd(a, d);
        val[i] = mint(M).pow(s);
    }

    rep(i, 0, div.size()) {
        rep(j, 0, i) {
            if (div[i] % div[j] == 0) {
                val[i] -= val[j];
            }
        }
    }

    mint ans = 0;
    rep(i, 0, div.size()) { ans += val[i] / div[i]; }
    cout << ans << endl;
}
0