結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
goodbaton
|
| 提出日時 | 2018-12-25 14:25:12 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,337 ms / 9,973 ms |
| コード長 | 1,514 bytes |
| コンパイル時間 | 878 ms |
| コンパイル使用メモリ | 100,432 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-16 23:10:45 |
| 合計ジャッジ時間 | 4,867 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cstdint>
#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <cassert>
typedef long long ll;
using namespace std;
#ifdef LOCAL
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
#else
#define debug(x) ;
#endif
#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010
typedef __int128_t Int;
Int power(Int k, Int n, ll M){
if(n==0) return 1;
if(n==1) return k % M;
Int res = power(k, n/2, M);
res = res * res % M;
if(n % 2)
res = k % M * res % M;
return res;
}
bool isPrime(ll x) {
srand(time(NULL));
int a[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, -1}; // x < 2^64
if(x == 2) return true;
if(x <= 1 || x % 2 == 0) return false;
Int d = x-1; int s = 0;
for(;d%2 == 0;s++) d >>= 1;
for(int i=0;a[i] != -1 && a[i] < x;i++){
Int t = d;
Int y = power(a[i], t, x);
bool f = true;
for(int j=0;j<s;j++){
f &= power(a[i], t, x) != x - 1;
t <<= 1;
}
if(y != 1 && f) return false;
}
return true;
}
int main(){
int n;
ll x;
cin >> n;
for(int i=0;i<n;i++){
cin >> x;
cout << x << " " << isPrime(x) << endl;
}
return 0;
}
goodbaton