結果

問題 No.802 だいたい等差数列
ユーザー yurujirouyurujirou
提出日時 2021-09-26 16:10:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 996 ms / 2,000 ms
コード長 1,561 bytes
コンパイル時間 3,782 ms
コンパイル使用メモリ 166,836 KB
最終ジャッジ日時 2025-01-24 18:27:17
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <functional>
#include <math.h>
#include <complex>
using namespace std;

#include <atcoder/all>
using namespace atcoder;

#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define RREP(i,n) for(int i = (int)n-1; i >= 0; i--)
#define LREP(i,n) for(LL i = 0; i < (LL)n; i++)

#define Vi vector<int>
#define Vl vector<long long>
#define LP pair<long long ,long long>
#define P pair<int, int>
#define T3 tuple<LL, LL, LL>
#define T4 tuple<LL, LL, LL, LL>
#define INF 1000000007
#define SIZE	2000010
#define MOD 1000000007
typedef long long LL;

LL F[SIZE], FI[SIZE];
void init() {
	F[0] = FI[0] = 1;
	for (LL i = 1; i < SIZE; i++) {
		F[i] = (F[i - 1] * i) % MOD;
		FI[i] = (FI[i - 1] * inv_mod(i, MOD)) % MOD;
	}
}

LL comb(LL n, LL k) {
	if (n < k) return 0;
	LL res = F[n];
	res = (res * FI[n - k]) % MOD;
	res = (res * FI[k]) % MOD;
	return res;
}

LL N, M, D1, D2;

LL cal(LL x) {
	LL n = (M - 1) - (N - 1) * D1;
	n -= x * (D2 - D1 + 1);
	LL res = comb(n + N, N);
	res = (res * comb(N - 1, x)) % MOD;
	return res;
}

int main() {
	cin >> N >> M >> D1 >> D2;
	init();

	LL ans = 0;
	for (LL x = 1; x <= N - 1; x++) {
		LL res = cal(x);
		if (x % 2 == 1) {
			ans = (ans + res) % MOD;
		}
		else {
			ans = (ans - res) % MOD;
		}
	}
	ans = (cal(0) - ans) % MOD;
	cout << (ans + MOD) % MOD << endl;
}
0