結果

問題 No.802 だいたい等差数列
ユーザー heno239heno239
提出日時 2019-03-10 00:52:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,520 bytes
コンパイル時間 1,656 ms
コンパイル使用メモリ 168,244 KB
実行使用メモリ 23,408 KB
最終ジャッジ日時 2023-09-14 12:07:50
合計ジャッジ時間 3,377 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 19 ms
18,784 KB
testcase_11 AC 24 ms
23,052 KB
testcase_12 AC 22 ms
21,048 KB
testcase_13 AC 24 ms
22,708 KB
testcase_14 AC 24 ms
21,516 KB
testcase_15 AC 21 ms
20,380 KB
testcase_16 AC 25 ms
22,416 KB
testcase_17 AC 24 ms
22,028 KB
testcase_18 AC 23 ms
22,276 KB
testcase_19 AC 25 ms
23,408 KB
testcase_20 AC 26 ms
23,400 KB
testcase_21 AC 26 ms
23,340 KB
testcase_22 AC 25 ms
23,292 KB
testcase_23 AC 26 ms
23,312 KB
testcase_24 AC 26 ms
23,324 KB
testcase_25 AC 19 ms
18,628 KB
testcase_26 AC 11 ms
7,752 KB
testcase_27 AC 24 ms
22,612 KB
testcase_28 AC 15 ms
12,324 KB
testcase_29 AC 26 ms
23,336 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 14 ms
12,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
struct perm {
private:
	int sz;
	vector<ll> p, invp;
public:
	perm(int n) {
		sz = n+1;
		p.resize(sz), invp.resize(sz);
		p[0] = 1;
		rep1(i, sz - 1) {
			p[i] = p[i - 1] * i%mod;
		}
		invp[sz - 1] = 1;
		ll cop = mod - 2, x = p[sz-1];
		while (cop) {
			if (cop % 2)invp[sz - 1] = invp[sz - 1] * x%mod;
			cop >>= 1; x = x * x % mod;
		}
		per(i, sz - 1) {
			invp[i] = invp[i + 1] * (i + 1) % mod;
		}
	}
	ll comb(ll x, ll y) {
		if (x < y || y < 0)return 0;
		ll ret = p[x];
		(ret *= invp[y]) %= mod;
		(ret *= invp[x - y]) %= mod;
		return ret;
	}
	ll combP(ll x, ll y) {
		if (x < y || y < 0)return 0;
		return p[x] * invp[x - y] % mod;
	}
};
int main() {
	ll n, m; cin >> n >> m;
	perm p(n-1+m);
	ll d1, d2; cin >> d1 >> d2;
	//左端を消し、a1<a2<...<anにする
	//このとき差は1以上d2-d1+1以下になる
	m -= (d1-1)*(n - 1);
	d2 -= d1-1;

	ll ans = 0;
	//条件を満たさないやつをi個として、包除原理
	rep(i, n) {
		//差がd2+1以上のやつは先にd2引いておいて残りをn-1個に再分配すると考えて問題ない
		ll rest = m - d2 * i;

		//一般に、1<=a1<a2<...<an<=mを満たす数列aの個数はmCn
		ll csum = p.comb(rest, n)*p.comb(n - 1, i)%mod;
		if (i % 2)ans += mod-csum;
		else ans += csum;
	}
	cout << ans % mod << endl;
	return 0;
}
0