結果

問題 No.109 N! mod M
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-05-26 12:28:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,003 ms / 5,000 ms
コード長 2,278 bytes
コンパイル時間 1,641 ms
コンパイル使用メモリ 168,132 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 00:11:02
合計ジャッジ時間 5,382 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 95 ms
4,348 KB
testcase_02 AC 51 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 42 ms
4,348 KB
testcase_05 AC 3,003 ms
4,348 KB
testcase_06 AC 6 ms
4,348 KB
testcase_07 AC 9 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
int add(int mod, int x, int y) { return (x += y) >= mod ? x - mod : x; }
template<class... T> int add(int mod, int x, T... y) { return add(mod, x, add(mod, y...)); }
int mul(int mod, int x, int y) { return 1LL * x * y % mod; }
template<class... T> int mul(int mod, int x, T... y) { return mul(mod, x, mul(mod, y...)); }
int sub(int mod, int x, int y) { return add(mod, x, mod - y); }
int modpow(int mod, int a, long long b) { int ret = 1; while (b > 0) {
    if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1; } return ret; }
int modinv(int mod, int a) { return modpow(mod, a, mod - 2); }
//---------------------------------------------------------------------------------------------------
bool isprime(int v) {
    if (v == 1) return false;
    for (int i = 2; 1LL * i*i <= v; i++) if (v%i == 0) return false;
    return true;
}
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/



int N, M;
//---------------------------------------------------------------------------------------------------
void solve() {
    cin >> N >> M;

    int ans = 1;
    if (M <= N || M == 1) {
        ans = 0;
    } else if (N < 101010) {
        rep(i, 1, N + 1) ans = mul(M, ans, i);
    } else if(isprime(M)) {
        ans = M - 1;
        rep(i, N + 1, M) ans = mul(M, ans, modinv(M, i));
    } else ans = 0;
    cout << ans << endl;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    int T; cin >> T;
    rep(t, 0, T) solve();
}
0