結果
問題 | No.117 組み合わせの数 |
ユーザー | veqcc |
提出日時 | 2020-04-01 19:42:22 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 321 ms / 5,000 ms |
コード長 | 4,766 bytes |
コンパイル時間 | 825 ms |
コンパイル使用メモリ | 70,656 KB |
実行使用メモリ | 50,176 KB |
最終ジャッジ日時 | 2024-06-27 04:41:15 |
合計ジャッジ時間 | 1,533 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ソースコード
#include <iostream> #include <vector> typedef long long ll; using namespace std; const ll MOD = 1000000007LL; template <ll mod> class ModInt { ll a; public: constexpr ModInt(const ll a = 0) noexcept : a((a % mod + mod) % mod) {} constexpr ll &value() noexcept { return a; } constexpr ModInt operator + (const ModInt &rhs) const noexcept { return ModInt(*this) += rhs; } constexpr ModInt operator - (const ModInt &rhs) const noexcept { return ModInt(*this) -= rhs; } constexpr ModInt operator * (const ModInt &rhs) const noexcept { return ModInt(*this) *= rhs; } constexpr ModInt operator / (const ModInt &rhs) const noexcept { return ModInt(*this) /= rhs; } constexpr ModInt &operator += (const ModInt &rhs) noexcept { a += rhs.a; if (a >= mod) a -= mod; return *this; } constexpr ModInt &operator -= (const ModInt &rhs) noexcept { a += mod - rhs.a; if (a >= mod) a -= mod; return *this; } constexpr ModInt &operator *= (const ModInt &rhs) noexcept { a = a * rhs.a % mod; return *this; } constexpr ModInt pow(ll t) const noexcept { if (t == 0) return 1; auto ret = pow(t >> 1); ret *= ret; if (t & 1) ret *= *this; return ret; } constexpr ModInt inv() const noexcept { return pow(mod - 2); } constexpr ModInt operator /=(const ModInt &rhs) { return (*this) *= rhs.inv(); } constexpr bool operator == (const ModInt &rhs) const noexcept { return this->a == rhs.a; } constexpr bool operator != (const ModInt &rhs) const noexcept { return this->a != rhs.a; } friend constexpr ostream &operator << (ostream &os, const ModInt &rhs) noexcept { return os << rhs.a; } friend constexpr istream &operator >> (istream &is, ModInt &rhs) { is >> rhs.a; return is; } }; using mint = ModInt<MOD>; class Combination { public: vector <mint> fac, inv, fiv; Combination(int N) : fac(N + 1), inv(N + 1), fiv(N + 1) { fac[0] = inv[0] = fiv[0] = fac[1] = inv[1] = fiv[1] = 1; for (ll i = 2; i <= N; i++) { fac[i] = fac[i - 1] * i; // n! inv[i] = inv[MOD % i] * (MOD - MOD / i); // n^-1 fiv[i] = fiv[i - 1] * inv[i]; // (n!)^-1 } } mint C(ll n, ll k) { if (k < 0 || n < k) return 0; return fac[n] * fiv[k] * fiv[n - k]; } mint P(ll n, ll k) { if (k < 0 || n < k) return 0; return fac[n] * fiv[n - k]; } mint H(ll n, ll k) { if (n == 0 && k == 0) return 1; return C(n + k - 1, k - 1); } }; // verified // https://yukicoder.me/problems/no/117 void yuki117() { int T; cin >> T; Combination com(2000005); for (int t = 0; t < T; t++) { string s; cin >> s; int n = 0, k = 0, i = 2; while (s[i] != ',') { n *= 10; n += s[i++] - '0'; } i++; while (s[i] != ')') { k *= 10; k += s[i++] - '0'; } if (s[0] == 'C') cout << com.C(n, k) << "\n"; else if (s[0] == 'P') cout << com.P(n, k) << "\n"; else cout << com.H(k, n) << "\n"; } } //// verified //// https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/all/DPL_5_B //void AOJ_DPL_5_B() { // int n, k; // cin >> n >> k; // Combination com(k); // cout << com.P(k, n) << "\n"; //} // // //// verified //// https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/5/DPL_5_C //ll pow_mod(ll num, ll pow, ll mod) { // ll prod = 1; num %= mod; // while (pow > 0) { if (pow & 1) prod = prod * num % mod; num = num * num % mod; pow >>= 1; } // return prod; //} //void AOJ_DPL_5_C() { // int n, k; // cin >> n >> k; // Combination com(k); // ll ans = 0; // for (int i = 0; i < k; i++) { // ll sgn = i % 2 == 0 ? 1 : -1; // ans = (ans + MOD + sgn * com.C(k, k - i) * pow_mod(k - i, n, MOD) % MOD) % MOD; // } // cout << ans << "\n"; //} // // //// verified //// https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/5/DPL_5_D //void AOJ_DPL_5_D() { // int n, k; // cin >> n >> k; // Combination com(n + k); // cout << com.H(n, k) << "\n"; //} // // //// verified //// https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/5/DPL_5_E //void AOJ_DPL_5_E() { // int n, k; // cin >> n >> k; // Combination com(k); // cout << com.C(k, n) << "\n"; //} // // //// verified //// https://onlinejudge.u-aizu.ac.jp/courses/library/7/DPL/5/DPL_5_F //void AOJ_DPL_5_F() { // int n, k; // cin >> n >> k; // Combination com(n); // cout << com.H(n - k, k) << "\n"; //} int main() { yuki117(); // AOJ_DPL_5_B(); // AOJ_DPL_5_C(); // AOJ_DPL_5_D(); // AOJ_DPL_5_E(); // AOJ_DPL_5_F(); return 0; }