結果

問題 No.109 N! mod M
ユーザー piyoko_212
提出日時 2015-12-28 19:32:57
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 789 bytes
コンパイル時間 326 ms
コンパイル使用メモリ 35,276 KB
実行使用メモリ 6,940 KB
最終ジャッジ日時 2024-09-19 07:57:27
合計ジャッジ時間 2,341 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 8 WA * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:17:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   17 |         scanf("%d",&T);
      |         ~~~~~^~~~~~~~~
main.cpp:19:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   19 |                 int a,b;scanf("%d%d",&a,&b);
      |                         ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

#include<stdio.h>
#include<algorithm>
using namespace std;
long long extgcd(long long a,long long b,long long&x,long long&y){
	for(long long u=y=1,v=x=0;a;){
		long long q=b/a;swap(x-=q*u,u);swap(y-=q*v,v);swap(b-=q*a,a);
	}
	return b;
}
long long mod_inv(long long a,long long m){
	long long x,y;
	extgcd(a,m,x,y);
	return (m+x%m)%m;
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int a,b;scanf("%d%d",&a,&b);
		if(a>=b){
			printf("0\n");continue;
		}
		if(a<300000){
			long long ret=1;
			for(int i=1;i<=a;i++)ret=ret*i%b;
			printf("%lld\n",ret);continue;
		}
		bool hp=false;
		for(int i=2;i*i<=b;i++){
			if(b%i==0)hp=true;
		}
		if(hp){
			printf("0\n");continue;
		}
		long long now=b-1;
		for(int i=b-1;i>a;i--){
			now=now*mod_inv(i,b)%b;
		}
		printf("%lld\n",now);
	}
}
0