結果

問題 No.774 tatyamと素数大富豪
コンテスト
ユーザー ceni1055
提出日時 2018-12-22 00:53:00
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 996 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,282 ms
コンパイル使用メモリ 193,872 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-12 14:17:13
合計ジャッジ時間 3,688 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4 WA * 1
other AC * 6 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <assert.h>

using namespace std;

long long pow(long long x, long long p, long long mod)
{
  long long ret = 1;

  while (p > 0)
  {
    if (p & 1)
      ret = ret * x % mod;
    x = x * x % mod;
    p >>= 1;
  }

  return ret;
}

bool fermatTest(long long p, int k = 100)
{
  if (p < 2)
    return false;

  srand((unsigned)time(NULL));
  for (int i = 0; i < k; i++)
  {
    long long a = rand() % (p - 1) + 1;
    if (pow(a, p - 1, p) != 1) // this condition is better than pow(a, p, p) != a
      return false;
  }

  return true;
}

int main()
{
  int n;
  cin >> n;
  vector<string> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  long long ans = 0;
  do
  {
    string tmp;
    for (int i = 0; i < n; i++)
      tmp += a[i];
    long long check = stoll(tmp);
    if (fermatTest(check))
      ans = max(ans, check);
  } while (next_permutation(a.begin(), a.end()));
  cout << (ans == 0 ? -1 : ans) << endl;

  return 0;
}
0