結果

問題 No.117 組み合わせの数
ユーザー unagiunagunagiunag
提出日時 2020-08-24 13:53:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,260 bytes
コンパイル時間 871 ms
コンパイル使用メモリ 78,224 KB
最終ジャッジ日時 2025-01-13 13:21:29
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other WA * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:52:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   52 |         scanf("%d\n", &T);
      |         ~~~~~^~~~~~~~~~~~
main.cpp:57:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   57 |                 scanf("%c(%d,%d)\n", &type, &n, &k);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007;

class Factorial {
private:
	vector<lint> fac, inv, finv;

	void build(int N) {
		fac[0] = fac[1] = 1; inv[1] = 1; finv[0] = finv[1] = 1;

		for (int i = 2; i < N; 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;
		}
	}

public:
	Factorial(int N = 110000) : fac(N + 1), inv(N + 1), finv(N + 1) { build(N + 1); }

	lint Cmod (int n, int k) {
		if (n < k || k < 0) return 0LL;
		return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
	}

	lint Pmod(int n, int k) {
		if (n < k || k < 0) return 0LL;
		return fac[n] * finv[n - k] % MOD;
	}
};


struct init {
	init() {
		cin.tie(nullptr); ios::sync_with_stdio(false);
		cout << fixed << setprecision(10);
	}
} init_;

int main() {

	Factorial F(3000000);

	int T;
	scanf("%d\n", &T);

	for (int i = 0; i < T; i++) {
		char type;
		int n, k;
		scanf("%c(%d,%d)\n", &type, &n, &k);

		if (type == 'C') {
			printf("%lld\n", F.Cmod(n, k));
		}
		else if (type == 'P') {
			printf("%lld\n", F.Pmod(n, k));
		}
		else {
			printf("%lld\n", F.Cmod(n + k - 1, k));
		}
	}

	return 0;
}

0