結果

問題 No.1191 数え上げを愛したい(数列編)
ユーザー 👑 jupirojupiro
提出日時 2020-10-22 23:59:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 3,217 bytes
コンパイル時間 1,301 ms
コンパイル使用メモリ 132,700 KB
実行使用メモリ 33,072 KB
最終ジャッジ日時 2023-09-28 14:57:33
合計ジャッジ時間 3,927 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
29,160 KB
testcase_01 AC 50 ms
30,720 KB
testcase_02 AC 48 ms
29,192 KB
testcase_03 AC 49 ms
29,264 KB
testcase_04 AC 54 ms
30,460 KB
testcase_05 AC 50 ms
29,200 KB
testcase_06 AC 53 ms
33,072 KB
testcase_07 AC 48 ms
29,428 KB
testcase_08 AC 47 ms
29,440 KB
testcase_09 AC 52 ms
29,688 KB
testcase_10 AC 49 ms
29,276 KB
testcase_11 AC 44 ms
29,168 KB
testcase_12 AC 55 ms
30,500 KB
testcase_13 AC 49 ms
31,280 KB
testcase_14 AC 52 ms
31,904 KB
testcase_15 AC 51 ms
29,156 KB
testcase_16 AC 49 ms
29,164 KB
testcase_17 AC 45 ms
29,124 KB
testcase_18 AC 51 ms
29,380 KB
testcase_19 AC 48 ms
29,132 KB
testcase_20 AC 46 ms
29,276 KB
testcase_21 AC 50 ms
29,192 KB
testcase_22 AC 50 ms
29,356 KB
testcase_23 AC 49 ms
29,156 KB
testcase_24 AC 46 ms
29,156 KB
testcase_25 AC 47 ms
29,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;
using ull = unsigned long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
//constexpr long long mod = 1000000007LL;
constexpr long long mod = 998244353;
constexpr int MAX = 1100000;

struct mint {
	long long x;
	mint(long long x = 0) :x((x% mod + mod) % mod) {}
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime mod
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res /= a;
	}
};

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}

mint COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return mint(fac[n] * (finv[k] * finv[n - k] % mod) % mod);
}

mint LCOM(ll n, ll k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	if (k > n / 2) k = n - k;
	mint res = 1;
	for (int i = 0; i < k; i++) res *= (n - i);
	res *= finv[k];
	return res;
}
int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	COMinit();
	ll n, m, a, b; cin >> n >> m >> a >> b;

	mint res = 0;
	if (a * (n - 1) > b) {
		cout << 0 << endl;
		return 0;
	}
	ll t = b - a * (n - 1);
	vector<mint> v(t + 1);
	for (int i = 0; i <= t; i++) {
		v[i] = COM(n - 2 + i, i);
	}
	vector<mint> sum(v.size() + 1);
	for (int i = 0; i < v.size(); i++) sum[i + 1] = sum[i] + v[i];
	for (ll i = 1; i <= m; i++) {
		if (i + a * (n - 1) > m) break;
		res += sum[min(t, m - i - a * (n - 1)) + 1];
	}
	res *= fac[n];
	cout << res.x << "\n";
}
0