結果

問題 No.802 だいたい等差数列
ユーザー tsutajtsutaj
提出日時 2019-03-17 22:25:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 2,644 bytes
コンパイル時間 961 ms
コンパイル使用メモリ 101,264 KB
実行使用メモリ 50,496 KB
最終ジャッジ日時 2023-09-22 07:47:32
合計ジャッジ時間 3,950 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
50,324 KB
testcase_01 AC 45 ms
50,228 KB
testcase_02 AC 45 ms
50,232 KB
testcase_03 AC 45 ms
50,292 KB
testcase_04 AC 45 ms
50,408 KB
testcase_05 AC 45 ms
50,280 KB
testcase_06 AC 45 ms
50,220 KB
testcase_07 AC 45 ms
50,492 KB
testcase_08 AC 46 ms
50,280 KB
testcase_09 AC 46 ms
50,184 KB
testcase_10 AC 45 ms
50,280 KB
testcase_11 AC 46 ms
50,296 KB
testcase_12 AC 45 ms
50,188 KB
testcase_13 AC 46 ms
50,220 KB
testcase_14 AC 47 ms
50,180 KB
testcase_15 AC 47 ms
50,496 KB
testcase_16 AC 46 ms
50,176 KB
testcase_17 AC 45 ms
50,224 KB
testcase_18 AC 46 ms
50,332 KB
testcase_19 AC 46 ms
50,296 KB
testcase_20 AC 49 ms
50,184 KB
testcase_21 AC 49 ms
50,484 KB
testcase_22 AC 47 ms
50,284 KB
testcase_23 AC 49 ms
50,328 KB
testcase_24 AC 45 ms
50,280 KB
testcase_25 AC 46 ms
50,292 KB
testcase_26 AC 46 ms
50,412 KB
testcase_27 AC 46 ms
50,212 KB
testcase_28 AC 47 ms
50,284 KB
testcase_29 AC 49 ms
50,340 KB
testcase_30 AC 45 ms
50,348 KB
testcase_31 AC 45 ms
50,328 KB
testcase_32 AC 45 ms
50,356 KB
testcase_33 AC 47 ms
50,184 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 = 3000010;
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