結果
| 問題 |
No.1840 Random Painting
|
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2021-12-12 23:58:11 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 416 ms / 2,000 ms |
| コード長 | 7,397 bytes |
| コンパイル時間 | 1,672 ms |
| コンパイル使用メモリ | 125,536 KB |
| 実行使用メモリ | 41,876 KB |
| 最終ジャッジ日時 | 2024-06-27 15:57:18 |
| 合計ジャッジ時間 | 7,872 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
#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';
}
hitonanode