結果

問題 No.109 N! mod M
ユーザー piyoko_212piyoko_212
提出日時 2015-12-28 19:32:57
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 789 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 35,196 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 11:51:06
合計ジャッジ時間 2,410 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 75 ms
4,348 KB
testcase_02 AC 97 ms
4,348 KB
testcase_03 AC 0 ms
4,348 KB
testcase_04 AC 21 ms
4,348 KB
testcase_05 AC 1,175 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 WA -
testcase_08 AC 0 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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