結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー cielciel
提出日時 2017-10-22 16:40:47
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,180 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 33,152 KB
実行使用メモリ 8,608 KB
最終ジャッジ日時 2024-04-29 12:41:36
合計ジャッジ時間 11,672 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

long long mul(long long x,long long y,long long m){
/*
	ull z=0;
	for(;y;y>>=1){
		if(y&1)z=(z+x)%m;
		x=(x+x)%m;
	}
	return z;
*/
	long long q=((double)x*(double)y/(double)m);
	long long r=x*y-m*q;
	r=(r%m+m)%m;
	return r;
}
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;
}

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
		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;
}

void solve(ull n){
	if(n%2==0){puts(n>2?"Yes":"No");return;}
	for(ull m=2;m<n;m*=2){
		ull z=n-m;
		for(int k=1;k<61;k++){
			ull r=k==1?z:expl(logl(z+1)/k)+1e-6;
			if(m+powl(r,k)!=n)continue;
			if(miller_rabin(r)){puts("Yes");return;}
		}
	}
	puts("No");
}
int main(){
	ull t,n;
	for(scanf("%llu",&t);t--;solve(n))scanf("%llu",&n);
}
0