結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー cielciel
提出日時 2017-10-22 03:55:11
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 3,637 ms / 9,973 ms
コード長 1,083 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 32,256 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 08:50:07
合計ジャッジ時間 8,031 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1,769 ms
5,376 KB
testcase_05 AC 1,554 ms
5,376 KB
testcase_06 AC 60 ms
5,376 KB
testcase_07 AC 54 ms
5,376 KB
testcase_08 AC 53 ms
5,376 KB
testcase_09 AC 3,637 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
typedef unsigned long long ull;

#if __SIZEOF_POINTER__==8
ull powmod(__uint128_t x,ull y,ull m){
	__uint128_t z=1;
	for(;y;y>>=1){
		if(y&1)z=z*x%m;
		x=x*x%m;
	}
	return z;
}
#elif __SIZEOF_POINTER__==4
ull mul(ull x,ull y,ull m){
	ull z=0;
	for(;y;y>>=1){
		if(y&1)z=(z+x)%m;
		x=(x+x)%m;
	}
	return z;
}
ull powmod(ull x,ull y,ull m){
	ull z=1;
	for(;y;y>>=1){
		if(y&1)z=mul(z,x,m);
		x=mul(x,x,m);
	}
	return z;
}
#else
#error powmod is not supported
#endif

ull gcd(ull x,ull y){return y?gcd(y,x%y):x;}
bool miller_rabin(ull n){
	if(n==2)return true;
	if(n==1||n%2==0)return false;
	ull d=n-1,s=0,a=1;
	for(;d%2==0;d/=2)s+=1;
	for(int k=99;k--;){ //todo
		//for(a++;gcd(a,n)!=1;a++); //todo
		for(a=0;gcd(a,n)!=1;)a=rand()%~-n+1;
		ull r=powmod(a,d,n);
		if(r==1||r==n-1)continue;
		int t=s;
		for(;t;t--){
			r=powmod(r,2,n);
			if(r==n-1)break;
		}
		if(!t)return false;
	}
	return true;
}

int main(){
	ull t,n;
	for(scanf("%llu",&t);t--;printf("%llu %d\n",n,miller_rabin(n)))scanf("%llu",&n);
}
0