結果

問題 No.1140 EXPotentiaLLL!
ユーザー akaneakane
提出日時 2020-07-31 22:16:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,271 bytes
コンパイル時間 1,865 ms
コンパイル使用メモリ 197,868 KB
実行使用メモリ 11,960 KB
最終ジャッジ日時 2023-09-20 23:50:29
合計ジャッジ時間 8,942 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
      x %= mod;
    }
    a = a*a;
    a %= mod;
    n >>= 1;//bit全体を右に1つシフトして一番右を捨てる。
  }
  return x;
}

int main(){
    int testcase; cin>> testcase;
    while(testcase--){
        ll A,P; cin>>A>>P;
        mod = P;
        // Aが素数かどうか。
        bool pflg = false;
        if(P==1){
            cout << -1 << endl;
            pflg = true;
        }else{
            for(int i=2; i*i<=P; i++){
                if(P%i==0){
                    cout << -1 << endl;
                    pflg = true; break;
                }
            }
        }
        if(pflg) continue;

        // Pが素数の時
        ll rA=A%P;

        ll rA1 = pow_kai(rA,P);
        // cerr << rA1 << endl;
        ll rA2 = pow_kai(rA,(P-1)/2);
        // cerr << rA2 << endl;
        ll ans = (rA1 * rA2)%mod;

        // for(ll i=P; i>0; i--){
        //     rA = pow_kai(rA, i);
        // }
        cout << ans << endl;
    }
}
0