結果

問題 No.109 N! mod M
ユーザー 沙耶花沙耶花
提出日時 2021-10-26 16:33:31
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,186 ms / 5,000 ms
コード長 1,212 bytes
コンパイル時間 4,619 ms
コンパイル使用メモリ 258,044 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 15:35:37
合計ジャッジ時間 6,586 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 56 ms
5,376 KB
testcase_02 AC 32 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 18 ms
5,376 KB
testcase_05 AC 1,186 ms
5,376 KB
testcase_06 AC 6 ms
5,376 KB
testcase_07 AC 128 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000

long long get(long long n,long long m,int p,int cnt){
	if(n>=m)return 0;
	long long cur = n;
	long long t = 0;
	while(cur!=0){
		t += cur / p;
		cur /= p;
	}
	if(t >= cnt)return 0;
	long long ret;
	if(cnt==1){
		ret = p - 1;
		for(int i=p-1;i>=n+1;i--){
			ret *= inv_mod(i,m);
			ret %= m;
		}
	}
	else{
		ret = 1LL;
		for(int i=1;i<=n;i++){
			ret *= i;
			ret %= m;
		}
	}
	return ret;
	
}

int main(){
	
	int _t;
	cin>>_t;
	
	rep(_,_t){
		int n,m;
		cin>>n>>m;
		int mm = m;
		if(n>=m){
			cout<<0<<endl;
			continue;
		}
		
		vector<pair<int,int>> ps;
		for(int i=2;i*i<=m;i++){
			if(m%i==0){
				ps.emplace_back(i,0);
				while(m%i==0){
					m/=i;
					ps.back().second++;
				}
			}
		}
		if(m!=1)ps.emplace_back(m,1);
		m = mm;
		
		vector<long long> x,y;
		rep(i,ps.size()){
			long long temp = 1LL;
			rep(j,ps[i].second)temp *= ps[i].first;
			x.push_back(get(n,temp,ps[i].first,ps[i].second));
			y.push_back(temp);
			//cout<<x.back()<<','<<y.back()<<endl;
		}
		cout<<crt(x,y).first<<endl;
		
		
		
	}
	
	return 0;
}
0