結果

問題 No.802 だいたい等差数列
ユーザー tsutajtsutaj
提出日時 2019-03-17 22:25:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,644 bytes
コンパイル時間 788 ms
コンパイル使用メモリ 101,616 KB
実行使用メモリ 19,176 KB
最終ジャッジ日時 2023-09-22 07:46:20
合計ジャッジ時間 6,177 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
19,032 KB
testcase_01 AC 17 ms
19,172 KB
testcase_02 AC 16 ms
18,980 KB
testcase_03 AC 16 ms
19,028 KB
testcase_04 AC 16 ms
19,104 KB
testcase_05 AC 16 ms
19,036 KB
testcase_06 AC 17 ms
18,984 KB
testcase_07 AC 17 ms
18,980 KB
testcase_08 AC 17 ms
19,036 KB
testcase_09 AC 17 ms
19,036 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 17 ms
18,964 KB
testcase_13 RE -
testcase_14 AC 18 ms
19,176 KB
testcase_15 AC 18 ms
19,032 KB
testcase_16 AC 17 ms
18,964 KB
testcase_17 AC 16 ms
19,032 KB
testcase_18 AC 16 ms
18,956 KB
testcase_19 AC 17 ms
18,972 KB
testcase_20 AC 20 ms
18,976 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 19 ms
19,168 KB
testcase_24 AC 16 ms
19,088 KB
testcase_25 AC 16 ms
19,096 KB
testcase_26 AC 17 ms
19,032 KB
testcase_27 AC 17 ms
18,972 KB
testcase_28 AC 18 ms
19,020 KB
testcase_29 RE -
testcase_30 AC 17 ms
19,020 KB
testcase_31 AC 16 ms
18,968 KB
testcase_32 AC 16 ms
18,956 KB
testcase_33 AC 17 ms
18,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;


const int MAXN = 1000010;
ll fact[MAXN], inv[MAXN];

// 繰り返し二乗法 (modの世界での累乗)
// ※modが素数の場合、この関数で(mod - 2)乗したら、mod割り算ができるよ!
// (参考問題: ABC034 C問題など)
ll mod_pow(ll x, ll n) {
    ll res = 1;
    while(n > 0) {
        if(n & 1) res = (res * x) % MOD; //ビット演算(最下位ビットが1のとき)
        x = (x * x) % MOD;
        n >>= 1; //右シフト(n = n >> 1)
    }
    return res;
}

void init_fact() {
    fact[0] = 1;
    for(int i=1; i<MAXN; i++) {
        fact[i] = (fact[i-1] * i) % MOD;
    }

    inv[MAXN - 1] = mod_pow(fact[MAXN - 1], MOD-2);
    for(int i=MAXN - 2; i>=0; i--) {
        inv[i] = (inv[i+1] * (i+1)) % MOD;
    }
}

ll comb(int n, int r) {
    if(r < 0 || n < r) return 0;
    return fact[n] * inv[n-r] % MOD * inv[r] % MOD;
}

signed main() {
    init_fact();
    
    int N, M, D1, D2; cin >> N >> M >> D1 >> D2;
    int rem = M - 1 - D1 * (N - 1);
    if(rem < 0) {
        cout << 0 << endl;
        return 0;
    }

    // N+1 個の隙間に、合計 rem 個を突っ込む
    // D2 - D1 を超えてはいけない箇所が N-1 箇所
    // 包除っぽくやる?
    int ans = 0;
    for(int i=0; i<N; i++) {
        // i 箇所は必ず D2 - D1 を超えるようにしたい
        // D2 - D1 + 1 を予めそこに突っ込むことにする
        if(rem < (D2 - D1 + 1) * i) continue;

        int nrem = rem - (D2 - D1 + 1) * i;
        int val = comb(N-1, i);
        (val *= comb(nrem + N, N)) %= MOD;

        if(i % 2 == 0) (ans += val) %= MOD;
        else (ans += MOD - val) %= MOD;
    }
    cout << ans << endl;
    return 0;
}
0