結果

問題 No.774 tatyamと素数大富豪
ユーザー goodbatongoodbaton
提出日時 2018-12-27 21:23:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,826 bytes
コンパイル時間 994 ms
コンパイル使用メモリ 103,664 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 03:06:34
合計ジャッジ時間 1,914 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifdef LOCAL
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
#else
#define debug(x) ;
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

/* ミラー-ラビン素数判定法 */

namespace MillerRabin {
  typedef __int128_t Int;
  Int power(Int k, ll n, ll M) {
    if(n == 0) return 1;
    if(n == 1) return k % M;

    Int res = power(k, n/2, M);
    res = res * res % M;
    return n%2 ? res % M * k % M : res;
  }

  bool isPrime(ll x) {
    int a[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, -1}; // x < 2^64
    //int a[] = {2, 7, 61, -1}; // x < 2^32
    if(x == 2) return true;
    if(x <= 1 || x % 2 == 0) return false;
    ll d = x - 1;
    int s = 0;
    for(; d%2 == 0; s++) d >>= 1;
    for(int i=0; a[i] != -1 && a[i] < x; i++){
      Int y = power(a[i], d, x);
      if(y == 1) continue;
      for(int j=0; j<s-1 && y != x-1; j++) y = (y * y) % x;
      if(y != x-1) return false;
    }
    return true;
  }
};


int main(){
  int s[14];
  int n;

  scanf("%d", &n);

  for(int i=0;i<n;i++) cin >> s[i];

  sort(s, s+n);

  ll ans = -1;

  do{
    ll t = 0;

    for(int i=0;i<n;i++){
      if(s[i] < 10) t *= 10;
      else t *= 100;
      t += s[i];
    }

    if(ans < t && MillerRabin::isPrime(t)) ans = max(ans, t);
  }while(next_permutation(s, s+n));

  cout << ans << endl;

  return 0;
}
0