結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2018-04-09 21:14:53 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,897 bytes |
| コンパイル時間 | 907 ms |
| コンパイル使用メモリ | 103,604 KB |
| 実行使用メモリ | 10,024 KB |
| 最終ジャッジ日時 | 2024-11-18 16:27:44 |
| 合計ジャッジ時間 | 38,194 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 7 TLE * 3 |
ソースコード
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;
class Xorshift
{
private:
unsigned x, y, z, w;
public:
Xorshift(unsigned seed=88675123, int loop=50){
x = 123456789;
y = 362436069;
z = 521288629;
w = seed;
while(--loop >= 0){
(*this)();
}
}
unsigned operator()(){
unsigned t=(x^(x<<11));
x=y; y=z; z=w;
return w=(w^(w>>19))^(t^(t>>8));
}
unsigned operator()(unsigned size){
return (*this)() % size;
}
unsigned long long get64(){
unsigned long long a = (*this)();
unsigned long long b = (*this)();
return (a << 32) | b;
}
unsigned long long get64(unsigned long long size){
return get64() % size;
}
template <class T>
void shufflte(vector<T>& v){
unsigned n = v.size();
for(unsigned i=0; i<n-1; ++i){
unsigned j = (*this)(n-i) + i;
swap(v[i], v[j]);
}
}
};
Xorshift xorshift;
unsigned long long modmul(unsigned long long a, unsigned long long b, unsigned long long mod)
{
unsigned long long ret = 0;
while(b > 0){
if(b & 1){
ret += a;
ret %= mod;
}
a *= 2;
a %= mod;
b >>= 1;
}
return ret;
}
unsigned long long modpow(unsigned long long a, unsigned long long b, unsigned long long mod)
{
unsigned long long ret = 1;
unsigned long long tmp = a;
while(b > 0){
if(b & 1)
ret = modmul(ret, tmp, mod);
tmp = modmul(tmp, tmp, mod);
b >>= 1;
}
return ret;
}
bool millerRabinPrimalityTest(long long n, int loop=30)
{
if(n < 2)
return false;
long long d = n - 1;
int s = 0;
while(d % 2 == 0){
++ s;
d /= 2;
}
while(--loop >= 0){
long long a = xorshift.get64(n-1) + 1;
long long x = modpow(a, d, n);
if(x != 1){
bool isComposite = true;
for(int r=0; r<s; ++r){
if(x == n - 1){
isComposite = false;
break;
}
x = modmul(x, x, n);
}
if(isComposite)
return false;
}
}
return true;
}
int main()
{
int n;
cin >> n;
while(--n >= 0){
long long x;
cin >> x;
int ans = millerRabinPrimalityTest(x) ? 1 : 0;
cout << x << ' ' << ans << endl;
}
return 0;
}