結果

問題 No.109 N! mod M
ユーザー beetbeet
提出日時 2018-11-04 14:03:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,700 bytes
コンパイル時間 1,948 ms
コンパイル使用メモリ 198,176 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-13 03:17:51
合計ジャッジ時間 6,162 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 160 ms
4,376 KB
testcase_02 AC 133 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 42 ms
4,380 KB
testcase_05 AC 3,002 ms
4,380 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

int MOD = 1000000007;
template<typename T>
struct Mint{
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(int k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }
  
  Mint inv(){return pow(MOD-2);}
  
  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}
  
  Mint operator+(Mint a) const{return Mint(v)+=a;};
  Mint operator-(Mint a) const{return Mint(v)-=a;};
  Mint operator*(Mint a) const{return Mint(v)*=a;};
  Mint operator/(Mint a) const{return Mint(v)/=a;};

  Mint operator-(){return v?MOD-v:v;}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  
};

int isprime(int x){
  if(x<=1) return 0;
  for(int i=2;i*i<=x;i++)
    if(x%i==0) return 0;
  return 1;
}

//INSERT ABOVE HERE
signed main(){
  int T;
  cin>>T;
  using M = Mint<int>;
  while(T--){
    int n,m;
    cin>>n>>m;
    MOD=m;
    if(n>=m){
      cout<<0<<endl;
    }else if(isprime(m)){
      M ans(-1LL);
      for(int i=m-1;i>n;i--) ans/=M(i);
      cout<<ans.v<<endl;
    }else if(n<=1e6){
      M ans(1);
      for(int i=1;i<=n;i++) ans*=M(i);
      cout<<ans.v<<endl;
    }else{
      cout<<0<<endl;
    }
  }
  return 0;
}
0