結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2019-09-04 07:38:41 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,361 bytes |
| コンパイル時間 | 908 ms |
| コンパイル使用メモリ | 95,220 KB |
| 実行使用メモリ | 8,448 KB |
| 最終ジャッジ日時 | 2024-11-18 16:55:53 |
| 合計ジャッジ時間 | 53,139 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 6 TLE * 4 |
ソースコード
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<ctime>
#include <random>
#include <limits>
using namespace std;
#define int __int128
#define endl "\n"
int power(int x, int n, int mod){
int ans = 1;
while(n){
if(n&1) ans *= x;
n >>= 1;
x *= x;
x %= mod;
ans %= mod;
}
return ans;
}
bool Miller_Rabin(int num, int k){
if(num <= 1) return false;
else if(num == 2) return true;
else if(num%2 == 0) return false;
int s = 0, d = 0;
random_device rnd;
mt19937 mt(rnd());
// uniform_int_distribution<> rd(1,min((int)numeric_limits<int>::max(), num-1));
for(int n = num-1;;){
if(n%2){
d = n; break;
} else s++, n >>= 1;
}
for(int i = 0; i < k; i++){
// long long a = rd(mt), n = d, a2 = 1;
int a = mt()%(num-1)+1, n = d, a2 = 1;
bool flag = true;
a2 = power(a, d, num);
if(a2 == 1) continue;
for(int 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);
long long n;
cin>>n;
for(int i = 0; i < n; i++){
long long x;
cin>>x;
cout<<x<<" "<<Miller_Rabin(x, 100000)<<endl;
}
return 0;
}