結果
問題 | No.117 組み合わせの数 |
ユーザー | Pachicobue |
提出日時 | 2017-12-31 16:04:54 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 433 ms / 5,000 ms |
コード長 | 3,571 bytes |
コンパイル時間 | 2,363 ms |
コンパイル使用メモリ | 208,412 KB |
実行使用メモリ | 96,904 KB |
最終ジャッジ日時 | 2024-06-01 10:04:25 |
合計ジャッジ時間 | 3,593 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ソースコード
#include <bits/stdc++.h> #define VARNAME(x) #x #define show(x) cerr << #x << " = " << x << endl using namespace std; using ll = long long; using ld = long double; template <typename T> vector<T> Vec(int n, T v) { return vector<T>(n, v); } template <class... Args> auto Vec(int n, Args... args) { auto val = Vec(args...); return vector<decltype(val)>(n, move(val)); } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "sz:" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template <typename S, typename T> ostream& operator<<(ostream& os, const pair<S, T>& p) { os << "(" << p.first << "," << p.second << ")"; return os; } constexpr ll MOD = (ll)1e9 + 7LL; constexpr ld PI = static_cast<ld>(3.1415926535898); template <typename T> constexpr T INF = numeric_limits<T>::max() / 10; class Modulo { public: Modulo(const int n, const ll mod = 1000000007LL) : m_size{n + 1}, m_mod{mod} // mod should be prime { assert(n > 0); m_fact.resize(n + 1); m_inv.resize(n + 1); m_inv_fact.resize(n + 1); m_fact[0] = 1; m_inv[0] = 1; m_inv_fact[0] = 1; m_fact[1] = 1; m_inv[1] = 1; m_inv_fact[1] = 1; for (int i = 2; i <= n; i++) { m_fact[i] = (m_fact[i - 1] * static_cast<ll>(i)) % mod; m_inv[i] = ((mod - (mod / static_cast<ll>(i))) * m_inv[static_cast<unsigned int>(mod) % i]) % mod; m_inv_fact[i] = (m_inv_fact[i - 1] * m_inv[i]) % mod; } } ll factorial(const int n) const { assert(n < m_size); return m_fact[n]; } ll inverse(const int n) const { assert(n < m_size); return m_inv[n]; } ll inverseFactorial(const int n) const { assert(n < m_size); return m_inv_fact[n]; } ll permutation(const int n, const int k) const { assert(n < m_size); if (k > n) { return 0; } return (m_fact[n] * m_inv_fact[n - k]) % m_mod; } ll combination(const int n, const int k) const { assert(n < m_size); if (k > n) { return 0; } return (((m_fact[n] * m_inv_fact[k]) % m_mod) * m_inv_fact[n - k]) % m_mod; } ll homogenious(const int n, const int k) const { if (n == 0 and k == 0) { return 1; } return combination(n + k - 1, k); } private: const int m_size; const ll m_mod; vector<ll> m_fact; vector<ll> m_inv; vector<ll> m_inv_fact; }; int main() { cin.tie(0); ios::sync_with_stdio(false); constexpr ll MAX = 2000000; int T; cin >> T; auto parse = [](const string& s) { const int t = s[0] == 'C' ? 0 : s[0] == 'P' ? 1 : 2; const int comma = s.find(','); const ll n1 = stoll(s.substr(2, comma - 2)); const ll n2 = stoll(s.substr(comma + 1, s.size() - comma - 2)); return make_pair(t, make_pair(n1, n2)); }; Modulo mod(2 * MAX); for (int t = 0; t < T; t++) { string s; cin >> s; auto p = parse(s); const ll n1 = p.second.first; const ll n2 = p.second.second; if (p.first == 0) { cout << mod.combination(n1, n2) << endl; } else if (p.first == 1) { cout << mod.permutation(n1, n2) << endl; } else { cout << mod.homogenious(n1, n2) << endl; } } return 0; }