結果

問題 No.802 だいたい等差数列
ユーザー risujirohrisujiroh
提出日時 2020-01-09 23:31:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,525 ms / 2,000 ms
コード長 10,504 bytes
コンパイル時間 2,309 ms
コンパイル使用メモリ 188,956 KB
実行使用メモリ 280,852 KB
最終ジャッジ日時 2024-11-23 05:03:55
合計ジャッジ時間 20,284 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 4 ms
5,248 KB
testcase_02 AC 4 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 4 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 3 ms
5,248 KB
testcase_07 AC 3 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 4 ms
5,248 KB
testcase_10 AC 1,407 ms
277,876 KB
testcase_11 AC 1,393 ms
280,752 KB
testcase_12 AC 691 ms
142,168 KB
testcase_13 AC 1,441 ms
280,208 KB
testcase_14 AC 1,398 ms
277,756 KB
testcase_15 AC 666 ms
140,616 KB
testcase_16 AC 1,439 ms
278,084 KB
testcase_17 AC 2 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 689 ms
143,124 KB
testcase_20 AC 1,446 ms
278,664 KB
testcase_21 AC 1,470 ms
280,852 KB
testcase_22 AC 1,525 ms
280,796 KB
testcase_23 AC 167 ms
41,248 KB
testcase_24 AC 2 ms
6,816 KB
testcase_25 AC 2 ms
6,820 KB
testcase_26 AC 8 ms
6,912 KB
testcase_27 AC 7 ms
6,820 KB
testcase_28 AC 684 ms
142,312 KB
testcase_29 AC 1,472 ms
280,848 KB
testcase_30 AC 2 ms
6,816 KB
testcase_31 AC 2 ms
6,816 KB
testcase_32 AC 2 ms
6,816 KB
testcase_33 AC 681 ms
139,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T> vector<T> operator-(vector<T> a) {
  for (auto&& e : a) e = -e;
  return a;
}
template <class T> vector<T>& operator+=(vector<T>& l, const vector<T>& r) {
  l.resize(max(l.size(), r.size()));
  for (int i = 0; i < (int)r.size(); ++i) l[i] += r[i];
  return l;
}
template <class T> vector<T> operator+(vector<T> l, const vector<T>& r) {
  return l += r;
}
template <class T> vector<T>& operator-=(vector<T>& l, const vector<T>& r) {
  l.resize(max(l.size(), r.size()));
  for (int i = 0; i < (int)r.size(); ++i) l[i] -= r[i];
  return l;
}
template <class T> vector<T> operator-(vector<T> l, const vector<T>& r) {
  return l -= r;
}
template <class T> vector<T>& operator<<=(vector<T>& a, size_t n) {
  return a.insert(begin(a), n, 0), a;
}
template <class T> vector<T> operator<<(vector<T> a, size_t n) {
  return a <<= n;
}
template <class T> vector<T>& operator>>=(vector<T>& a, size_t n) {
  return a.erase(begin(a), begin(a) + min(a.size(), n)), a;
}
template <class T> vector<T> operator>>(vector<T> a, size_t n) {
  return a >>= n;
}
template <class T> vector<T> operator*(const vector<T>& l, const vector<T>& r) {
  if (l.empty() or r.empty()) return {};
  vector<T> res(l.size() + r.size() - 1);
  for (int i = 0; i < (int)l.size(); ++i)
    for (int j = 0; j < (int)r.size(); ++j) res[i + j] += l[i] * r[j];
  return res;
}
template <class T> vector<T>& operator*=(vector<T>& l, const vector<T>& r) {
  return l = l * r;
}
template <class T> vector<T> inverse(const vector<T>& a) {
  assert(not a.empty() and not (a[0] == 0));
  vector<T> b{1 / a[0]};
  while (b.size() < a.size()) {
    vector<T> x(begin(a), begin(a) + min(a.size(), 2 * b.size()));
    x *= b * b;
    b.resize(2 * b.size());
    for (auto i = b.size() / 2; i < min(x.size(), b.size()); ++i) b[i] = -x[i];
  }
  return {begin(b), begin(b) + a.size()};
}
template <class T> vector<T> operator/(vector<T> l, vector<T> r) {
  if (l.size() < r.size()) return {};
  reverse(begin(l), end(l)), reverse(begin(r), end(r));
  int n = l.size() - r.size() + 1;
  l.resize(n), r.resize(n);
  l *= inverse(r);
  return {rend(l) - n, rend(l)};
}
template <class T> vector<T>& operator/=(vector<T>& l, const vector<T>& r) {
  return l = l / r;
}
template <class T> vector<T> operator%(vector<T> l, const vector<T>& r) {
  if (l.size() < r.size()) return l;
  l -= l / r * r;
  return {begin(l), begin(l) + (r.size() - 1)};
}
template <class T> vector<T>& operator%=(vector<T>& l, const vector<T>& r) {
  return l = l % r;
}
template <class T> vector<T> derivative(const vector<T>& a) {
  vector<T> res(max((int)a.size() - 1, 0));
  for (int i = 0; i < (int)res.size(); ++i) res[i] = (i + 1) * a[i + 1];
  return res;
}
template <class T> vector<T> primitive(const vector<T>& a) {
  vector<T> res(a.size() + 1);
  for (int i = 1; i < (int)res.size(); ++i) res[i] = a[i - 1] / i;
  return res;
}
template <class T> vector<T> logarithm(const vector<T>& a) {
  assert(not a.empty() and a[0] == 1);
  auto res = primitive(derivative(a) * inverse(a));
  return {begin(res), begin(res) + a.size()};
}
template <class T> vector<T> exponent(const vector<T>& a) {
  assert(a.empty() or a[0] == 0);
  vector<T> b{1};
  while (b.size() < a.size()) {
    vector<T> x(begin(a), begin(a) + min(a.size(), 2 * b.size()));
    x[0] += 1;
    b.resize(2 * b.size());
    x -= logarithm(b);
    x *= {begin(b), begin(b) + b.size() / 2};
    for (auto i = b.size() / 2; i < min(x.size(), b.size()); ++i) b[i] = x[i];
  }
  return {begin(b), begin(b) + a.size()};
}

namespace fft {
struct C {
  double x, y;
  C(double _x = 0, double _y = 0) : x(_x), y(_y) {}
};
C operator+(C l, C r) { return {l.x + r.x, l.y + r.y}; }
C operator-(C l, C r) { return {l.x - r.x, l.y - r.y}; }
C operator*(C l, C r) { return {l.x * r.x - l.y * r.y, l.x * r.y + l.y * r.x}; }
C operator~(C a) { return {a.x, -a.y}; }
vector<C> w{1};
void ensure(int n) {
  for (int m = w.size(); m < n; m *= 2) {
    C dw{cos(acos(0) / m), sin(acos(0) / m)};
    w.resize(2 * m);
    for (int i = 0; i < m; ++i) w[m + i] = w[i] * dw;
  }
}
void fft(vector<C>& a, int n, bool inverse) {
  assert((n & (n - 1)) == 0);
  ensure(n);
  if (not inverse) {
    for (int m = n; m >>= 1; ) {
      for (int s = 0, k = 0; s < n; s += 2 * m, ++k) {
        for (int i = s, j = s + m; i < s + m; ++i, ++j) {
          C x = a[i], y = a[j] * w[k];
          a[i] = x + y, a[j] = x - y;
        }
      }
    }
  } else {
    for (int m = 1; m < n; m *= 2) {
      for (int s = 0, k = 0; s < n; s += 2 * m, ++k) {
        for (int i = s, j = s + m; i < s + m; ++i, ++j) {
          C x = a[i], y = a[j];
          a[i] = x + y, a[j] = (x - y) * ~w[k];
        }
      }
    }
    double inv = 1.0 / n;
    for (auto&& e : a) e.x *= inv, e.y *= inv;
  }
}
void real_fft(vector<C>& a) {
  if (a.size() < 2) return;
  assert(a.size() % 2 == 0);
  int n = a.size() / 2;
  for (int i = 0; i < n; ++i) a[i] = {a[2 * i].x, a[2 * i + 1].x};
  fft(a, n, false);
  for (int s = n; s >>= 1; ) for (int i = s, j = 2 * s; j-- > s; ++i) {
    C wa((1 + w[i].y) / 2, -w[i].x / 2), wb((1 - w[i].y) / 2, w[i].x / 2);
    a[2 * i] = a[i] * wa + ~a[j] * wb, a[2 * j + 1] = ~a[2 * i];
  }
  a[1] = a[0].x - a[0].y, a[0] = a[0].x + a[0].y;
}
void real_ifft(vector<C>& a) {
  if (a.size() < 2) return;
  assert(a.size() % 2 == 0);
  int n = a.size() / 2;
  for (int i = 0; i < n; ++i) {
    C wa((1 + w[i].y) / 2, w[i].x / 2), wb((1 - w[i].y) / 2, -w[i].x / 2);
    a[i] = a[2 * i] * wa + a[2 * i + 1] * wb;
  }
  fft(a, n, true);
  for (int i = n; i--; ) a[2 * i].x = a[i].x, a[2 * i + 1].x = a[i].y;
}
} // namespace fft

template <class T, class F = multiplies<T>>
T power(T a, long long n, F op = multiplies<T>(), T e = {1}) {
  assert(n >= 0);
  T res = e;
  while (n) {
    if (n & 1) res = op(res, a);
    if (n >>= 1) a = op(a, a);
  }
  return res;
}

template <unsigned Mod> struct Modular {
  using M = Modular;
  unsigned v;
  Modular(long long a = 0) : v((a %= Mod) < 0 ? a + Mod : a) {}
  M operator-() const { return M() -= *this; }
  M& operator+=(M r) { if ((v += r.v) >= Mod) v -= Mod; return *this; }
  M& operator-=(M r) { if ((v += Mod - r.v) >= Mod) v -= Mod; return *this; }
  M& operator*=(M r) { v = (uint64_t)v * r.v % Mod; return *this; }
  M& operator/=(M r) { return *this *= power(r, Mod - 2); }
  friend M operator+(M l, M r) { return l += r; }
  friend M operator-(M l, M r) { return l -= r; }
  friend M operator*(M l, M r) { return l *= r; }
  friend M operator/(M l, M r) { return l /= r; }
  friend bool operator==(M l, M r) { return l.v == r.v; }
};

template <unsigned Mod, size_t K = 2, int B = __lg(Mod) / K + 1>
array<vector<fft::C>, K> mint_fft(const vector<Modular<Mod>>& a, int sz) {
  array<vector<fft::C>, K> res;
  for (size_t p = 0; p < K; ++p) {
    res[p].resize(sz);
    for (int i = 0; i < (int)a.size(); ++i)
      res[p][i] = (a[i].v >> (p * B)) & ((1 << B) - 1);
    fft::real_fft(res[p]);
  }
  return res;
}
template <unsigned Mod, size_t N, int B = __lg(Mod) / ((N + 1) / 2) + 1>
vector<Modular<Mod>> mint_ifft(array<vector<fft::C>, N> a) {
  int n = a[0].size();
  vector<Modular<Mod>> res(n);
  for (size_t p = 0; p < N; ++p) {
    fft::real_ifft(a[p]);
    auto base = power(Modular<Mod>(2), p * B);
    for (int i = 0; i < n; ++i) res[i] += round(a[p][i].x) * base;
  }
  return res;
}
template <class T, size_t K> array<vector<T>, 2 * K - 1> operator*(
    const array<vector<T>, K>& l, const array<vector<T>, K>& r) {
  int n = l[0].size();
  array<vector<T>, 2 * K - 1> res;
  for (size_t p = 0; p < K; ++p) for (size_t q = 0; q < K; ++q) {
    res[p + q].resize(n);
    for (int i = 0; i < n; ++i)
      res[p + q][i] = res[p + q][i] + l[p][i] * r[q][i];
  }
  return res;
}
template <unsigned Mod> vector<Modular<Mod>> operator*(
    const vector<Modular<Mod>>& l, const vector<Modular<Mod>>& r) {
  if (l.empty() or r.empty()) return {};
  int n = l.size(), m = r.size(), sz = 1 << __lg(2 * (n + m - 1) - 1);
  if (min(n, m) < 30) {
    vector<long long> res(n + m - 1);
    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j)
      res[i + j] += (l[i] * r[j]).v;
    return {begin(res), end(res)};
  }
  bool eq = l == r;
  auto a = mint_fft(l, sz), b = eq ? a : mint_fft(r, sz);
  auto res = mint_ifft<Mod>(a * b);
  return {begin(res), begin(res) + (n + m - 1)};
}
template <unsigned Mod>
vector<Modular<Mod>> inverse(const vector<Modular<Mod>>& a) {
  assert(not a.empty() and not (a[0] == 0));
  vector<Modular<Mod>> b{1 / a[0]};
  for (int m = 1; m < (int)a.size(); m *= 2) {
    vector<Modular<Mod>> x(begin(a), begin(a) + min<int>(a.size(), 2 * m));
    auto fb = mint_fft(b, 2 * m);
    x = mint_ifft<Mod>(mint_fft(x, 2 * m) * fb);
    fill(begin(x), begin(x) + m, 0);
    x = mint_ifft<Mod>(mint_fft(-x, 2 * m) * fb);
    b.insert(end(b), begin(x) + m, end(x));
  }
  return {begin(b), begin(b) + a.size()};
}

constexpr long long mod = 1e9 + 7;
using Mint = Modular<mod>;

vector<Mint> fact, inv_fact, minv;
void prepare(int n) {
  fact.resize(n + 1), inv_fact.resize(n + 1), minv.resize(n + 1);
  for (int i = 0; i <= n; ++i) fact[i] = i ? i * fact[i - 1] : 1;
  inv_fact[n] = 1 / fact[n];
  for (int i = n; i; --i) inv_fact[i - 1] = i * inv_fact[i];
  for (int i = 1; i <= n; ++i) minv[i] = inv_fact[i] * fact[i - 1];
}
Mint binom(int n, int k) {
  if (k < 0 or k > n) return 0;
  return fact[n] * inv_fact[k] * inv_fact[n - k];
}
template<> Mint& Mint::operator/=(Mint r) {
  return *this *= r.v < minv.size() ? minv[r.v] : power(r, mod - 2);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  long long N, M, D1, D2;
  cin >> N >> M >> D1 >> D2;
  long long n = N - 1;
  long long m = M - D1 * (N - 1);
  long long d = D2 - D1 + 1;
  if (m <= 0) return cout << 0 << '\n', 0;
  // [X^(m-1)](1-X^d)^n/(1-X)^(n+2)
  prepare(n + 2);
  vector<Mint> f(m), g(m);
  for (int i = 0; i <= n; ++i) {
    if (i * d >= m) break;
    f[i * d] = binom(n, i);
    if (i & 1) f[i * d] = -f[i * d];
  }
  for (int i = 0; i <= n + 2; ++i) {
    if (i >= m) break;
    g[i] = binom(n + 2, i);
    if (i & 1) g[i] = -g[i];
  }
  f *= inverse(g);
  cout << f[m - 1].v << '\n';
}
0