結果
| 問題 |
No.117 組み合わせの数
|
| ユーザー |
bad_boy_gaogao
|
| 提出日時 | 2021-08-03 03:30:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,516 bytes |
| コンパイル時間 | 2,181 ms |
| コンパイル使用メモリ | 196,568 KB |
| 最終ジャッジ日時 | 2025-01-23 13:48:54 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 1 |
コンパイルメッセージ
In file included from /usr/include/c++/13/istream:41,
from /usr/include/c++/13/sstream:40,
from /usr/include/c++/13/complex:45,
from /usr/include/c++/13/ccomplex:39,
from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:127,
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:
/usr/include/c++/13/ostream:204:25: warning: ‘ans’ may be used uninitialized [-Wmaybe-uninitialized]
204 | { 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 = 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;
}
}
bad_boy_gaogao