結果
| 問題 | No.117 組み合わせの数 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-08-13 02:33:25 |
| 言語 | C++23(gcc16) (gcc 16.1.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 174 ms / 5,000 ms |
| + 222µs | |
| コード長 | 1,415 bytes |
| 記録 | |
| コンパイル時間 | 1,592 ms |
| コンパイル使用メモリ | 180,684 KB |
| 実行使用メモリ | 66,048 KB |
| 最終ジャッジ日時 | 2026-08-13 02:33:48 |
| 合計ジャッジ時間 | 2,723 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 1 |
コンパイルメッセージ
main.cpp: In function 'std::pair<int, int> f(std::string&)':
main.cpp:30:22: warning: 'co' may be used uninitialized [-Wmaybe-uninitialized]
30 | int a=stoi(s.substr(0, co)), b=stoi(s.substr(co+1, n-co-1));
| ~~~~~~~~^~~~~~~
main.cpp:28:7: note: 'co' was declared here
28 | int co;
| ^~
main.cpp: In function 'int main()':
main.cpp:52:6: warning: 'l' may be used uninitialized [-Wmaybe-uninitialized]
52 | l++;
| ~^~
main.cpp:47:9: note: 'l' was declared here
47 | int l, r;
| ^
main.cpp:53:27: warning: 'r' may be used uninitialized [-Wmaybe-uninitialized]
53 | string T=s.substr(l, r-l);
| ~^~
main.cpp:47:12: note: 'r' was declared here
47 | int l, r;
| ^
ソースコード
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;
ll modpow(ll a, ll b, ll p){
a%=p;
ll ans=1;
while(b>0){
if(b%2==1) ans=(ans*a)%p;
b/=2;
a=(a*a)%p;
}
return ans;
}
pair<vector<ll>, vector<ll>> factorial(int n, ll mod){
vector<ll> fact(n+1, 1), invfact(n+1, 1);
for(ll i=2; i<=n; i++) fact[i]=fact[i-1]*i%mod;
invfact[n]=modpow(fact[n], mod-2, mod);
for(ll i=n-1; i>=1; i--) invfact[i]=invfact[i+1]*(i+1)%mod;
return {fact, invfact};
}
pair<int, int> f(string& s){
int n=s.size();
int co;
for(int i=0; i<n; i++)if(s[i]==','){co=i; break;}
int a=stoi(s.substr(0, co)), b=stoi(s.substr(co+1, n-co-1));
return {a, b};
}
int main(void){
int q; cin >> q;
ll mod=1e9+7;
auto [fact, inv]=factorial(2e6+1, mod);
auto nCk=[&](int n, int k){
if(k>n||n<0) return 0ll;
return fact[n]*inv[k]%mod*inv[n-k]%mod;
};
while(q--){
string s; cin >> s;
int t;
if(s[0]=='P') t=0;
else t=(s[0]=='C'?1:2);
int l, r;
for(int i=0; i<s.size(); i++){
if(s[i]=='(') l=i;
if(s[i]==')') r=i;
}
l++;
string T=s.substr(l, r-l);
auto [a, b]=f(T);
if(t==0) cout << nCk(a, b)*fact[b]%mod << '\n';
else if(t==1) cout << nCk(a, b) << '\n';
else{
if(a==0){
cout << (b==0?1:0) << '\n';
}
else cout << nCk(a+b-1, b) << '\n';
}
}
return 0;
}