結果
問題 | No.802 だいたい等差数列 |
ユーザー | risujiroh |
提出日時 | 2019-03-17 22:28:40 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 5,355 bytes |
コンパイル時間 | 2,106 ms |
コンパイル使用メモリ | 189,232 KB |
実行使用メモリ | 74,452 KB |
最終ジャッジ日時 | 2024-07-08 00:25:20 |
合計ジャッジ時間 | 8,251 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | TLE | - |
testcase_01 | -- | - |
testcase_02 | -- | - |
testcase_03 | -- | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
ソースコード
#include <bits/stdc++.h> using namespace std; using lint = long long; template<class T = int> using V = vector<T>; template<class T = int> using VV = V< V<T> >; template<unsigned P> struct ModInt { using M = ModInt; unsigned v; ModInt() : v(0) {} template<class Z> ModInt(Z x) : v(x >= 0 ? x % P : -x % P ? P - -x % P : 0) {} constexpr ModInt(unsigned v, int) : v(v) {} static constexpr unsigned p() { return P; } M operator+() const { return *this; } M operator-() const { return {v ? P - v : 0, 0}; } explicit operator bool() const noexcept { return v; } bool operator!() const noexcept { return !(bool) *this; } M operator*(M r) const { return M(*this) *= r; } M operator/(M r) const { return M(*this) /= r; } M operator+(M r) const { return M(*this) += r; } M operator-(M r) const { return M(*this) -= r; } bool operator==(M r) const { return v == r.v; } bool operator!=(M r) const { return !(*this == r); } M& operator*=(M r) { v = (uint64_t) v * r.v % P; return *this; } M& operator/=(M r) { return *this *= r.inv(); } M& operator+=(M r) { v = r.v < P - v ? v + r.v : v - (P - r.v); return *this; } M& operator-=(M r) { v = r.v <= v ? v - r.v : v + (P - r.v); return *this; } M inv() const { int a = v, b = P, x = 1, u = 0; while (b) { int q = a / b; swap(a -= q * b, b); swap(x -= q * u, u); } assert(a == 1); return x; } template<class Z> M pow(Z n) const { n = n >= 0 ? n % (P - 1) : P - 1 - -n % (P - 1); M res = 1; for (M a = *this; n; a *= a, n >>= 1) if (n & 1) res *= a; return res; } template<class Z> friend M operator*(Z l, M r) { return M(l) *= r; } template<class Z> friend M operator/(Z l, M r) { return M(l) /= r; } template<class Z> friend M operator+(Z l, M r) { return M(l) += r; } template<class Z> friend M operator-(Z l, M r) { return M(l) -= r; } friend ostream& operator<<(ostream& os, M r) { return os << r.v; } friend istream& operator>>(istream& is, M& r) { lint x; is >> x; r = x; return is; } template<class Z> friend bool operator==(Z l, M r) { return M(l) == r; } template<class Z> friend bool operator!=(Z l, M r) { return !(l == r); } }; using Mint = ModInt<(unsigned) 1e9 + 7>; template<unsigned P, unsigned g> void dft(V< ModInt<P> >& a, bool inv = false) { int n = a.size(); int j = 0; for (int i = 1; i < n; ++i) { int k = n >> 1; while (j >= k) j -= k, k >>= 1; j += k; if (i < j) swap(a[i], a[j]); } assert((P - 1) % n == 0); auto zeta = ModInt<P>(g).pow((P - 1) / n); if (inv) zeta = zeta.inv(); V< ModInt<P> > xi(n + 1, 1); for (int i = 0; i < n; ++i) xi[i + 1] = xi[i] * zeta; for (int k = 1; k < n; k <<= 1) { for (int i0 = 0; i0 < n; i0 += k << 1) { for (int i = i0; i < i0 + k; ++i) { j = i + k; a[j] *= xi[(n >> 1) / k * (i - i0)]; tie(a[i], a[j]) = make_pair(a[i] + a[j], a[i] - a[j]); } } } } template<unsigned P, unsigned g = 3> void convolution(V< ModInt<P> >& a, V< ModInt<P> >& b) { assert(!a.empty() and !b.empty()); int n = 1 << __lg(2 * (a.size() + b.size() - 1) - 1); a.resize(n), b.resize(n); dft<P, g>(a), dft<P, g>(b); for (int i = 0; i < n; ++i) a[i] *= b[i]; dft<P, g>(a, true); auto inv_n = ModInt<P>(n).inv(); for (int i = 0; i < n; ++i) a[i] *= inv_n; } lint tmod(lint a, lint p) { return (a %= p) < 0 ? a + p : a; } lint mod_inv(lint a, lint p) { a = tmod(a, p); lint b = p, x = 1, u = 0; while (b) { lint q = a / b; swap(a -= q * b, b); swap(x -= q * u, u); } return a == 1 ? tmod(x, p) : -1; } lint CRT(const V<lint>& a, const V<lint>& p, lint mod) { int n = a.size(); V<lint> y(n); for (int i = 0; i < n; ++i) { y[i] = a[i]; lint prod = 1; for (int j = 0; j < i; ++j) { y[i] -= prod * y[j] % p[i]; (prod *= p[j]) %= p[i]; } y[i] = tmod(y[i], p[i]); for (int j = 0; j < i; ++j) { (y[i] *= mod_inv(p[j], p[i])) %= p[i]; } } lint res = 0, prod = 1; for (int i = 0; i < n; ++i) { res += prod * y[i] % mod; (prod *= p[i]) %= mod; } return res % mod; } void convolution(V<lint>& a, V<lint> b) { using Mint0 = ModInt<998244353>; using Mint1 = ModInt<1004535809>; int n = a.size(), m = b.size(); V<Mint0> a0(n), b0(m); V<Mint1> a1(n), b1(m); for (int i = 0; i < n; ++i) a0[i] = a[i], a1[i] = a[i]; for (int j = 0; j < m; ++j) b0[j] = b[j], b1[j] = b[j]; convolution(a0, b0); convolution(a1, b1); n = a0.size(); a.resize(n); for (int i = 0; i < n; ++i) { a[i] = CRT({a0[i].v, a1[i].v}, {Mint0::p(), Mint1::p()}, Mint::p()); } // auto inv = Mint1(Mint0::p()).inv(); // for (int i = 0; i < n; ++i) { // a[i] = a0[i].v + (lint) Mint0::p() * ((a1[i] - a0[i].v) * inv).v; // } } V<lint> pow(V<lint> a, int k) { V<lint> res(a.size()); res[0] = 1; while (k) { if (k & 1) convolution(res, a); convolution(a, a); k >>= 1; } return res; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m, d0, d1; cin >> n >> m >> d0 >> d1; m -= d0 * (n - 1); if (m <= 0) return cout << 0 << '\n', 0; V<lint> a(m); for (int i = 0; i <= d1 - d0 and i < m; ++i) { a[i] = 1; } auto dp = pow(a, n - 1); Mint res; for (int s = 0; s < m; ++s) { res += Mint(dp[s]) * (m - s); } cout << res << '\n'; }