結果

問題 No.1140 EXPotentiaLLL!
ユーザー tyawanmusityawanmusi
提出日時 2020-07-31 22:14:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,259 bytes
コンパイル時間 1,488 ms
コンパイル使用メモリ 167,880 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 23:46:44
合計ジャッジ時間 15,669 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,680 ms
4,376 KB
testcase_01 AC 1,043 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 1,012 ms
4,380 KB
testcase_04 AC 791 ms
4,376 KB
testcase_05 AC 1,261 ms
4,380 KB
testcase_06 AC 1,198 ms
4,376 KB
testcase_07 TLE -
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
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;
        int p;
        scanf("%d%d",&a,&p);
        if (miller.check(p)){
          if (a%p == 0) cout << 0 << endl;
          else cout << 1 << endl;
        }
        else cout << -1 << endl;
    }
}
0