結果

問題 No.802 だいたい等差数列
ユーザー てんぷら
提出日時 2024-04-13 14:35:16
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 1,550 bytes
コンパイル時間 5,400 ms
コンパイル使用メモリ 310,180 KB
実行使用メモリ 35,584 KB
最終ジャッジ日時 2024-10-03 00:08:11
合計ジャッジ時間 7,635 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
using namespace std;
using namespace atcoder;
using mint = modint1000000007;
const int inf = 1000000007;
const ll longinf = 1ll << 60;

namespace mod_util {
vector<mint> fact, invfact, inv;
void init(int n = 1 << 20) {
    fact.resize(n + 1);
    invfact.resize(n + 1);
    inv.resize(n + 1);
    fact[0] = 1;
    rep(i, n) fact[i + 1] = fact[i] * (i + 1);
    invfact[n] = fact[n].inv();
    rep(i, n) invfact[n - i - 1] = invfact[n - i] * (n - i);
    rep(i, n) inv[i + 1] = fact[i] * invfact[i + 1];
}

mint comb(int n, int k) {
    if(n < 0 || k < 0 || k > n)
        return 0;
    return fact[n] * invfact[k] * invfact[n - k];
}
} // namespace mod_util
using namespace mod_util;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    init(1 << 21);

    int n, m, d1, d2;
    cin >> n >> m >> d1 >> d2;
    vector<mint> bunshi(m + 1), bunbo(m + 1);
    rep(i, n) {
        ll p = ll(d1) * i + ll(d2 + 1) * (n - 1 - i);
        if(p > m)
            continue;
        if((n - 1 - i) % 2 == 0) {
            bunshi[p] = comb(n - 1, i);
        } else {
            bunshi[p] = -comb(n - 1, i);
        }
    }
    rep(i, m + 1) {
        bunbo[i] = comb(n + i, i);
    }
    mint ans = 0;
    rep(i, m) {
        ans += bunshi[i] * bunbo[m - 1 - i];
    }
    cout << ans.val() << endl;
    return 0;
}
0