結果

問題 No.1704 Many Bus Stops (easy)
ユーザー KoDKoD
提出日時 2021-10-08 22:48:04
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 7,823 bytes
コンパイル時間 3,313 ms
コンパイル使用メモリ 258,372 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-23 05:54:54
合計ジャッジ時間 5,863 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 46 ms
5,376 KB
testcase_02 AC 20 ms
5,376 KB
testcase_03 AC 20 ms
5,376 KB
testcase_04 AC 20 ms
5,376 KB
testcase_05 AC 19 ms
5,376 KB
testcase_06 AC 19 ms
5,376 KB
testcase_07 AC 20 ms
5,376 KB
testcase_08 AC 19 ms
5,376 KB
testcase_09 AC 20 ms
5,376 KB
testcase_10 AC 20 ms
5,376 KB
testcase_11 AC 20 ms
5,376 KB
testcase_12 AC 20 ms
5,376 KB
testcase_13 AC 20 ms
5,376 KB
testcase_14 AC 19 ms
5,376 KB
testcase_15 AC 20 ms
5,376 KB
testcase_16 AC 19 ms
5,376 KB
testcase_17 AC 20 ms
5,376 KB
testcase_18 AC 20 ms
5,376 KB
testcase_19 AC 20 ms
5,376 KB
testcase_20 AC 20 ms
5,376 KB
testcase_21 AC 20 ms
5,376 KB
testcase_22 AC 46 ms
5,376 KB
testcase_23 AC 46 ms
5,376 KB
testcase_24 AC 46 ms
5,376 KB
testcase_25 AC 46 ms
5,376 KB
testcase_26 AC 47 ms
5,376 KB
testcase_27 AC 47 ms
5,376 KB
testcase_28 AC 47 ms
5,376 KB
testcase_29 AC 47 ms
5,376 KB
testcase_30 AC 47 ms
5,376 KB
testcase_31 AC 47 ms
5,376 KB
testcase_32 AC 47 ms
5,376 KB
testcase_33 AC 47 ms
5,376 KB
testcase_34 AC 47 ms
5,376 KB
testcase_35 AC 47 ms
5,376 KB
testcase_36 AC 47 ms
5,376 KB
testcase_37 AC 47 ms
5,376 KB
testcase_38 AC 47 ms
5,376 KB
testcase_39 AC 47 ms
5,376 KB
testcase_40 AC 46 ms
5,376 KB
testcase_41 AC 46 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class T> constexpr T totient(T x) {
    T ret = x;
    for (T i = 2; i * i <= x; ++i) {
        if (x % i == 0) {
            ret /= i;
            ret *= i - 1;
            while (x % i == 0) x /= i;
        }
    }
    if (x > 1) {
        ret /= x;
        ret *= x - 1;
    }
    return ret;
}

template <class T> constexpr T rem_euclid(T value, const T& mod) { return (value %= mod) >= 0 ? value : value + mod; }

template <u32 MOD, std::enable_if_t<((u32)1 <= MOD and MOD <= ((u32)1 << 31))>* = nullptr> class StaticModint {
    using Mint = StaticModint;

    static inline constexpr u32 PHI = totient(MOD);
    u32 v;

  public:
    static constexpr u32 mod() noexcept { return MOD; }

    template <class T, std::enable_if_t<std::is_signed_v<T> and std::is_integral_v<T>>* = nullptr>
    static constexpr T normalize(const T x) noexcept {
        return rem_euclid<std::common_type_t<T, i64>>(x, MOD);
    }
    template <class T, std::enable_if_t<std::is_unsigned_v<T> and std::is_integral_v<T>>* = nullptr>
    static constexpr T normalize(const T x) noexcept {
        return x % MOD;
    }

    constexpr StaticModint() noexcept : v(0) {}
    template <class T> constexpr StaticModint(const T x) noexcept : v(normalize(x)) {}
    template <class T> static constexpr Mint raw(const T x) noexcept {
        Mint ret;
        ret.v = x;
        return ret;
    }

    constexpr u32 get() const noexcept { return v; }
    constexpr Mint neg() const noexcept { return raw(v == 0 ? 0 : MOD - v); }
    constexpr Mint inv() const noexcept { return pow(PHI - 1); }
    constexpr Mint pow(u64 exp) const noexcept {
        Mint ret(1), mult(*this);
        for (; exp > 0; exp >>= 1) {
            if (exp & 1) ret *= mult;
            mult *= mult;
        }
        return ret;
    }

    constexpr Mint operator-() const noexcept { return neg(); }
    constexpr Mint operator~() const noexcept { return inv(); }

    constexpr Mint operator+(const Mint& rhs) const noexcept { return Mint(*this) += rhs; }
    constexpr Mint& operator+=(const Mint& rhs) noexcept {
        if ((v += rhs.v) >= MOD) v -= MOD;
        return *this;
    }

    constexpr Mint operator-(const Mint& rhs) const noexcept { return Mint(*this) -= rhs; }
    constexpr Mint& operator-=(const Mint& rhs) noexcept {
        if (v < rhs.v) v += MOD;
        v -= rhs.v;
        return *this;
    }

    constexpr Mint operator*(const Mint& rhs) const noexcept { return Mint(*this) *= rhs; }
    constexpr Mint& operator*=(const Mint& rhs) noexcept {
        v = (u64)v * rhs.v % MOD;
        return *this;
    }

    constexpr Mint operator/(const Mint& rhs) const noexcept { return Mint(*this) /= rhs; }
    constexpr Mint& operator/=(const Mint& rhs) noexcept { return *this *= rhs.inv(); }

    constexpr bool operator==(const Mint& rhs) const noexcept { return v == rhs.v; }
    constexpr bool operator!=(const Mint& rhs) const noexcept { return v != rhs.v; }
    friend std::ostream& operator<<(std::ostream& stream, const Mint& rhs) { return stream << rhs.v; }
};

using Modint1000000007 = StaticModint<1000000007>;
using Modint998244353 = StaticModint<998244353>;

template <class T> struct NumSemiRing {
    using Type = T;
    static constexpr T zero() { return T(0); }
    static constexpr T one() { return T(1); }
    static constexpr T sum(const T& x, const T& y) { return x + y; }
    static constexpr T product(const T& x, const T& y) { return x * y; }
};

template <class S> class SemiRingMatrix {
    using T = typename S::Type;
    using Self = SemiRingMatrix;
    std::vector<std::vector<T>> data;

  public:
    SemiRingMatrix() = default;
    explicit SemiRingMatrix(const usize h, const usize w, const T& val = S::zero()) : data(h, std::vector<T>(w, val)) {}

    SemiRingMatrix(const std::vector<std::vector<T>>& vec) : data(vec) {}
    SemiRingMatrix(const std::initializer_list<std::initializer_list<T>>& list) {
        data.reserve(list.size());
        for (const auto& v : list) {
            data.emplace_back(v.begin(), v.end());
        }
    }
    template <usize H, usize W> SemiRingMatrix(const std::array<std::array<T, W>, H>& arr) {
        data.reserve(H);
        for (const auto& v : arr) {
            data.emplace_back(v.begin(), v.end());
        }
    }

    usize height() const { return data.size(); }
    usize width() const { return data.empty() ? 0 : data[0].size(); }

    T& operator()(const usize i, const usize j) {
        assert(i < height());
        assert(j < width());
        return data[i][j];
    }
    const T& operator()(const usize i, const usize j) const {
        assert(i < height());
        assert(j < width());
        return data[i][j];
    }

    Self operator+(const Self& other) const { return Self(*this) += other; }
    Self& operator+=(const Self& other) {
        assert(height() == other.height());
        assert(width() == other.width());
        for (const usize i : rep(0, height())) {
            for (const usize j : rep(0, width())) {
                data[i][j] = S::sum(data[i][j], other.data[i][j]);
            }
        }
        return *this;
    }

    Self operator*(const Self& other) const {
        assert(width() == other.height());
        Self ret(height(), other.width(), S::zero());
        for (const usize i : rep(0, height())) {
            for (const usize k : rep(0, width())) {
                for (const usize j : rep(0, other.width())) {
                    ret.data[i][j] = S::sum(ret.data[i][j], S::product(data[i][k], other.data[k][j]));
                }
            }
        }
        return ret;
    }

    Self operator*(const T& other) const { return Self(*this) *= other; }
    Self& operator*=(const T& other) {
        for (const usize i : rep(0, height())) {
            for (const usize j : rep(0, width())) {
                data[i][j] = S::product(data[i][j], other);
            }
        }
    }

    Self pow(u64 exp) const {
        assert(height() == width());
        Self ret(height(), width(), S::zero()), mult(*this);
        for (const usize i : rep(0, height())) {
            ret.data[i][i] = S::one();
        }
        for (; exp > 0; exp >>= 1) {
            if (exp & 1) ret = ret * mult;
            mult = mult * mult;
        }
        return ret;
    }
};

template <class T> using Vec = std::vector<T>;

using Fp = Modint1000000007;
using Num = NumSemiRing<Fp>;
using Matrix = SemiRingMatrix<Num>;

void main_() {
    u32 C, N, M;
    std::cin >> N;
    C = 3;
    M = 1;
    Matrix A = {{1, 0, 0, 0}};
    Matrix B = {{Fp(1) / Fp(C), 0, 0, 1},
                {Fp(C - 1) / Fp(C), 0, 0, 0},
                {0, Fp(1) / Fp(C - 1), Fp(1) / Fp(C), Fp(C - 2) / Fp(C - 1)},
                {0, 0, Fp(C - 1) / Fp(C), 0}};
    const auto P = (A * B.pow(N))(0, 0);
    std::cout << Fp(1) - (Fp(1) - P).pow(M) << '\n';
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    usize cases;
    std::cin >> cases;
    while (cases--) main_();
    return 0;
}
0