結果
問題 | No.1847 Good Sequence |
ユーザー | 👑 rin204 |
提出日時 | 2024-05-05 16:39:31 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 4 ms / 3,000 ms |
コード長 | 11,364 bytes |
コンパイル時間 | 3,286 ms |
コンパイル使用メモリ | 263,564 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-27 15:45:03 |
合計ジャッジ時間 | 4,588 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 3 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 3 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 2 ms
5,248 KB |
testcase_12 | AC | 2 ms
5,248 KB |
testcase_13 | AC | 2 ms
5,248 KB |
testcase_14 | AC | 2 ms
5,248 KB |
testcase_15 | AC | 2 ms
5,248 KB |
testcase_16 | AC | 3 ms
5,248 KB |
testcase_17 | AC | 2 ms
5,248 KB |
testcase_18 | AC | 2 ms
5,248 KB |
testcase_19 | AC | 2 ms
5,248 KB |
testcase_20 | AC | 2 ms
5,248 KB |
testcase_21 | AC | 4 ms
5,248 KB |
testcase_22 | AC | 3 ms
5,248 KB |
testcase_23 | AC | 2 ms
5,248 KB |
testcase_24 | AC | 2 ms
5,248 KB |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 3 ms
5,248 KB |
testcase_29 | AC | 2 ms
5,248 KB |
testcase_30 | AC | 2 ms
5,248 KB |
testcase_31 | AC | 3 ms
5,248 KB |
testcase_32 | AC | 2 ms
5,248 KB |
testcase_33 | AC | 3 ms
5,248 KB |
testcase_34 | AC | 2 ms
5,248 KB |
testcase_35 | AC | 2 ms
5,248 KB |
testcase_36 | AC | 3 ms
5,248 KB |
testcase_37 | AC | 3 ms
5,248 KB |
testcase_38 | AC | 2 ms
5,248 KB |
testcase_39 | AC | 2 ms
5,248 KB |
testcase_40 | AC | 4 ms
5,248 KB |
ソースコード
#define PROBLEM "https://yukicoder.me/problems/no/1847" #include <bits/stdc++.h> using namespace std; template <int MOD> struct Modint { int x; Modint() : x(0) {} Modint(int64_t y) { if (y >= 0) x = y % MOD; else x = (y % MOD + MOD) % MOD; } Modint &operator+=(const Modint &p) { x += p.x; if (x >= MOD) x -= MOD; return *this; } Modint &operator-=(const Modint &p) { x -= p.x; if (x < 0) x += MOD; return *this; } Modint &operator*=(const Modint &p) { x = int(1LL * x * p.x % MOD); return *this; } Modint &operator/=(const Modint &p) { *this *= p.inverse(); return *this; } Modint &operator%=(const Modint &p) { assert(p.x == 0); return *this; } Modint operator-() const { return Modint(-x); } Modint &operator++() { x++; if (x == MOD) x = 0; return *this; } Modint &operator--() { if (x == 0) x = MOD; x--; return *this; } Modint operator++(int) { Modint result = *this; ++*this; return result; } Modint operator--(int) { Modint result = *this; --*this; return result; } friend Modint operator+(const Modint &lhs, const Modint &rhs) { return Modint(lhs) += rhs; } friend Modint operator-(const Modint &lhs, const Modint &rhs) { return Modint(lhs) -= rhs; } friend Modint operator*(const Modint &lhs, const Modint &rhs) { return Modint(lhs) *= rhs; } friend Modint operator/(const Modint &lhs, const Modint &rhs) { return Modint(lhs) /= rhs; } friend Modint operator%(const Modint &lhs, const Modint &rhs) { assert(rhs.x == 0); return Modint(lhs); } bool operator==(const Modint &p) const { return x == p.x; } bool operator!=(const Modint &p) const { return x != p.x; } bool operator<(const Modint &rhs) const { return x < rhs.x; } bool operator<=(const Modint &rhs) const { return x <= rhs.x; } bool operator>(const Modint &rhs) const { return x > rhs.x; } bool operator>=(const Modint &rhs) const { return x >= rhs.x; } Modint inverse() const { int a = x, b = MOD, u = 1, v = 0, t; while (b > 0) { t = a / b; a -= t * b; u -= t * v; std::swap(a, b); std::swap(u, v); } return Modint(u); } Modint pow(int64_t k) const { Modint ret(1); Modint y(x); while (k > 0) { if (k & 1) ret *= y; y *= y; k >>= 1; } return ret; } friend std::ostream &operator<<(std::ostream &os, const Modint &p) { return os << p.x; } friend std::istream &operator>>(std::istream &is, Modint &p) { int64_t y; is >> y; p = Modint<MOD>(y); return (is); } static int get_mod() { return MOD; } }; struct Arbitrary_Modint { int x; static int MOD; static void set_mod(int mod) { MOD = mod; } Arbitrary_Modint() : x(0) {} Arbitrary_Modint(int64_t y) { if (y >= 0) x = y % MOD; else x = (y % MOD + MOD) % MOD; } Arbitrary_Modint &operator+=(const Arbitrary_Modint &p) { x += p.x; if (x >= MOD) x -= MOD; return *this; } Arbitrary_Modint &operator-=(const Arbitrary_Modint &p) { x -= p.x; if (x < 0) x += MOD; return *this; } Arbitrary_Modint &operator*=(const Arbitrary_Modint &p) { x = int(1LL * x * p.x % MOD); return *this; } Arbitrary_Modint &operator/=(const Arbitrary_Modint &p) { *this *= p.inverse(); return *this; } Arbitrary_Modint &operator%=(const Arbitrary_Modint &p) { assert(p.x == 0); return *this; } Arbitrary_Modint operator-() const { return Arbitrary_Modint(-x); } Arbitrary_Modint &operator++() { x++; if (x == MOD) x = 0; return *this; } Arbitrary_Modint &operator--() { if (x == 0) x = MOD; x--; return *this; } Arbitrary_Modint operator++(int) { Arbitrary_Modint result = *this; ++*this; return result; } Arbitrary_Modint operator--(int) { Arbitrary_Modint result = *this; --*this; return result; } friend Arbitrary_Modint operator+(const Arbitrary_Modint &lhs, const Arbitrary_Modint &rhs) { return Arbitrary_Modint(lhs) += rhs; } friend Arbitrary_Modint operator-(const Arbitrary_Modint &lhs, const Arbitrary_Modint &rhs) { return Arbitrary_Modint(lhs) -= rhs; } friend Arbitrary_Modint operator*(const Arbitrary_Modint &lhs, const Arbitrary_Modint &rhs) { return Arbitrary_Modint(lhs) *= rhs; } friend Arbitrary_Modint operator/(const Arbitrary_Modint &lhs, const Arbitrary_Modint &rhs) { return Arbitrary_Modint(lhs) /= rhs; } friend Arbitrary_Modint operator%(const Arbitrary_Modint &lhs, const Arbitrary_Modint &rhs) { assert(rhs.x == 0); return Arbitrary_Modint(lhs); } bool operator==(const Arbitrary_Modint &p) const { return x == p.x; } bool operator!=(const Arbitrary_Modint &p) const { return x != p.x; } bool operator<(const Arbitrary_Modint &rhs) { return x < rhs.x; } bool operator<=(const Arbitrary_Modint &rhs) { return x <= rhs.x; } bool operator>(const Arbitrary_Modint &rhs) { return x > rhs.x; } bool operator>=(const Arbitrary_Modint &rhs) { return x >= rhs.x; } Arbitrary_Modint inverse() const { int a = x, b = MOD, u = 1, v = 0, t; while (b > 0) { t = a / b; a -= t * b; u -= t * v; std::swap(a, b); std::swap(u, v); } return Arbitrary_Modint(u); } Arbitrary_Modint pow(int64_t k) const { Arbitrary_Modint ret(1); Arbitrary_Modint y(x); while (k > 0) { if (k & 1) ret *= y; y *= y; k >>= 1; } return ret; } friend std::ostream &operator<<(std::ostream &os, const Arbitrary_Modint &p) { return os << p.x; } friend std::istream &operator>>(std::istream &is, Arbitrary_Modint &p) { int64_t y; is >> y; p = Arbitrary_Modint(y); return (is); } static int get_mod() { return MOD; } }; int Arbitrary_Modint::MOD = 998244353; using modint9 = Modint<998244353>; using modint1 = Modint<1000000007>; using modint = Arbitrary_Modint; template <typename T> T modinv(T a, T MOD) { T b = MOD; T u = 1; T v = 0; while (b > 0) { T t = a / b; a -= t * b; u -= t * v; std::swap(a, b); std::swap(u, v); } if (a != 1) return -1; if (u < 0) u += MOD; return u; } template <typename T> std::vector<T> berlekampMessy(const std::vector<T> &A) { int n = A.size(); std::vector<T> B(1, -1); std::vector<T> C(1, -1); T y = 1; for (int j = 1; j <= n; j++) { int l = C.size(); int m = B.size(); T x = 0; for (int i = 0; i < l; i++) { x += C[i] * A[j - l + i]; } B.push_back(0); m++; if (x == 0) continue; T freq = x / y; if (l < m) { std::vector<T> D(m - l, T(0)); D.insert(D.end(), C.begin(), C.end()); for (int i = 0; i < m; i++) { D[m - 1 - i] -= freq * B[m - 1 - i]; } std::swap(B, C); std::swap(C, D); y = x; } else { for (int i = 0; i < m; i++) { C[l - 1 - i] -= freq * B[m - 1 - i]; } } } std::reverse(C.begin(), C.end()); for (auto &c : C) c = -c; return C; } /* return x^n mod f 引数: f_reversed: f(x)の係数(逆順, f_reversed[0] = 1) */ template <typename T> std::vector<T> monomial_mod_polynomial(long long n, const std::vector<T> &f_reversed) { assert(!f_reversed.empty() and f_reversed[0] == 1); int K = f_reversed.size() - 1; if (!K) return {}; int D = 64 - __builtin_clzll(n); std::vector<T> ret(K, 0); ret[0] = 1; auto self_conv = [](std::vector<T> &x) -> std::vector<T> { int d = x.size(); std::vector<T> ret(2 * d - 1); for (int i = 0; i < d; i++) { ret[2 * i] += x[i] * x[i]; for (int j = 0; j < i; j++) ret[i + j] += 2 * x[i] * x[j]; } return ret; }; for (int d = D - 1; d >= 0; d--) { ret = self_conv(ret); for (int i = 2 * K - 2; i >= K; i--) { for (int j = 1; j <= K; j++) { ret[i - j] -= ret[i] * f_reversed[j]; } } ret.resize(K); if ((n >> d) & 1) { std::vector<T> c(K); c[0] = -ret[K - 1] * f_reversed[K]; for (int i = 1; i < K; i++) { c[i] = ret[i - 1] - ret[K - 1] * f_reversed[K - i]; } ret = c; } } return ret; } template <typename T> T guess_kth_term(const std::vector<T> &A, long long k, bool debug = false) { assert(k >= 0); if (k < int(A.size())) return A[k]; const auto F = berlekampMessy(A); if (debug) { std::cerr << "F.size() = " << F.size() << std::endl; } const auto G = monomial_mod_polynomial<T>(k, F); T ret = 0; for (size_t i = 0; i < G.size(); i++) ret += G[i] * A[i]; return ret; } using mint = modint1; void solve() { long long l, n, m; cin >> l >> n >> m; const int N = 1000; vector<bool> B(n + 1, false); for (int i = 0; i < m; i++) { int b; cin >> b; B[b] = true; } vector<vector<mint>> dp(N, vector<mint>(n + 1)); dp[0][0] = 1; for (int i = 1; i < N; i++) { for (int j = 1; j <= n; j++) { for (int k = 0; k <= n; k++) { dp[i][j] += dp[i - 1][k]; } if (B[j] and i >= j) { for (int k = 0; k <= n; k++) { if (k != j) { dp[i][j] -= dp[i - j][k]; } } if (i >= j + 1) { for (int k = 0; k <= n; k++) { if (k != j) { dp[i][j] += dp[i - j - 1][k]; } } } } } } vector<mint> F(N - 10); mint all_ = 1; for (int i = 0; i < N - 10; i++) { for (int j = 0; j <= n; j++) { F[i] += dp[i][j]; } F[i] = all_ - F[i]; all_ *= n; } auto res = guess_kth_term(F, l, true); cout << res << endl; } int main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(12); int t; t = 1; // cin >> t; while (t--) solve(); return 0; }