結果

問題 No.774 tatyamと素数大富豪
ユーザー GrayCoderGrayCoder
提出日時 2018-12-22 18:15:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,694 ms / 2,000 ms
コード長 1,446 bytes
コンパイル時間 1,728 ms
コンパイル使用メモリ 172,900 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-04-08 22:30:12
合計ジャッジ時間 7,544 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,694 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 8 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 30 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 11 ms
6,548 KB
testcase_12 AC 752 ms
6,548 KB
testcase_13 AC 600 ms
6,548 KB
testcase_14 AC 12 ms
6,548 KB
testcase_15 AC 16 ms
6,548 KB
testcase_16 AC 7 ms
6,548 KB
testcase_17 AC 6 ms
6,548 KB
testcase_18 AC 9 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define sorted(a) sort(a.begin(), a.end());
typedef long long signed int ll;

const vector<unsigned int> numset = {2,3,5,7,11,13,17,19,23,29,31,37};
unsigned long long mod_pow(unsigned __int128 x, unsigned long long k, unsigned long long mod) {
  unsigned __int128 res = 1;
  while (k) {
    if (k & 1) res = res * x % mod;
    x = x * x % mod;
    k >>= 1;
  }
  return res;
}

bool isPrime(unsigned long long n) {
  if (n < 2) return false;
  unsigned long long d = n - 1, s = 0;
  while(d % 2 == 0){
    d /= 2;
    s++;
  }
  for(unsigned int a : numset) {
    if (a == n) return true;
    unsigned long long res = mod_pow(a, d, n);
    if (res == 1) continue;
    bool ok = true;
    for (unsigned int r = 0; r < s; r++) {
      if (res == n-1) {
        ok = false;
        break;
      }
      res = (__int128)res * res % n;
    }
    if (ok) return false;
  }
  return true;
}

int main(void) {
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  ll ans = -1;
  ll n, l;
  do {
    n = 0;
    for (int i = 0; i < N; i++) {
      l = pow(10, to_string(A[i]).length());
      n = n * l + A[i];
    }
    if (isPrime(n)) {
      ans = max(ans, n);
    }
  } while (next_permutation(A.begin(), A.end()));
  cout << ans << endl;

  return 0;
}
0