結果

問題 No.1140 EXPotentiaLLL!
ユーザー kurotemkokurotemko
提出日時 2020-08-03 20:05:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,515 bytes
コンパイル時間 3,976 ms
コンパイル使用メモリ 202,824 KB
実行使用メモリ 23,956 KB
最終ジャッジ日時 2023-10-11 16:08:04
合計ジャッジ時間 11,622 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 579 ms
23,696 KB
testcase_01 AC 265 ms
23,644 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 110 ms
23,928 KB
testcase_09 WA -
testcase_10 AC 110 ms
23,692 KB
testcase_11 WA -
testcase_12 AC 109 ms
23,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long mod = 1000000007;
vector<bool> vp;
vector<int> vk(5000001);

// ------------------------------
// 素数列挙
// 参考:https://ei1333.github.io/luzhiled/snippets/math/prime-table.html
void prime_table(int n) {
  vector<bool> prime(n+1, true);
  if(n >= 0) prime[0] = false;
  if(n >= 1) prime[1] = false;
  for(int i = 2; i*i <= n; ++i) {
    if(!prime[i]) continue;
    for(int j = i+i; j <= n; j += i) {
      prime[j] = false;
    }
  }
  vp = prime;
}
// ------------------------------

void kaijou() {
    vk[0] = 1;
    for(int i = 1; i <= 5000000; ++i) vk[i] = (vk[i-1]*i)%mod;
}

// ------------------------------
// 繰り返し二乗法
// 参考:https://math.nakaken88.com/textbook/cp-binary-exponentiation-and-recursive-function/
int modPow(int a, int n, int p) {
  if (n == 0) return 1;
  if (n == 1) return a % p;
  if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}
// ------------------------------

int main() {

    // あらかじめ素数テーブルと階乗テーブルを作成
    // ちゃんとmodを取っておく
    prime_table(5000000);
    kaijou();

    long long t;
    cin >> t;

    for(int i = 0; i < t; ++i) {
        long long a;
        int p;
        scanf("%lld %d", &a, &p);
        if(vp[p]) {
            int ans = modPow((a%p), vk[p], p);
            printf("%d\n", ans);
        }
        else {
            printf("-1\n");
        }
    }
}
0