結果

問題 No.1840 Random Painting
ユーザー 👑 hitonanodehitonanode
提出日時 2021-12-12 23:58:11
言語 C++23(draft)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 412 ms / 2,000 ms
コード長 7,397 bytes
コンパイル時間 1,736 ms
コンパイル使用メモリ 126,040 KB
実行使用メモリ 42,068 KB
最終ジャッジ日時 2023-09-09 23:31:39
合計ジャッジ時間 7,960 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 23 ms
7,028 KB
testcase_10 AC 209 ms
36,576 KB
testcase_11 AC 323 ms
40,820 KB
testcase_12 AC 201 ms
36,832 KB
testcase_13 AC 63 ms
32,944 KB
testcase_14 AC 398 ms
41,680 KB
testcase_15 AC 301 ms
40,612 KB
testcase_16 AC 340 ms
41,416 KB
testcase_17 AC 144 ms
34,928 KB
testcase_18 AC 409 ms
41,732 KB
testcase_19 AC 411 ms
41,688 KB
testcase_20 AC 407 ms
41,840 KB
testcase_21 AC 405 ms
42,068 KB
testcase_22 AC 410 ms
41,676 KB
testcase_23 AC 412 ms
41,660 KB
testcase_24 AC 61 ms
32,752 KB
testcase_25 AC 61 ms
32,812 KB
testcase_26 AC 60 ms
32,848 KB
testcase_27 AC 58 ms
32,828 KB
testcase_28 AC 61 ms
32,876 KB
testcase_29 AC 62 ms
32,812 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

// #include <atcoder/modint>

template <int md> struct ModInt {
#if __cplusplus >= 201402L
#define MDCONST constexpr
#else
#define MDCONST
#endif
    using lint = long long;
    MDCONST static int mod() { return md; }
    static int get_primitive_root() {
        static int primitive_root = 0;
        if (!primitive_root) {
            primitive_root = [&]() {
                std::set<int> fac;
                int v = md - 1;
                for (lint i = 2; i * i <= v; i++)
                    while (v % i == 0) fac.insert(i), v /= i;
                if (v > 1) fac.insert(v);
                for (int g = 1; g < md; g++) {
                    bool ok = true;
                    for (auto i : fac)
                        if (ModInt(g).pow((md - 1) / i) == 1) {
                            ok = false;
                            break;
                        }
                    if (ok) return g;
                }
                return -1;
            }();
        }
        return primitive_root;
    }
    int val;
    MDCONST ModInt() : val(0) {}
    MDCONST ModInt &_setval(lint v) { return val = (v >= md ? v - md : v), *this; }
    MDCONST ModInt(lint v) { _setval(v % md + md); }
    MDCONST explicit operator bool() const { return val != 0; }
    MDCONST ModInt operator+(const ModInt &x) const { return ModInt()._setval((lint)val + x.val); }
    MDCONST ModInt operator-(const ModInt &x) const { return ModInt()._setval((lint)val - x.val + md); }
    MDCONST ModInt operator*(const ModInt &x) const { return ModInt()._setval((lint)val * x.val % md); }
    MDCONST ModInt operator/(const ModInt &x) const { return ModInt()._setval((lint)val * x.inv() % md); }
    MDCONST ModInt operator-() const { return ModInt()._setval(md - val); }
    MDCONST ModInt &operator+=(const ModInt &x) { return *this = *this + x; }
    MDCONST ModInt &operator-=(const ModInt &x) { return *this = *this - x; }
    MDCONST ModInt &operator*=(const ModInt &x) { return *this = *this * x; }
    MDCONST ModInt &operator/=(const ModInt &x) { return *this = *this / x; }
    friend MDCONST ModInt operator+(lint a, const ModInt &x) { return ModInt()._setval(a % md + x.val); }
    friend MDCONST ModInt operator-(lint a, const ModInt &x) { return ModInt()._setval(a % md - x.val + md); }
    friend MDCONST ModInt operator*(lint a, const ModInt &x) { return ModInt()._setval(a % md * x.val % md); }
    friend MDCONST ModInt operator/(lint a, const ModInt &x) {
        return ModInt()._setval(a % md * x.inv() % md);
    }
    MDCONST bool operator==(const ModInt &x) const { return val == x.val; }
    MDCONST bool operator!=(const ModInt &x) const { return val != x.val; }
    MDCONST bool operator<(const ModInt &x) const { return val < x.val; } // To use std::map<ModInt, T>
    friend std::istream &operator>>(std::istream &is, ModInt &x) {
        lint t;
        return is >> t, x = ModInt(t), is;
    }
    MDCONST friend std::ostream &operator<<(std::ostream &os, const ModInt &x) { return os << x.val; }
    MDCONST ModInt pow(lint n) const {
        ModInt ans = 1, tmp = *this;
        while (n) {
            if (n & 1) ans *= tmp;
            tmp *= tmp, n >>= 1;
        }
        return ans;
    }

    static std::vector<ModInt> facs, facinvs, invs;
    MDCONST static void _precalculation(int N) {
        int l0 = facs.size();
        if (N > md) N = md;
        if (N <= l0) return;
        facs.resize(N), facinvs.resize(N), invs.resize(N);
        for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i;
        facinvs[N - 1] = facs.back().pow(md - 2);
        for (int i = N - 2; i >= l0; i--) facinvs[i] = facinvs[i + 1] * (i + 1);
        for (int i = N - 1; i >= l0; i--) invs[i] = facinvs[i] * facs[i - 1];
    }
    MDCONST lint inv() const {
        if (this->val < std::min(md >> 1, 1 << 21)) {
            while (this->val >= int(facs.size())) _precalculation(facs.size() * 2);
            return invs[this->val].val;
        } else {
            return this->pow(md - 2).val;
        }
    }
    MDCONST ModInt fac() const {
        while (this->val >= int(facs.size())) _precalculation(facs.size() * 2);
        return facs[this->val];
    }
    MDCONST ModInt facinv() const {
        while (this->val >= int(facs.size())) _precalculation(facs.size() * 2);
        return facinvs[this->val];
    }
    MDCONST ModInt doublefac() const {
        lint k = (this->val + 1) / 2;
        return (this->val & 1) ? ModInt(k * 2).fac() / (ModInt(2).pow(k) * ModInt(k).fac())
                               : ModInt(k).fac() * ModInt(2).pow(k);
    }
    MDCONST ModInt nCr(const ModInt &r) const {
        return (this->val < r.val) ? 0 : this->fac() * (*this - r).facinv() * r.facinv();
    }
    MDCONST ModInt nPr(const ModInt &r) const {
        return (this->val < r.val) ? 0 : this->fac() * (*this - r).facinv();
    }

    ModInt sqrt() const {
        if (val == 0) return 0;
        if (md == 2) return val;
        if (pow((md - 1) / 2) != 1) return 0;
        ModInt b = 1;
        while (b.pow((md - 1) / 2) == 1) b += 1;
        int e = 0, m = md - 1;
        while (m % 2 == 0) m >>= 1, e++;
        ModInt x = pow((m - 1) / 2), y = (*this) * x * x;
        x *= (*this);
        ModInt z = b.pow(m);
        while (y != 1) {
            int j = 0;
            ModInt t = y;
            while (t != 1) j++, t *= t;
            z = z.pow(1LL << (e - j - 1));
            x *= z, z *= z, y *= z;
            e = j;
        }
        return ModInt(std::min(x.val, md - x.val));
    }
};
template <int md> std::vector<ModInt<md>> ModInt<md>::facs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::facinvs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::invs = {0};
using mint1 = ModInt<998244353>;
using mint2 = ModInt<1000000007>;

#include <atcoder/math>

template <class Mint> Mint A107984(Mint n, Mint k) { return (k + 1) * (n + 2) * (2 * n - k + 3) * (n - k + 1) / 6; }

// 右に r ステップ進むより左に l ステップ進む方が先の確率 * (そのようなときにそれを達成するまでの時間の条件付き期待値)
template <class Mint> Mint solve(int l, int r) {
    int N = l + r;
    return A107984<Mint>(N - 2, l - 1) * 2 / N / N;
}

template <class Mint> Mint sol(const string &S) {
    const int N = S.size();
    vector<int> zeropos;
    for (int i = 0; i < N; i++) {
        if (S[i] == '1') continue;
        zeropos.push_back(i);
    }
    Mint ret = solve<Mint>(N - zeropos[0], zeropos[0]) + solve<Mint>(zeropos.back(), N - zeropos.back());

    for (int i = 1; i < int(zeropos.size()); ++i) {
        int a = zeropos[i - 1], b = zeropos[i];
        for (int t = 0; t < 2; ++t) {
            Mint e1 = solve<Mint>(a, N - b);
            Mint e2 = solve<Mint>(N - (b - a), b - a);
            ret += e1 * (b - a) / N + e2 * (N - b) / (N - b + a);
            swap(a, b);
            a = N - a;
            b = N - b;
        }
    }
    return ret;
}

// using mint1 = atcoder::modint998244353;
// using mint2 = atcoder::modint1000000007;

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N;
    string S;
    cin >> N >> S;
    auto v1 = sol<mint1>(S).val;
    auto v2 = sol<mint2>(S).val;
    cout << atcoder::crt({v1, v2}, {mint1::mod(), mint2::mod()}).first << '\n';
}

0