結果
問題 | No.2068 Restricted Permutation |
ユーザー | 👑 rin204 |
提出日時 | 2022-09-02 22:02:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 148 ms / 2,000 ms |
コード長 | 2,381 bytes |
コンパイル時間 | 314 ms |
コンパイル使用メモリ | 82,396 KB |
実行使用メモリ | 126,848 KB |
最終ジャッジ日時 | 2024-11-16 03:29:56 |
合計ジャッジ時間 | 4,390 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 93 ms
91,264 KB |
testcase_01 | AC | 94 ms
91,136 KB |
testcase_02 | AC | 94 ms
91,264 KB |
testcase_03 | AC | 98 ms
91,136 KB |
testcase_04 | AC | 93 ms
91,136 KB |
testcase_05 | AC | 93 ms
91,008 KB |
testcase_06 | AC | 96 ms
91,264 KB |
testcase_07 | AC | 94 ms
91,392 KB |
testcase_08 | AC | 94 ms
91,392 KB |
testcase_09 | AC | 95 ms
91,008 KB |
testcase_10 | AC | 96 ms
91,136 KB |
testcase_11 | AC | 93 ms
91,136 KB |
testcase_12 | AC | 95 ms
91,136 KB |
testcase_13 | AC | 105 ms
96,512 KB |
testcase_14 | AC | 100 ms
94,208 KB |
testcase_15 | AC | 101 ms
94,336 KB |
testcase_16 | AC | 131 ms
117,332 KB |
testcase_17 | AC | 148 ms
126,720 KB |
testcase_18 | AC | 140 ms
126,208 KB |
testcase_19 | AC | 143 ms
126,592 KB |
testcase_20 | AC | 141 ms
126,592 KB |
testcase_21 | AC | 144 ms
126,464 KB |
testcase_22 | AC | 143 ms
126,464 KB |
testcase_23 | AC | 147 ms
126,848 KB |
testcase_24 | AC | 123 ms
114,372 KB |
testcase_25 | AC | 120 ms
114,432 KB |
ソースコード
# https://atcoder.jp/contests/code-festival-2016-qualc/submissions/33924036 from bisect import bisect class Bit: def __init__(self, n): self.size = n self.n0 = 1 << (n.bit_length() - 1) self.tree = [0] * (n + 1) def range_sum(self, l, r): return self.sum(r - 1) - self.sum(l - 1) def sum(self, i): i += 1 s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def get(self, i): return self.sum(i) - self.sum(i - 1) def add(self, i, x): i += 1 while i <= self.size: self.tree[i] += x i += i & -i def lower_bound(self, x): pos = 0 plus = self.n0 while plus > 0: if pos + plus <= self.size and self.tree[pos + plus] < x: x -= self.tree[pos + plus] pos += plus plus //= 2 return pos MOD = 998244353 N = 2000000 fact = [0 for _ in range(N)] invfact = [0 for _ in range(N)] fact[0] = 1 for i in range(1, N): fact[i] = fact[i - 1] * i % MOD invfact[N - 1] = pow(fact[N - 1], MOD - 2, MOD) for i in range(N - 2, -1, -1): invfact[i] = invfact[i + 1] * (i + 1) % MOD def nCk(n, k): if k < 0 or n < k: return 0 else: return (fact[n] * invfact[k] % MOD) * invfact[n - k] % MOD def nPk(n, k): if k < 0 or n < k: return 0 else: return fact[n] * invfact[n - k] % MOD def nHk(n, k): if n == k == 0: return 1 return nCk(n + k - 1, k) n, k, x = map(int, input().split()) P = [0] * n P[k - 1] = x P = P[::-1] tf = [False] * (n + 1) ind = [] for i, p in enumerate(P): tf[p] = True lst = [] for i in range(1, n + 1): if not tf[i]: lst.append(i) inv2 = pow(2, MOD - 2, MOD) ans = 0 l = len(lst) invl = pow(l, MOD - 2, MOD) bit = Bit(n + 1) x = 0 t = 0 for i, p in enumerate(P): if p == 0: ans += (x * fact[i]) * inv2 % MOD ans %= MOD x += 1 ans += (t * fact[i] % MOD) * invl % MOD ans %= MOD else: c = bit.sum(p) ans += fact[i] * c % MOD ans %= MOD bit.add(p, 1) c = bisect(lst, p) ans += (c * invl % MOD) * (fact[i] * x % MOD) % MOD ans %= MOD t += l - c ans *= fact[l] ans %= MOD print(ans)