結果
| 問題 |
No.109 N! mod M
|
| ユーザー |
maine_honzuki
|
| 提出日時 | 2020-05-14 17:28:46 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,216 bytes |
| コンパイル時間 | 1,563 ms |
| コンパイル使用メモリ | 167,056 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-15 12:07:37 |
| 合計ジャッジ時間 | 5,366 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 8 WA * 1 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
bool isprime(long long x) {
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
int main() {
int T;
cin >> T;
while (T--) {
long long N, M;
cin >> N >> M;
if (N < 1e5) {
long long ans = 1;
for (int i = 2; i <= N; i++) {
(ans *= i) %= M;
}
cout << ans << endl;
continue;
}
if (N >= M || M == 1) {
cout << 0 << endl;
continue;
}
if (!isprime(M)) {
cout << 0 << endl;
continue;
}
auto modpow = [&](long long a, long long n) {
long long t = 1;
while (n) {
if (n & 1)
(t *= a) %= M;
(a *= a) %= M;
n >>= 1;
}
return t;
};
auto inv = [&](long long a) {
return modpow(a, M - 2);
};
long long ans = M - 1;
for (long long i = M - 1; i > N; i--)
(ans *= inv(i)) %= M;
cout << ans << endl;
}
}
maine_honzuki