結果
問題 | No.117 組み合わせの数 |
ユーザー | is_eri23 |
提出日時 | 2015-01-05 16:11:13 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 89 ms / 5,000 ms |
コード長 | 3,502 bytes |
コンパイル時間 | 1,506 ms |
コンパイル使用メモリ | 160,936 KB |
実行使用メモリ | 18,976 KB |
最終ジャッジ日時 | 2024-06-13 02:42:02 |
合計ジャッジ時間 | 1,963 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ソースコード
#include <bits/stdc++.h> #define EPS 1e-9 #define INF 1070000000LL #define MOD 1000000007LL #define fir first #define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++) #define numa(x,a) for(auto x: a) #define ite iterator #define mp make_pair #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define pb push_back #define pf push_front #define sec second #define sz(x) ((int)(x).size()) #define ALL( c ) (c).begin(), (c).end() #define gcd(a,b) __gcd(a,b) #define mem(x,n) memset(x,n,sizeof(x)) #define endl "\n" using namespace std; template <int POS, class TUPLE> void deploy(std::ostream &os, const TUPLE &tuple){} template <int POS, class TUPLE, class H, class ...Ts> void deploy(std::ostream &os, const TUPLE &t){ os << (POS == 0 ? "" : ", ") << get<POS>(t); deploy<POS + 1, TUPLE, Ts...>(os, t); } template <class T,class U> std::ostream& operator<<(std::ostream &os, std::pair<T,U> &p){ os << "(" << p.first <<", " << p.second <<")";return os; } template <class T> std::ostream& operator<<(std::ostream &os, std::vector<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; } template <class T> std::ostream& operator<<(std::ostream &os, std::set<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; } template <class T, class K> std::ostream& operator<<(std::ostream &os, std::map<T, K> &mp){ int remain = mp.size(); os << "{"; for(auto e: mp) os << "(" << e.first << " -> " << e.second << ")" << (--remain == 0 ? "}" : ", "); return os; } #define DEBUG1(var0) { std::cerr << (#var0) << "=" << (var0) << endl; } #define DEBUG2(var0, var1) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG1(var1); } #define DEBUG3(var0, var1, var2) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG2(var1,var2); } #define DEBUG4(var0, var1, var2, var3) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG3(var1,var2,var3); } using ll = long long; #define MAX_N 2000100 ll fac[MAX_N]; int string_to_int(const string &str){ int ret_ = 0; rep(i,sz(str)){ ret_ *= 10; ret_ += (str[i]-'0'); } return ret_; } int extgcd(int a,int b,int &x,int &y){ int d = a; if(b != 0){ d = extgcd(b, a % b,y,x); y -= (a / b) * x; }else{ x = 1; y = 0; } return d; } int mod_inv(int a, int p=MOD){ int x,y; extgcd(a,p,x,y); return (p + x % p) % p; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); fac[0] = 1; rep2(i,1,MAX_N){ fac[i] = (fac[i-1] * i) % MOD; } int T; string s; cin >> T; while(T--){ cin >> s; string sn; string sr; { int i = 2; for(;;i++){ if(s[i] == ','){ i++; break; } sn += s[i]; } for(;;i++){ if(s[i] == ')'){ break; } sr += s[i]; } } int n = string_to_int(sn); int r = string_to_int(sr); ll ans; if (n < r && s[0] != 'H') { cout << 0 << endl; continue; } if (n == 0 && r == 0) { cout << 1 << endl; continue; } //DEBUG2(n,r); switch(s[0]){ case 'P': ans = fac[n] * mod_inv(fac[n-r]); break; case 'C': ans = fac[n] * mod_inv(fac[n-r]); ans %= MOD; ans *= mod_inv(fac[r]); break; default: ans = fac[n+r-1] * mod_inv(fac[r]); ans %= MOD; ans *= mod_inv(fac[n-1]); break; } cout << ans % MOD << endl; } return 0; }