結果

問題 No.1140 EXPotentiaLLL!
ユーザー tyawanmusityawanmusi
提出日時 2020-07-31 22:41:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,291 bytes
コンパイル時間 1,928 ms
コンパイル使用メモリ 201,016 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 01:15:30
合計ジャッジ時間 11,022 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,047 ms
4,380 KB
testcase_01 AC 404 ms
4,376 KB
testcase_02 WA -
testcase_03 AC 507 ms
4,380 KB
testcase_04 AC 389 ms
4,376 KB
testcase_05 AC 621 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 1 ms
4,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct Miller{
  const vector<long long> v = { 2 , 7 , 61 }; // < 4,759,123,141
  // x^k (mod m)
  long long modpow( long long x, long long k, long long m ){
    long long res = 1;
    while( k ){
      if( k & 1 ){
        res = res * x % m;
      }
      k /= 2;
      x = x * x % m;
    }
    return res;
  }
  // check if n is prime
  bool check( long long n ){
    if( n < 2 ){
      return false;
    }
    long long d = n - 1;
    long long s = 0;
    while( d % 2 == 0 ){
      d /= 2;
      s++;
    }
    for( long long a : v ){
      if( a == n ){
        return true;
      }
      if( modpow( a , d , n ) != 1 ){
        bool ok = true;
        for( long long r = 0; r < s; r++ ){
          if( modpow( a, d * (1LL << r), n ) == n-1 ){
            ok = false;
            break;
          }
        }
        if( ok ){
          return false;
        }
      }
    }
    return true;
  }
};

Miller miller;

int main(){
    int n;
    cin >> n;
    for (int i = 0; i < n; i++){
        long long a;
        long long p;
        scanf("%d%d",&a,&p);
        int ans;
        if (miller.check((int)p)) {
            ans = 1;
            if (gcd(a, p) == 0) ans = 0;
        }
        else ans = -1;
        printf("%d\n",ans);
    }
}
0