結果

問題 No.1140 EXPotentiaLLL!
ユーザー hayaten
提出日時 2020-07-31 22:35:17
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,108 bytes
コンパイル時間 1,348 ms
コンパイル使用メモリ 168,404 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-06 19:48:18
合計ジャッジ時間 17,305 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 3 WA * 6 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define ALL(v) v.begin(), v.end()
using namespace std;
using P = pair<int, int>;
typedef long long ll;

bool isPrime(int num) {
  if (num < 2) return false;
  else if (num == 2) return true;
  else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }
  // 素数である
  return true;
}

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

void solve() {
  ll a;
  int b;
  cin >> a >> b;
  if(!isPrime(b)){
    cout << -1 << endl;
    return;
  }
  if(a % b == 0){
    cout << 0 << endl;
    return;
  }
  ll sum = (1 + b) * b / 2;
  ll num = sum % (b - 1);
  cout << modpow(a, num, b) << endl;
}

int main(){
  int t;
  cin >> t;
  rep(i, t) solve();
}
0