結果
| 問題 |
No.117 組み合わせの数
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-05-25 14:36:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 264 ms / 5,000 ms |
| コード長 | 1,771 bytes |
| コンパイル時間 | 1,999 ms |
| コンパイル使用メモリ | 195,996 KB |
| 最終ジャッジ日時 | 2025-01-21 18:11:14 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 1 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
const int nmax=3000005;
ll fac[nmax],finv[nmax],inv[nmax];
void COMinit(){
fac[0]=fac[1]=1;
finv[0]=finv[1]=1;
inv[1]=1;
for(int i=2;i<nmax;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;
}
}
ll com(int n,int k){
if(n<k||n<0||k<0){
return 0;
}
return fac[n]*(finv[k]*finv[n-k]%mod)%mod;
}
pair<int,int> f(string s){
int u=0,v=0;
bool f=false;
REP(i,s.size()){
if(s[i]==','){
f=true;
continue;
}
if(!f) u=u*10+s[i]-'0';
else v=v*10+s[i]-'0';
}
return {u,v};
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int T;cin >> T;
COMinit();
while(T--){
string s;cin >> s;
auto [u,v]=f(s.substr(2,s.size()-3));
if(s[0]=='C'){
cout << com(u,v) << endl;
}
else if(s[0]=='P'){
cout << com(u,v)*fac[v]%mod << endl;
}
else{
if(u==0&&v==0){
cout << 1 << endl;
continue;
}
cout << com(u+v-1,u-1) << endl;
}
}
}