結果

問題 No.117 組み合わせの数
コンテスト
ユーザー Rumain831
提出日時 2026-08-13 02:28:33
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.90.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
RE  
実行時間 -
コード長 1,364 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,015 ms
コンパイル使用メモリ 180,016 KB
実行使用メモリ 34,560 KB
最終ジャッジ日時 2026-08-13 02:28:39
合計ジャッジ時間 3,399 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other RE * 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:50:6: warning: 'l' may be used uninitialized [-Wmaybe-uninitialized]
   50 |     l++;
      |     ~^~
main.cpp:47:9: note: 'l' was declared here
   47 |     int l, r;
      |         ^
main.cpp:51:27: warning: 'r' may be used uninitialized [-Wmaybe-uninitialized]
   51 |     string T=s.substr(l, r-l);
      |                          ~^~
main.cpp:47:12: note: 'r' was declared here
   47 |     int l, r;
      |            ^

ソースコード

diff #
raw source code

#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(1e6+1, mod);
  auto nCk=[&](int n, int k){
    if(k>n) 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; break;}
    for(int i=s.size()-1; i>=0; i--)if(s[i]=='('){r=i; break;}
    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 cout << nCk(a+b-1, b) << '\n';
  }
  return 0; 
}
0