結果

問題 No.1191 数え上げを愛したい(数列編)
ユーザー e869120e869120
提出日時 2021-03-05 18:43:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 500 ms / 2,000 ms
コード長 1,393 bytes
コンパイル時間 1,006 ms
コンパイル使用メモリ 84,300 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2024-04-16 06:52:02
合計ジャッジ時間 15,382 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 492 ms
8,320 KB
testcase_01 AC 495 ms
8,320 KB
testcase_02 AC 494 ms
8,192 KB
testcase_03 AC 500 ms
8,320 KB
testcase_04 AC 497 ms
8,320 KB
testcase_05 AC 493 ms
8,320 KB
testcase_06 AC 500 ms
8,320 KB
testcase_07 AC 496 ms
8,192 KB
testcase_08 AC 498 ms
8,320 KB
testcase_09 AC 498 ms
8,320 KB
testcase_10 AC 491 ms
8,320 KB
testcase_11 AC 493 ms
8,320 KB
testcase_12 AC 498 ms
8,320 KB
testcase_13 AC 500 ms
8,320 KB
testcase_14 AC 497 ms
8,320 KB
testcase_15 AC 487 ms
8,320 KB
testcase_16 AC 490 ms
8,320 KB
testcase_17 AC 492 ms
8,192 KB
testcase_18 AC 492 ms
8,448 KB
testcase_19 AC 489 ms
8,320 KB
testcase_20 AC 489 ms
8,192 KB
testcase_21 AC 488 ms
8,320 KB
testcase_22 AC 494 ms
8,192 KB
testcase_23 AC 490 ms
8,320 KB
testcase_24 AC 490 ms
8,192 KB
testcase_25 AC 491 ms
8,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
#pragma warning (disable: 4996)

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 63; i++) {
		if ((b / (1LL << i)) % 2LL == 1) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}

long long Div(long long a, long long b, long long m) {
	return (a * modpow(b, m - 2, m)) % m;
}

long long mod = 998244353;
long long fact[1 << 19];
long long factinv[1 << 19];

// 入力
long long N, M, A, B;
long long Answer = 0;

long long ncr(long long n, long long r) {
	if (r < 0 || n < r) return 0;
	return (fact[n] * factinv[r] % mod) * factinv[n - r] % mod;
}

int main() {
	// Step #1. 入力・調整
	cin >> N >> M >> A >> B;
	M -= (N - 1) * (A - 1);
	B -= (N - 1) * (A - 1);

	// Step #2. 階乗の計算
	fact[0] = 1;
	for (int i = 1; i <= 300000; i++) fact[i] = (1LL * i * fact[i - 1]) % mod;
	for (int i = 0; i <= 300000; i++) factinv[i] = Div(1, fact[i], mod);

	// Step #3. 動的計画法
	for (int i = 1; i <= M; i++) {
		long long kosuu = min(B + 1LL, M - i + 1LL);
		long long ret = ncr(kosuu - 1, N - 1);
		Answer += ret;
		Answer %= mod;
	}

	// Step #4. 出力
	long long FinalAns = Answer * fact[N] % mod;
	cout << FinalAns << endl;
	return 0;
}
0