結果
| 問題 | No.117 組み合わせの数 |
| コンテスト | |
| ユーザー |
bad_boy_gaogao
|
| 提出日時 | 2021-08-03 03:46:50 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 155 ms / 5,000 ms |
| コード長 | 1,587 bytes |
| 記録 | |
| コンパイル時間 | 1,307 ms |
| コンパイル使用メモリ | 217,744 KB |
| 実行使用メモリ | 50,332 KB |
| 最終ジャッジ日時 | 2026-06-22 08:31:57 |
| 合計ジャッジ時間 | 2,314 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge2_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 1 |
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/ostream:42,
from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/istream:43,
from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/sstream:42,
from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/complex:50,
from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:141,
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:63:17:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/ostream.h:212:25: warning: 'ans' may be used uninitialized [-Wmaybe-uninitialized]
212 | { return _M_insert(__n); }
| ~~~~~~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:56:12: note: 'ans' was declared here
56 | ll ans;
| ^~~
ソースコード
#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 = 2000000;
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') {
if (n == 0 && k == 0) ans = 1;
else ans = comb(n+k-1,k);
}
cout << ans << endl;
}
}
bad_boy_gaogao