結果

問題 No.1126 SUM
ユーザー Kome_soudouKome_soudou
提出日時 2022-11-09 02:47:03
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 64 ms / 1,000 ms
コード長 1,238 bytes
コンパイル時間 1,806 ms
コンパイル使用メモリ 171,324 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-22 09:59:35
合計ジャッジ時間 3,852 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

//a^b mod p O(logP)
int64_t ModPow(int64_t a, int64_t b, int64_t p)
{
	int64_t ans = 1;
	while(b > 0)
	{
		if(b & 1) ans = ans * a % p; //bの最下位bitが1ならa^(2^i)をかける
		a = a * a % p;
		b >>= 1; //bを1bit右にシフト
	}
	return ans;
}
vector<int64_t> fact; // 要素数(N + 1)
vector<int64_t> fact_inv; // 要素数(N + 1)
//a! mod pと(a!)^-1 mod pを配列に記録 O(NlogP) (やや遅い まだ高速化可能)
//ModComb実行前にこれを実行すること
void CombInit(int64_t n, int64_t p)
{
	fact.resize(n + 1);
	fact_inv.resize(n + 1);
	fact.at(0) = 1; fact.at(1) = 1;
	for(int64_t i = 2; i <= n; i++)
	{
		fact.at(i) = fact.at(i - 1) * i;
		fact.at(i) %= p;
	}
	fact_inv.at(0) = 1; fact_inv.at(1) = 1;
	for(int64_t i = 2; i <= n; i++) fact_inv.at(i) = ModPow(fact.at(i), p - 2, p);
}
//nCk mod p O(1)
int64_t ModComb(int64_t n, int64_t k, int64_t p)
{
  return ((fact.at(n) * fact_inv.at(k)) % p * fact_inv.at(n - k)) % p;
}

int main()
{
	int64_t n, m;
	cin >> n >> m;
	const int64_t mod = 1000000007;
	
	CombInit(m, mod);
	int64_t ans = 0;
	for(int64_t i = n; i <= m; i++)
	{
		ans += ModComb(i, n, mod);
		ans %= mod;
	}
	cout << ans << endl;
	return 0;
}
0