結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
AC2K
|
| 提出日時 | 2023-01-26 19:36:01 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,297 bytes |
| コンパイル時間 | 2,389 ms |
| コンパイル使用メモリ | 205,540 KB |
| 最終ジャッジ日時 | 2025-02-10 07:04:53 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 5 TLE * 5 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define rep(i, N) for(int i=0;i<(N);i++)
#define all(x) (x).begin(),(x).end()
#define popcount(x) __builtin_popcount(x)
using ll = long long;
//using i128=__int128_t;
using ld = long double;
using graph = vector<vector<int>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll infl = 1e18;
const ld eps = 1e-6;
const long double pi = acos(-1);
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const int dx[4] = { 1,0,-1,0 };
const int dy[4] = { 0,1,0,-1 };
template<class T>inline void chmax(T&x,T y){if(x<y)x=y;}
template<class T>inline void chmin(T&x,T y){if(x>y)x=y;}
class MillerRabin {
//底
using ll = long long;
using i128 = __int128_t;
const vector<ll> bases = { 2 , 7 , 61 };
i128 mod_pow(i128 base, i128 exp, ll mod) {
i128 ans = 1;
base %= mod;
while (exp) {
if (exp & 1) {
ans *= base;
ans %= mod;
}
base *= base;
base %= mod;
exp >>= 1;
}
return ans;
}
//CHECK!!!
public:
bool is_prime(ll n) {
if (n < 2) {
return false;
}
//2^q*d==n-1となるように分解する
ll d = n - 1;
ll q = 0;
while ((d & 1) == 0) {
d >>= 1;
q++;
}
for (ll a : bases) {
if (a == n) {
return true;
}
if (mod_pow(a, d, n) != 1) {
bool flag = true;
for (ll r = 0; r < q; r++) {
ll pow = mod_pow(a, d * (1ll << r), n);
if (pow == n - 1) {
flag = false;
break;
}
}
if (flag) {
return false;
}
}
}
return true;
}
};
//ポラード・ロー
class Rho{
//using ll=long long:
using i128=__int128_t;
mt19937 mt; //メルセンヌツイスタ~
MillerRabin MR;
long long c;
ll f(i128 x,ll n){
x%=n;
return (x*x%n+c)%n;
}
public:
Rho(){
mt.seed(clock());
}
ll find_factor(ll n){
if(n==4){
return 2;
}
c=mt()%n;
ll x=mt()%n;
ll y=x;
ll d=1;
while(d==1){
x=f(x,n);
y=f(f(y,n),n);
d=__gcd(abs(x-y),n);
}
if(d==n){
return -1; //失敗!!!
}
return d; //見つかってる
}
vector<ll> fact(const ll n,bool sorting=true){
if(n<2){
return {};
}
if(MR.is_prime(n)){
return{n};
}
ll res=-1;
while(res==-1){
res=find_factor(n);
}
vector<ll> V1=fact(res,false);
vector<ll> V2=fact(n/res,false);
V1.insert(V1.end(),V2.begin(),V2.end());
if(sorting)sort(V1.begin(),V1.end());
return V1;
}
};
int main() {
Rho rho;
int n;
cin>>n;
while(n--){
ll x;
cin>>x;
auto res=rho.fact(x);
cout<<x<<' ';
if(res.size()==1){
cout<<1<<'\n';
}else{
cout<<0<<'\n';
}
}
}
AC2K