結果
| 問題 |
No.774 tatyamと素数大富豪
|
| コンテスト | |
| ユーザー |
tottoripaper
|
| 提出日時 | 2018-12-25 01:45:49 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,188 bytes |
| コンパイル時間 | 1,771 ms |
| コンパイル使用メモリ | 170,064 KB |
| 実行使用メモリ | 13,696 KB |
| 最終ジャッジ日時 | 2024-10-01 06:41:37 |
| 合計ジャッジ時間 | 8,232 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 5 |
| other | TLE * 1 -- * 13 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))
using ll = long long;
using P = std::tuple<int,int>;
const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
int N;
std::vector<int> A;
template <typename T>
T mult(T a, T b, T mod){
T res = 0;
while(b){
if(b & 1){res = (res + a) % mod;}
a = (a + a) % mod;
b >>= 1;
}
return res;
}
template <typename T>
T expt(T a, T n, T mod){
T res = 1;
while(n){
if(n & 1){res = mult(res, a, mod);}
a = mult(a, a, mod);
n >>= 1;
}
return res;
}
bool isPrime(ll n){
if(n % 2 == 0){
return n == 2;
}
ll k = 0, q = n - 1;
while(q % 2 == 0){
k += 1;
q /= 2;
}
constexpr ll sets[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
for(ll a : sets){
ll p = expt(a, q, n);
if(p == 1){
continue;
}
bool ok = false;
for(int i=0;i<k;++i){
if(p == n - 1){
ok = true;
break;
}
p = mult(p, p, n);
}
if(!ok){
return false;
}
}
return true;
}
int main(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cin >> N;
A.resize(N);
for(int i=0;i<N;++i){
std::cin >> A[i];
}
ll res = -1;
while(true){
unless(N > 1 && (A[N-1] % 2 == 0 || A[N-1] % 5 == 0)){ // 枝刈り
ll n = 0;
for(int a : A){
if(a < 10){
n = n * 10 + a;
}else{
n = n * 100 + a;
}
}
unless(n > 100 && (n % 3 == 0 || n % 7 == 0 || n % 11 == 0 || n % 13 == 0)){ // 枝刈り
if(isPrime(n)){
res = std::max(res, n);
}
}
}
if(!next_permutation(A.begin(), A.end())){
break;
}
}
std::cout << res << std::endl;
}
tottoripaper