結果

問題 No.774 tatyamと素数大富豪
ユーザー ceni1055ceni1055
提出日時 2018-12-22 00:53:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 996 bytes
コンパイル時間 2,358 ms
コンパイル使用メモリ 177,788 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-26 01:42:58
合計ジャッジ時間 5,866 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 23 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 WA -
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 25 ms
4,348 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 14 ms
4,348 KB
testcase_15 WA -
testcase_16 AC 6 ms
4,348 KB
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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