結果

問題 No.1140 EXPotentiaLLL!
ユーザー akane
提出日時 2020-07-31 23:14:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,334 bytes
コンパイル時間 2,318 ms
コンパイル使用メモリ 195,988 KB
最終ジャッジ日時 2025-01-12 11:37:56
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 2 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i < (n); i++)
using namespace std;
using ll = long long;

ll mod;

ll pow_kai(ll a, ll n){//aのn乗を計算します。
  ll x = 1;
  while(n > 0){//全てのbitが捨てられるまで。
    if(n&1){//1番右のbitが1のとき。
      x = (x*a)%mod;
      // x %= mod;
    }
    a = (a*a)%mod;
    // a %= mod;
    n >>= 1;//bit全体を右に1つシフトして一番右を捨てる。
  }
  return x;
}

int main(){
    int testcase; cin>> testcase;
    vector<ll> vp(6000000+1,0);
    // set<ll> prime;
    for(int i=2; i*i<=6000000; i++){
        ll x=i+i;
        if(vp[x]==1) continue;
        while(x<=6000000){
            vp[x]=1;
            x += i;
        }
    }
    // mod = 70;
    // cerr << pow_kai(3,4) << endl;

    while(testcase--){
        ll A,P; cin>>A>>P;
        mod = P;
        // Aが素数かどうか。
        bool pflg = false;
        if(vp[P]==0 && P>=2){
          pflg=true;
        }else{
          cout << -1 << endl;
          continue;
        }

        // Pが素数の時
        ll rA=A%P;
        ll k = (P-1)/2 + 1;
        // while(k--){
        //   rA = (rA*rA)%mod;
        // }
        // cout << rA << endl;
        ll ans = pow_kai(rA,k);
        if(ans<0) ans += mod;
        cout << ans << endl;

        

    }
}
0