結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー konishikonishi
提出日時 2023-11-07 09:41:07
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 9,973 ms
コード長 2,220 bytes
コンパイル時間 3,099 ms
コンパイル使用メモリ 245,724 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-11-07 09:41:12
合計ジャッジ時間 4,912 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 156 ms
4,348 KB
testcase_05 AC 153 ms
4,348 KB
testcase_06 AC 73 ms
4,348 KB
testcase_07 AC 72 ms
4,348 KB
testcase_08 AC 73 ms
4,348 KB
testcase_09 AC 262 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define pii pair<int,int>
#define pll pair<ll,ll>

#define rep(i,num,n) for(int i=num;i<(int)(n);i++) //for_loop
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define rrep(i,num,n) for(int i=num-1;i>=(int)(n);i--) //reverse_for>
#define in(x,a,b) (a<=x && x<b)//範囲
#define reo return 0
#define all(v) v.begin(),v.end()

#define INFI 1010000000
#define INFL 4000000000000000000
//#define ten(x) ((int)1e##x)
//#define tenll(x) ((ll)1e##x)
#define PI 3.14159265358979
//#define mint modint1000000007
#define mint modint998244353

bool chmin(ll &a,ll b){if(a>b){a=b;return true;}return false;}
bool chmax(ll &a,ll b){if(a<b){a=b;return true;}return false;}
bool chmin(int &a,int b){if(a>b){a=b;return true;}return false;}
bool chmax(int &a,int b){if(a<b){a=b;return true;}return false;}

int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};
//int dx[]={1,1,1,0,0,-1,-1,-1},dy[]={1,0,-1,1,-1,1,0,-1};
//----------------------------
uint64_t modmul(uint64_t a, uint64_t b, uint64_t n) {
    return (uint64_t)(((__uint128_t)a) * ((__uint128_t)b) % ((__uint128_t)n));
}
uint64_t modpow(uint64_t a, uint64_t b, uint64_t n) {
    uint64_t t = ((b & 1) == 0) ? 1 : a;
    for (b >>= 1; b != 0; b >>= 1) {
        a = modmul(a, a, n);
	if ((b & 1) == 1) { t = modmul(t, a, n); } 
    }
    return t;
}

const uint64_t bases[] = {2,325,9375,28178,450775,9780504,1795265022};
bool miller_rabin(uint64_t n){
    if(n==2)return true;
    if(n<2 ||(n&1)==0)return false;
    uint64_t  n1=n-1,d=n-1;
    uint64_t  s=0;
    for(;(d&1)==0;d>>=1)s++;//s=dの二進桁数
    for(auto &base:bases){
        ll a=base;
        if(a>=n)
        {
            a%=n;
            if(a==0)continue;
        }
    
    uint64_t  t=modpow(a,d,n);
    if(t==1)continue;
    for(uint64_t  j=1;t!=n1;j++){
        if(j>=s)return false;
    t=modmul(t,t,n);

    }
    }
    return true;
}

int  main(){
    int n;cin>>n;
    rep(i,0,n){
        uint64_t a;cin>>a;cout<<a<<" ";
        //ll a=i;cout<<a<<" ";
        if(miller_rabin(a))cout<<1<<endl;
        else cout<<0<<endl;
    }
}
0