結果

問題 No.117 組み合わせの数
ユーザー bad_boy_gaogaobad_boy_gaogao
提出日時 2021-08-03 03:30:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,516 bytes
コンパイル時間 2,074 ms
コンパイル使用メモリ 204,424 KB
実行使用メモリ 237,676 KB
最終ジャッジ日時 2024-09-16 14:50:01
合計ジャッジ時間 4,095 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/istream:39,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/sstream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/complex:45,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ccomplex:39,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:54,
                 from main.cpp:1:
In member function 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>]',
    inlined from 'int main()' at main.cpp:60:17:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ostream:202:25: warning: 'ans' may be used uninitialized [-Wmaybe-uninitialized]
  202 |       { return _M_insert(__n); }
      |                ~~~~~~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:56:12: note: 'ans' was declared here
   56 |         ll ans;
      |            ^~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T=ll> using vec = vector<T>;
#define BE(x) x.begin(), x.end()

const ll mod   = 1000000007;
const ll max_n = 10000000;
vec<ll> inv(max_n+1,1), fact(max_n+1,1), finv(max_n+1,1);
bool pre_f = true;

void comb_pre () {
    for (ll i = 2; i <= max_n; i++) {
        inv [i] = mod-inv[mod%i]*(mod/i)%mod; // inverse for i
        fact[i] = fact[i-1]*i%mod;            // i!
        finv[i] = finv[i-1]*inv[i]%mod;       // inverse for i!
    }
    pre_f = false;
}

ll comb (ll n, ll k) {
    if (n<k || n<0 || k<0) return 0;
    if (pre_f) comb_pre();
    return fact[n]*(finv[k]*finv[n-k]%mod)%mod;
}
    // set max_n and mod value
    // n and k are up to about 10^7
    // pre:O(max_n), query:O(1)


int main() {
    int q;
    cin >> q;
    for (int _ = 0; _ < q; _++) {

        string s;
        cin >> s;
        char c = s[0];
        string sn, sk;
        s.erase(0,2);
        bool f = false;
        while (true) {
            if (!f) {
                if (s[0] == ',') f = true;
                else sn += s[0];
                s.erase(0,1);
            }
            else {
                if (s[0] == ')') break;
                sk += s[0];
                s.erase(0,1);
            }
        }
        ll n = stoll(sn), k = stoll(sk);

        ll ans;
        if (c == 'C') ans = comb(n,k);
        if (c == 'P') ans = comb(n,k)*fact[k]%mod;
        if (c == 'H') ans = comb(n+k-1,k);
        cout << ans << endl;
    }
}
0