結果

問題 No.2396 等差二項展開
ユーザー Focus_SashFocus_Sash
提出日時 2023-08-01 22:31:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,417 ms / 6,000 ms
コード長 3,525 bytes
コンパイル時間 2,310 ms
コンパイル使用メモリ 201,716 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 23:26:45
合計ジャッジ時間 14,143 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 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 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 13 ms
5,376 KB
testcase_16 AC 167 ms
5,376 KB
testcase_17 AC 737 ms
5,376 KB
testcase_18 AC 1,187 ms
5,376 KB
testcase_19 AC 1,417 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 616 ms
5,376 KB
testcase_24 AC 968 ms
5,376 KB
testcase_25 AC 1,170 ms
5,376 KB
testcase_26 AC 860 ms
5,376 KB
testcase_27 AC 966 ms
5,376 KB
testcase_28 AC 1,056 ms
5,376 KB
testcase_29 AC 966 ms
5,376 KB
testcase_30 AC 697 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

namespace util {
using ll = long long;
using vl = std::vector<long long>;
using pl = std::pair<long long, long long>;

constexpr long long kInf = std::numeric_limits<long long>::max() / 8;
constexpr long long kMax = std::numeric_limits<long long>::max();

template <typename T, typename U>
inline bool UpdateMax(T &x, const U &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T, typename U>
inline bool UpdateMin(T &x, const U &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

// verified
inline long long Pow(long long x, long long n) {
  assert(n >= 0);
  if (x == 0) return 0;
  long long res = 1LL;
  while (n > 0) {
    if (n & 1) {
      assert(x != 0 && std::abs(res) <= kMax / std::abs(x));
      res = res * x;
    }
    if (n >>= 1) {
      assert(x != 0 && std::abs(x) <= kMax / std::abs(x));
      x = x * x;
    }
  }
  return res;
}

// verified
inline long long Mod(long long n, const long long m) {
  // returns the "arithmetic modulo"
  // for a pair of integers (n, m) with m != 0, there exists a unique pair of
  // integer (q, r) s.t. n = qm + r and 0 <= r < |m| returns this r
  assert(m != 0);
  if (m < 0) return Mod(n, -m);
  if (n >= 0)
    return n % m;
  else
    return (m + n % m) % m;
}

inline long long Quotient(long long n, long long m) {
  // returns the "arithmetic quotient"
  assert((n - Mod(n, m)) % m == 0);
  return (n - Mod(n, m)) / m;
}

inline long long DivFloor(long long n, long long m) {
  // returns floor(n / m)
  assert(m != 0);
  if (m < 0) {
    n = -n;
    m = -m;
  }
  if (n >= 0)
    return n / m;
  else if (n % m == 0)
    return -(abs(n) / m);
  else
    return -(abs(n) / m) - 1;
}

inline long long DivCeil(long long n, long long m) {
  // returns ceil(n / m)
  assert(m != 0);
  if (n % m == 0)
    return DivFloor(n, m);
  else
    return DivFloor(n, m) + 1;
}

template <typename T>
inline T Sum(const std::vector<T> &vec) {
  return std::accumulate(vec.begin(), vec.end(), T(0));
}
}  // namespace util
using namespace util;

inline long long PowMod(long long x, long long n, const long long m) {
  assert(n >= 0);
  assert(m != 0);
  if (x == 0) return 0;
  long long res = 1;
  x = Mod(x, m);
  while (n > 0) {
    if (n & 1) {
      assert(x == 0 || std::abs(res) <= kMax / std::abs(x));
      res = Mod(res * x, m);
    }
    if (n >>= 1) {
      assert(x == 0 || std::abs(x) <= kMax / std::abs(x));
      x = Mod(x * x, m);
    }
  }
  return res;
}

void solve() {
  ll n, m, l, k, b;
  cin >> n >> m >> l >> k >> b;
  m %= b;

  if (l == 1) {
    cout << PowMod(m + 1, n, b) << '\n';
    return;
  }
  if (b == 1) {
  }

  auto Convolution = [&](vl f, vl g) {
    vl res(l, 0);
    for (ll i = 0; i < l; i++) {
      for (ll j = 0; j < l; j++) {
        if (i + j < l) {
          res[i + j] += f[i] * g[j];
          res[i + j] %= b;
        } else {
          res[i + j - l] += f[i] * g[j] % b * m;
          res[i + j - l] %= b;
        }
      }
    }
    return res;
  };
  vl f(l, 0);
  f[0] += 1;
  f[0] %= b;
  if (l == 1) {
    f[0] += m;
    f[0] %= b;
  } else {
    f[1] += 1;
    f[1] %= b;
  }
  vl g(l, 0);
  g[0] = 1;
  g[0] %= b;
  while (n > 0) {
    if (n % 2 == 1) {
      g = Convolution(g, f);
    }
    f = Convolution(f, f);
    n /= 2;
  }
  cout << g[k] << '\n';
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);

  solve();

  return 0;
}
0