結果

問題 No.2396 等差二項展開
ユーザー KumaTachiRenKumaTachiRen
提出日時 2023-07-29 19:28:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 6,000 ms
コード長 3,151 bytes
コンパイル時間 5,332 ms
コンパイル使用メモリ 267,248 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 23:40:31
合計ジャッジ時間 7,217 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 21 ms
5,376 KB
testcase_17 AC 45 ms
5,376 KB
testcase_18 AC 52 ms
5,376 KB
testcase_19 AC 55 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 52 ms
5,376 KB
testcase_24 AC 53 ms
5,376 KB
testcase_25 AC 53 ms
5,376 KB
testcase_26 AC 53 ms
5,376 KB
testcase_27 AC 53 ms
5,376 KB
testcase_28 AC 53 ms
5,376 KB
testcase_29 AC 53 ms
5,376 KB
testcase_30 AC 53 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

struct Fast {
  Fast() {
    std::cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(10);
  }
} fast;

#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (a); i--)

using ll = long long;

using mint = dynamic_modint<0>;

template <class T>
T extgcd(T a, T b, T& x, T& y) {
  for (T u = y = 1, v = x = 0; a;) {
    T q = b / a;
    swap(x -= q * u, u);
    swap(y -= q * v, v);
    swap(b -= q * a, a);
  }
  return b;
}
template <class T>
T mod_inv(T a, T m) {
  T x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}
ll mod_pow(ll a, ll n, ll mod) {
  ll ret = 1;
  ll p = a % mod;
  while (n) {
    if (n & 1) ret = ret * p % mod;
    p = p * p % mod;
    n >>= 1;
  }
  return ret;
}

template <class mint>
vector<mint> convolution_int32mod(vector<mint> a, vector<mint> b) {
  const ll m1 = 167772161;
  const ll m2 = 469762049;
  const ll m3 = 1224736769;
  const int mod = mint::mod();
  vector<ll> u(a.size()), v(b.size());
  for (int i = 0; i < (int)a.size(); i++) u[i] = a[i].val();
  for (int i = 0; i < (int)b.size(); i++) v[i] = b[i].val();
  auto x = convolution<m1>(u, v);
  auto y = convolution<m2>(u, v);
  auto z = convolution<m3>(u, v);

  const ll m1_inv_m2 = mod_inv<ll>(m1, m2);
  const ll m12_inv_m3 = mod_inv<ll>(m1 * m2, m3);
  const ll m12_mod = m1 * m2 % mod;
  int n = (int)x.size();
  vector<mint> ret(n);
  for (int i = 0; i < n; i++) {
    ll v1 = (y[i] - x[i]) * m1_inv_m2 % m2;
    if (v1 < 0) v1 += m2;
    ll v2 = (z[i] - (x[i] + m1 * v1) % m3) * m12_inv_m3 % m3;
    if (v2 < 0) v2 += m3;
    ll v3 = (x[i] + m1 * v1 + m12_mod * v2) % mod;
    if (v3 < 0) v3 += mod;
    ret[i] = v3;
  }
  return ret;
}

template <int mod>
vector<ll> convolution_int32mod(vector<ll> a, vector<ll> b) {
  using mint = static_modint<mod>;
  vector<mint> u(a.size()), v(b.size());
  for (int i = 0; i < (int)a.size(); i++) u[i] = a[i];
  for (int i = 0; i < (int)b.size(); i++) v[i] = b[i];
  auto w = convolution_int32mod(u, v);
  int n = (int)w.size();
  vector<ll> c(n);
  for (int i = 0; i < n; i++) c[i] = w[i].val();
  return c;
}

template <typename mint>
mint bostan_mori(vector<mint> a, vector<mint> b, ll n) {
  if (a.size() < b.size()) a.resize(b.size());
  auto c = vector<mint>(b.size());
  while (n) {
    rep(i, 0, b.size()) c[i] = i % 2 == 0 ? b[i] : -b[i];
    auto a1 = convolution_int32mod(a, c), b1 = convolution_int32mod(b, c);
    int r = (int)(n % 2);
    for (int i = 0; i < a.size() && i * 2 + r < a1.size(); i++)
      a[i] = a1[i * 2 + r];
    for (int i = 0; i < b.size(); i++) b[i] = b1[i * 2];
    n /= 2;
  }
  return a[0] / b[0];
};

int main() {
  ll n, m, l, k, mod;
  cin >> n >> m >> l >> k >> mod;

  if (mod == 1) {
    cout << 0 << endl;
    return 0;
  }

  mint::set_mod((int)mod);

  vector<mint> a(l, 0), b(l + 1, 0);
  a[k] = 1;
  rep(i, 0, l - (k + 1)) rrep(j, 1, l) a[j] -= a[j - 1];
  b[0] = 1;
  rep(i, 0, l) rrep(j, 1, l + 1) b[j] -= b[j - 1];
  b[l] -= m;
  cout << bostan_mori(a, b, n).val() << endl;
}
0