結果

問題 No.3023 素数判定するだけ
ユーザー okok
提出日時 2019-09-04 07:33:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,374 bytes
コンパイル時間 879 ms
コンパイル使用メモリ 93,460 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-26 21:32:22
合計ジャッジ時間 6,050 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

#include<ctime>
#include <random>
#include <limits>

using namespace std;

#define int long long
#define endl "\n"


long long power(long long x, long long n, long long mod){
	long long ans = 1;
	while(n){
		if(n&1) ans *= x;
		n >>= 1;
		x *= x;
		x %= mod;
		ans %= mod;
	}
	return ans;
}

bool Miller_Rabin(long long num, long long k){
	if(num <= 1)        return false;
	else if(num == 2)   return true;
	else if(num%2 == 0) return false; 
	
	long long s = 0, d = 0;
	random_device rnd;
	mt19937 mt(rnd()); 
	// uniform_int_distribution<> rd(1,min((int)numeric_limits<int>::max(), num-1));
	
	for(long long n = num-1;;){
		if(n%2){ 
			d = n; break;
		} else s++, n >>= 1;
	}
	
	for(long long i = 0; i < k; i++){
		// long long a = rd(mt), n = d, a2 = 1;
		long long a = mt()%(num-1)+1, n = d, a2 = 1;
		bool flag = true;
		a2 = power(a, d, num);
		
		if(a2 == 1) continue;
		for(long long r = 0, two = 1; r < s; r++, two <<= 1){
			a2 = power(a, two*d, num);
			
			if(a2 == num-1){
				flag = false;
				break;
			}
		}
		
		if(flag) return false;
	}
	
	return true;
}

signed main(){
	// cin.tie(0);
	// ios::sync_with_stdio(false);
	// cout<<fixed<<setprecision(10);
	
	int n;
	
	cin>>n;
	
	cout<<(Miller_Rabin(n, 100000)?"YES":"NO")<<endl;

	
	
	return 0;
}
0