結果
| 問題 |
No.117 組み合わせの数
|
| ユーザー |
youse_8zl
|
| 提出日時 | 2017-12-28 23:41:12 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 235 ms / 5,000 ms |
| コード長 | 2,584 bytes |
| コンパイル時間 | 1,591 ms |
| コンパイル使用メモリ | 171,688 KB |
| 実行使用メモリ | 15,076 KB |
| 最終ジャッジ日時 | 2024-12-21 09:33:12 |
| 合計ジャッジ時間 | 2,223 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 1 |
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:101:17: warning: 'res' may be used uninitialized [-Wmaybe-uninitialized]
101 | cout << res << endl;
| ^~~
main.cpp:94:26: note: 'res' was declared here
94 | char c; int n, k, t, res;
| ^~~
ソースコード
#include <bits/stdc++.h>
#define INF 1e9
#define MOD (int)1e9+7
#define debug(x) cerr << #x << ": " << x << "\n";
using namespace std;
using ll = long long;
using P = pair<int, int>;
vector<int> fact(1, 1);
// int fact[(int)2e6+10];
// return n! mod p
int factorial(int n, int p = INT_MAX){
if ((int) fact.size() <= n) {
int l = fact.size();
fact.resize(n+1);
for(int i = l; i <= n; i++) fact[i] = (ll) fact[i-1] * i % p;
}
return fact[n];
// if(fact[n] > 0) return fact[n];
// if(n == 0) return fact[n] = 1;
// int i = 0; while(fact[i] > 0) i++;
// for(int j = i; j <= n; j++){
// if(j == 0){
// fact[j] = 1;
// }else{
// fact[j] = (ll) fact[j - 1] * j % p;
// }
// }
// return fact[n];
}
//solve ax+by=gcd(a, b)
//return gcd(a, b)
int gcd(int a, int b, int* px = NULL, int* py = NULL){
if(py == NULL){
if(b == 0) return a;
return gcd(b, a % b);
}
int d = a;
if( b != 0){
d = gcd(b, a % b, py, px);
*py -= (a / b) * *px;
}else{
*px = 1; *py = 0;
}
return d;
}
// return a^(-1) mod m
int mod_inverse(int a, int m = MOD){
int x, y;
gcd(a, m, &x, &y);
return (m + x % m) % m;
}
// return a mod p (when n!=a*p^e), o(log_p n)
int mod_fact(int n, int& e, int p = MOD){
e = 0;
if(n == 0) return 1;
int res = mod_fact(n / p, e, p);
e += n / p;
if((n / p % 2 != 0)) return (ll) res * (p - factorial(n % p, p)) % p;
return (ll) res * factorial(n % p, p) % p;
}
// return nPk mod p
int perm(int n, int k, int p = MOD){
if(n < 0 or k < 0 or n < k) return 0;
int e1, e2;
int a1 = mod_fact(n, e1, p), a2 = mod_fact(n - k, e2, p);
return (ll) a1 * mod_inverse(a2, p) % p;
}
// return nCk mod p, o(log_p n)
int comb(int n, int k, int p = MOD){
if(n < 0 or k < 0 or n < k) return 0;
int e1, e2, e3;
int a1 = mod_fact(n, e1, p), a2 = mod_fact(k, e2, p), a3 = mod_fact(n - k, e3, p);
if(e1 > e2 + e3) return 0;
return (ll) a1 * mod_inverse((ll)a2 * a3 % p, p) % p;
}
// return nHk mod p
int hcomb(int n, int k, int p = MOD){
if(n < 0 or k < 0) return 0;
if(n == 0 and k == 0) return 1;
return comb(n + k - 1, k, p);
}
int main(void){
char c; int n, k, t, res;
cin >> t;
for(int i = 0; i < t; i++){
scanf(" %c(%d, %d)", &c, &n, &k);
if(c == 'C') res = comb(n, k);
else if(c == 'H') res = hcomb(n, k);
else if(c == 'P') res = perm(n, k);
cout << res << endl;
}
return 0;
}
youse_8zl