結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー soraie_
提出日時 2021-03-24 15:04:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 7,839 bytes
コンパイル時間 7,939 ms
コンパイル使用メモリ 279,148 KB
最終ジャッジ日時 2025-01-19 21:24:33
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 9 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using namespace std;
//--------------------------------------------------------------------
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define overload4(_1,_2,_3,_4,name,...) name
#define rep1(n) for(ll _=0;_<(ll)n;++_)
#define rep2(i,n) for(ll i=0;i<(ll)n;++i)
#define rep3(i,a,b) for(ll i=(ll)a;i<(ll)b;++i)
#define rep4(i,a,b,c) for(ll i=(ll)a;i<(ll)b;i+=(ll)c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#ifdef _DEBUG
#define pass(...) __VA_ARGS__ ;

#define debug1(a) cerr<<#a<<": "<<a<<"\n"
#define debug2(a,b) cerr<<#a<<": "<<a<<", "<<#b<<": "<<b<<"\n"
#define debug3(a,b,c) cerr<<#a<<": "<<a<<", "<<#b<<": "<<b<<", "<<#c<<": "<<c<<"\n"
#define debug4(a,b,c,d) cerr<<#a<<": "<<a<<", "<<#b<<": "<<b<<", "<<#c<<": "<<c<<", "<<#d<<": "<<d<<"\n"

/*
#define debug1(a) cout<<#a<<": "<<a<<"\n"
#define debug2(a,b) cout<<#a<<": "<<a<<", "<<#b<<": "<<b<<"\n"
#define debug3(a,b,c) cout<<#a<<": "<<a<<", "<<#b<<": "<<b<<", "<<#c<<": "<<c<<"\n"
#define debug4(a,b,c,d) cout<<#a<<": "<<a<<", "<<#b<<": "<<b<<", "<<#c<<": "<<c<<", "<<#d<<": "<<d<<"\n"
*/

#define debug(...) overload4(__VA_ARGS__,debug4,debug3,debug2,debug1)(__VA_ARGS__)
#define koko cerr << "koko\n";
#else
#define debug(...) void(0)
#define pass(...) void(0);
#define koko void(0);
#endif
#define mp make_pair
//#define fi first
//#define se second
void myset(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
void doset(int n){cout << fixed << setprecision(n);cerr << fixed << setprecision(n);}
using ll = long long;
using ld = long double;
using dou = double;
template<class First,class Second>ostream& operator<<(ostream& os,const pair<First,Second>& pp)
{return os << "{" << pp.first << "," << pp.second << "}";}
template<class T>ostream& operator<<(ostream& os,const vector<T>& VV)
{if(VV.empty())return os<<"[]";os<<"[";rep(i,VV.size())os<<VV[i]<<(i==int(VV.size()-1)?"]":",");return os;}
template<class T>ostream& operator<<(ostream& os,const set<T>& SS)
{if(SS.empty())return os<<"[]";os<<"[";auto ii=SS.begin();for(;ii!=SS.end();ii++)os<<*ii<<(ii==prev(SS.end())?"]":",");return os;}
template<class Key,class Tp>ostream& operator<<(ostream& os,const map<Key,Tp>& MM)
{if(MM.empty())return os<<"[]";os<<"[";auto ii=MM.begin();for(;ii!=MM.end();ii++)os<<"{"<<ii->first<<":"<<ii->second<<"}"<<(ii==prev(MM.end())?"]":",");return os;}
const int inf = 1 << 30;
const ll INF = 1LL << 61;
const ld pi = 3.14159265358;
const ll mod1 = 1000000007LL;
const ll mod2 = 998244353LL;
typedef pair<ll,ll> P;
template<class T, class U> inline bool chmin(T& a, const U& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T, class U> inline bool chmax(T& a, const U& b){ if(a < b){ a = b; return 1; } return 0; }
ll modpow(ll n,ll m,ll MOD){
    if(m == 0)return 1;
    if(m < 0)return 0;
    ll res = 1;
    n %= MOD;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}
ll mypow(ll n,ll m){
    if(m == 0)return 1;
    if(m < 0)return -1;
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n);
        m >>= 1;
        n *= n;
    }
    return res;
}

inline bool isp(ll n){
    bool res = true;
    if(n == 1 || n == 0)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}
inline bool Yes(bool b = 1){cout << (b ? "Yes\n":"No\n");return b;}
inline bool YES(bool b = 1){cout << (b ? "YES\n":"NO\n");return b;}
map<ll,ll> primefactor(ll n){
    map<ll,ll> ma;
    if(n <= 1)return ma;
    ll m = n;
    for(ll i = 2;i * i <= n;i++){
        while(m % i == 0){
            ma[i]++;
            m /= i;
        }
    }
    if(m != 1)ma[m]++;
    return ma;
}
vector<ll> divisor(ll n,bool sorted = true,bool samein = false){
    vector<ll> res;
    for(ll i = 1;i * i <= n;i++){
        if(n % i == 0){
            res.push_back(i);
            if(i * i != n || samein)res.push_back(n / i);
        }
    }
    if(sorted)sort(all(res));
    return res;
}

ll __lcm(ll a,ll b){return a / __gcd(a,b) * b;}

template<class T>T sum(const vector<T> &V){T r=0;for(auto x:V)r+=x;return r;}
template<class T>T sum(const initializer_list<T> &V){T r=0;for(auto x:V)r+=x;return r;}


#include <atcoder/all>
using namespace atcoder;


//--------------------------------------------------------------------





namespace prime{
    long long inner_modmul(long long x,long long y,long long p){
        #ifdef __SIZEOF_INT128__
        return __int128_t(x) * y % p;
        #else
        unsigned long long q = long double(x) * y / p;
        long long res = long long(x * y - p * q) % p;
        return res < 0 ? res + p : res;
        #endif
    }

    long long inner_modpow(long long n,long long m,long long p){
        long long res = 1;
        while(m){
            if(m & 1)res = inner_modmul(res,n,p);
            m >>= 1;
            n = inner_modmul(n,n,p);
        }
        return res;
    }

    bool miller_rabin(long long n,const std::vector<long long> as){
        long long s = __builtin_ctzll(n - 1),d = n >> s;
        for(long long a : as){
            if(n <= a)break;
            long long t = d,y = inner_modpow(a,t,n);
            while(t != n - 1 && y != 1 && y != (n - 1)){
                y = inner_modmul(y,y,n);
                t = inner_modmul(t,2,n);
            }
            if(y != n - 1 && ~t & 1)return false;
        }
        return true;
    }

    bool is_prime(long long n){
        if(n <= 2)return n == 2;
        else if(~n & 1)return false;
        else if(n <= 4'759'123'141)return miller_rabin(n,{2,7,61});
        else return miller_rabin(n,{2,325,9375,28178,450775,9780504,1795265022});
    }

    long long factor(long long n){
        if(~n & 1)return 2;
        else if(is_prime(n))return n;
        int c = 2;
        while(true){
            auto f = [&](long long x){
                long long r = inner_modmul(x,x,n) + c;
                if(r >= n)r -= n;
                return r;
            };
            long long x = c,g = 1,q = 1,xs,y;
            constexpr int m = 128;
            int l = 1;
            while(g == 1){
                y = x;
                for(int i = 1;i < l;i++){
                    x = f(x);
                }
                int k = 0;
                while(k < l && g == 1){
                    xs = x;
                    for(int i = 0;i < m && i < l - k;i++){
                        x = f(x);
                        q = inner_modmul(q,std::llabs(y - x),n);
                    }
                    g = std::__gcd(q,n);
                    k += m;
                }
                l *= 2;
            }
            if(g == n){
                do{
                    xs = f(xs);
                    g = std::__gcd(std::llabs(xs - y),n);
                }while(g == 1);
            }
            if(g != n)return g;
            c++;
        }
    }

    std::vector<long long> factorize_sub(long long n){
        if(n <= 1)return {};
        long long p = factor(n);
        if(p == n)return {p};
        auto l = factorize_sub(n / p);
        auto r = factorize_sub(p);
        copy(begin(r),end(r),back_inserter(l));
        return l;
    }

    std::vector<long long> factorize(long long n,bool sorted = true){
        auto res = factorize_sub(n);
        if(sorted)std::sort(res.begin(),res.end());
        return res;
    }
};
using prime::factorize;
using prime::is_prime;




int main(){
    myset();
    int T;
    cin >> T;
    while(T--){
        ll N;
        cin >> N;
        cout << N << " " << is_prime(N) << "\n";
    }
}
0