結果
| 問題 |
No.117 組み合わせの数
|
| ユーザー |
mayoko_
|
| 提出日時 | 2015-01-05 00:07:11 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,866 bytes |
| コンパイル時間 | 669 ms |
| コンパイル使用メモリ | 76,996 KB |
| 実行使用メモリ | 18,956 KB |
| 最終ジャッジ日時 | 2024-06-13 02:30:55 |
| 合計ジャッジ時間 | 1,633 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 1 |
ソースコード
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define INF 1000000000
using namespace std;
typedef long long ll;
const ll MAX = 2000010;
const ll MOD = 1000000007;
ll amari[MAX];
// extgcd
ll extgcd(ll a, ll b, ll& x, ll& y) {
ll d = a;
if (b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1; y = 0;
}
return d;
}
// mod_inverse
ll mod_inverse(ll a, ll m) {
ll x, y;
extgcd(a, m, x, y);
return (m+x%m) % m;
}
void init() {
amari[0] = 1;
amari[1] = 1;
for (ll i = 2; i < MAX; i++) {
amari[i] = (amari[i-1] * i) % MOD;
}
}
int main(void) {
int T;
cin >> T;
init();
while (T--) {
string s;
cin >> s;
string a, b;
int tmp = 0;
while (s[tmp] != ',') tmp++;
a = s.substr(2, tmp-2);
b = s.substr(tmp+1, s.size()-tmp-2);
ll N = stoll(a);
ll K = stoll(b);
if (s[0] == 'P') {
if (N < K) {
cout << 0 << endl;
continue;
}
cout << (amari[N] * mod_inverse(amari[N-K], MOD)) % MOD << endl;
} else if (s[0] == 'C') {
if (N < K) {
cout << 0 << endl;
continue;
}
ll first = (amari[N] * mod_inverse(amari[K], MOD)) % MOD;
cout << (first * mod_inverse(amari[N-K], MOD)) % MOD << endl;
} else if (s[0] == 'H') {
ll NN = N+K-1;
ll first = (amari[NN] * mod_inverse(amari[K], MOD)) % MOD;
cout << (first * mod_inverse(amari[NN-K], MOD)) % MOD << endl;
}
}
return 0;
}
mayoko_