結果
| 問題 |
No.117 組み合わせの数
|
| コンテスト | |
| ユーザー |
T1610
|
| 提出日時 | 2020-08-29 16:08:43 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 583 ms / 5,000 ms |
| コード長 | 3,640 bytes |
| コンパイル時間 | 2,979 ms |
| コンパイル使用メモリ | 202,236 KB |
| 最終ジャッジ日時 | 2025-01-13 20:21:24 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 1 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}
template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}
template <typename T, typename U> T pow_fast(T x, U n) {
T ret = (T)1;
while(n) {
if(n & (U)1) ret *= x;
x *= x;
n >>= 1;
}
return ret;
}
template <typename ModType, ModType MOD> struct ModInt {
ModType val;
ModInt() : val(0) {}
ModInt(ModType x) : val(x % MOD) {
if(val < 0) val += MOD;
};
operator ModType() const { return val; }
ModInt &operator+=(const ModInt &m) {
val += m.val;
if(val >= MOD) val -= MOD;
return *this;
}
ModInt &operator-=(const ModInt &m) {
val -= m.val;
if(val < 0) val += MOD;
return *this;
}
ModInt &operator*=(const ModInt &m) {
(val *= m.val) %= MOD;
return *this;
}
ModInt &operator/=(const ModInt &m) {
*this *= m.inverse();
return *this;
}
ModInt operator+(const ModInt &m) const { return ModInt(*this) += m; }
ModInt operator-(const ModInt &m) const { return ModInt(*this) -= m; }
ModInt operator*(const ModInt &m) const { return ModInt(*this) *= m; }
ModInt operator/(const ModInt &m) const { return ModInt(*this) /= m; }
ModInt inverse() const { return pow_fast(*this, MOD - 2); }
ModInt pow(ModType n) const {return pow_fast(*this, n); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &rhs) {
os << rhs.val;
return os;
}
friend std::istream &operator>>(std::istream &is, ModInt &rhs) {
ModType value;
is >> value;
rhs = ModInt(value);
return is;
}
};
template <class mint> struct Comb {
vector<mint> fact;
vector<mint> ifact;
const int N;
Comb(int n) : N(n + 1), fact(n + 1), ifact(n + 1) { makeFact(); }
void makeFact() {
fact[0] = ifact[0] = 1LL;
for(int i = 1; i < N; i++) {
fact[i] = fact[i - 1] * (mint)i;
ifact[i] = fact[i].inverse();
}
}
mint operator()(int n, int r) {
if(n < 0 || r < 0 || r > n) return 0;
if(r > n / 2) r = n - r;
return fact[n] * ifact[n - r] * ifact[r];
}
mint perm(int n, int r) {
if(n < r) return 0;
return fact[n] * ifact[n-r];
}
};
using mint = ModInt<ll, MOD>;
vector<string> split(const string& s, char c = ' ') {
vector<string> ret;
stringstream ss(s);
string t;
while (getline(ss, t, c)) {
ret.emplace_back(t);
}
return ret;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int t;
cin >> t;
Comb<mint> cmb(3e6+10);
rep(i, t) {
string s;
cin >> s;
char c = s[0];
s = s.substr(2);
s.pop_back();
auto v = split(s, ',');
int a = stoi(v[0].c_str());
int b = stoi(v[1].c_str());
// cout << i << " " << c << " " << a << " " << b << '\n' << flush;
mint ans;
if(c == 'C') ans = cmb(a, b);
else if(c == 'P') ans = cmb.perm(a, b);
else ans = (a == 0 && b == 0 ? mint(1) : cmb(a+b-1, b));
cout << ans << '\n';
}
return 0;
}
T1610