結果

問題 No.1140 EXPotentiaLLL!
ユーザー makichan
提出日時 2025-01-29 12:31:08
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 947 ms / 2,000 ms
コード長 1,541 bytes
コンパイル時間 5,525 ms
コンパイル使用メモリ 333,568 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2025-01-29 12:31:24
合計ジャッジ時間 14,661 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using mint = atcoder::static_modint<998244353>;
// using mint = atcoder::static_modint<1000000007>;
using namespace std;
using namespace atcoder;
using ld = long double;
using ll = long long;
#define mp(a,b) make_pair(a,b)
#define rep(i,s,n) for(int i=s; i<n; i++)
const vector<int> dx{1,0,-1,0},dy{0,1,0,-1};

bool is_prime(ll x){
    // miller rabin
    auto pow_mod=[&](__int128_t a,ll k,__int128_t mod){
        __int128_t output=1;
        a%=mod;
        while(k){
            if(k&1)output=(output*a)%mod;
            a=(a*a)%mod;
            k>>=1;
        }
        return output;
    };
    if(x<=1)return false;
    vector<ll> test;
    if(x<4759123141)test={2,7,61};
    else test={2,325,9375,28178,450775,9780504,1795265022};

    ll K=x-1;
    while(!(K&1))K>>=1;

    for(ll a:test){
        if(a>=x)return true;
        bool ok=false;
        ll k=K;
        __int128_t c=pow_mod(a,k,x);
        if(c==1)ok=true;
        else{
            while(k<=x-1){
                if(c==x-1){
                    ok=true;
                    break;
                }
                c=c*c%x;
                k<<=1;
            }
        }
        if(!ok)return false;
    }
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;cin >> T;
    rep(i,0,T){
        ll a,p;cin >> a >> p;
        a%=p;
        if(is_prime(p)){
            if(a)cout << 1 << "\n";
            else cout << 0 << "\n";
        }
        else cout << -1 << "\n";
    }
}
0