結果

問題 No.1684 Find Brackets
ユーザー 👑 ygussanyygussany
提出日時 2021-08-15 14:06:38
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,527 bytes
コンパイル時間 882 ms
コンパイル使用メモリ 31,468 KB
実行使用メモリ 17,500 KB
最終ジャッジ日時 2023-09-12 06:24:15
合計ジャッジ時間 2,377 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 21 ms
17,488 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 25 ms
17,084 KB
testcase_08 AC 22 ms
16,156 KB
testcase_09 AC 12 ms
16,048 KB
testcase_10 AC 6 ms
7,684 KB
testcase_11 AC 4 ms
7,044 KB
testcase_12 AC 13 ms
12,104 KB
testcase_13 AC 20 ms
16,116 KB
testcase_14 AC 22 ms
17,500 KB
testcase_15 AC 16 ms
17,400 KB
testcase_16 AC 22 ms
17,472 KB
testcase_17 AC 22 ms
17,420 KB
testcase_18 AC 21 ms
17,252 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 16 ms
17,416 KB
testcase_22 AC 16 ms
17,400 KB
testcase_23 AC 16 ms
17,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

const int Mod = 1000000007;

long long fact[1000001], fact_inv[1000001];

long long div_mod(long long x, long long y, long long z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

long long combination(int n, int k)
{
	if (k < 0) return 0;
	return fact[n] * fact_inv[k] % Mod * fact_inv[n-k] % Mod;
}

long long naive(int N, int M)
{
	int i, j, k;
	long long ans = 0;
	for (i = M; i <= N; i++) {
		k = N;
		ans += k + N;
		for (j = 1, k -= 2; j <= i && j <= N - i && k >= 0; j++, k -= 2) ans += (k + N) * (combination(N, j) - combination(N, j - 1) + Mod) % Mod;
	}
	return ans % Mod;
}

long long solve(int N, int M)
{
	int i, j, k;
	long long ans = 0;
	if (M * 2 >= N) {
		for (i = 0, j = N - M + 1, k = N * 2; j > 0; i++, j--, k -= 2) ans += k * (combination(N, i) - combination(N, i - 1) + Mod) % Mod * j % Mod;
	} else {
		for (i = 0, j = N + 1, k = N * 2; j > 0; i++, j -= 2, k -= 2) ans += k * (combination(N, i) - combination(N, i - 1) + Mod) % Mod * j % Mod;
		for (i = 0, j = M, k = N * 2; j > 0; i++, j--, k -= 2) ans += Mod - k * (combination(N, i) - combination(N, i - 1) + Mod) % Mod * j % Mod;
	}
	return ans % Mod;
}

int main()
{
	int i, N, M;
	scanf("%d %d", &N, &M);
	for (i = 1, fact[0] = 1; i <= N; i++) fact[i] = fact[i-1] * i % Mod;
	for (i = N - 1, fact_inv[N] = div_mod(1, fact[N], Mod); i >= 0; i--) fact_inv[i] = fact_inv[i+1] * (i + 1) % Mod;
	printf("%lld\n", solve(N, M));
	fflush(stdout);
	return 0;
}
0