結果
問題 | No.117 組み合わせの数 |
ユーザー | tancahn2380 |
提出日時 | 2017-09-24 16:08:20 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 2,287 ms / 5,000 ms |
コード長 | 1,987 bytes |
コンパイル時間 | 670 ms |
コンパイル使用メモリ | 86,804 KB |
実行使用メモリ | 394,012 KB |
最終ジャッジ日時 | 2024-11-14 07:10:35 |
合計ジャッジ時間 | 5,796 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ソースコード
# include <iostream> # include <algorithm> # include <vector> # include <string> # include <set> # include <map> # include <cmath> # include <iomanip> # include <functional> # include <utility> # include <stack> # include <queue> # include <list> # include <bitset> using namespace std; using LL = long long; using ULL = unsigned long long; constexpr long long MOD = 1000000000 + 7; constexpr long long INF = 100000000; const double PI = acos(-1); //組み合わせ(コンビネーション) int fact[100000000]; LL factrial(LL n, LL mod) { if (fact[n] != 0)return fact[n]; if (n == 0)return fact[n] = 1; return fact[n] = factrial(n - 1, mod)*n%mod; } void factrialinitialize(LL n, LL mod) { fact[0] = 1; for (LL i = 1; i < n; i++) { fact[i] = i * fact[i - 1] % mod; } } LL repeatsquaring(LL n, LL p, LL mod) { if (n == 0)return 0; if (p <= 0 || n == 1)return 1; if (p % 2 == 0) { LL t = repeatsquaring(n, p / 2, mod); return t*t%mod; } return n*repeatsquaring(n, p - 1, mod) % mod; } LL permutation(LL n, LL r, LL mod) { if (n < r)return 0; if (n == r)return factrial(n, mod); LL ans = 1; ans *= factrial(n, mod); ans %= mod; ans *= repeatsquaring(factrial(n - r, mod), mod - 2, mod); ans %= mod; return ans; } LL combination(LL n, LL r, LL mod) { if (r == 0) return 1; if (n < r)return 0; LL ans = 1; ans *= factrial(n, mod); ans %= mod; ans *= repeatsquaring(factrial(n - r, mod), mod - 2, mod); ans %= mod; ans *= repeatsquaring(factrial(r, mod), mod - 2, mod); ans %= mod; return ans; } LL CombinationWithRepetition(LL n, LL r, LL mod) { return combination(n + r - 1, r, mod); } int main() { factrialinitialize(100000000, MOD); //(n,r,MOD) int n; cin >> n; int a, b; char c, d; for (int i = 0; i < n; i++) { cin >> c >> d >> a >> d >> b >> d; if (c == 'C')cout << combination(a, b, MOD) << endl; if (c == 'P')cout << permutation(a, b, MOD) << endl; if (c == 'H')cout << CombinationWithRepetition(a, b, MOD) << endl; } }