結果
| 問題 |
No.774 tatyamと素数大富豪
|
| コンテスト | |
| ユーザー |
tottoripaper
|
| 提出日時 | 2018-12-25 18:26:20 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,128 bytes |
| コンパイル時間 | 5,137 ms |
| コンパイル使用メモリ | 350,060 KB |
| 実行使用メモリ | 10,496 KB |
| 最終ジャッジ日時 | 2024-10-01 13:30:53 |
| 合計ジャッジ時間 | 11,503 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 1 TLE * 1 -- * 12 |
ソースコード
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using namespace boost::multiprecision;
#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::deque<int> deq;
template <typename T>
T expt(T a, T n, T mod){
T res = 1;
while(n){
if(n & 1){res = (res * a) % mod;}
a = a * a % mod;
n >>= 1;
}
return res;
}
inline int f(int n){
if(n < 10){
return 10;
}
return 100;
}
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
for(ll a : sets){
auto p = expt<int128_t>(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 = p * p % n;
}
if(!ok){
return false;
}
}
return true;
}
ll rec(ll n, ll x, ll k){
if(k == 0){
if(isPrime(n)){
return n;
}
return -1;
}
ll res = -1;
for(int i=0;i<k;++i){
int A = deq.front();
deq.pop_front();
res = std::max(res, rec(n + x * A, x * f(A), k - 1));
deq.emplace_back(A);
}
return res;
}
int main(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cin >> N;
for(int i=0;i<N;++i){
int A;
std::cin >> A;
deq.emplace_back(A);
}
ll res = -1;
for(int i=0;i<N;++i){
int A = deq.front();
deq.pop_front();
unless(A % 2 == 0 || A % 5 == 0){
res = std::max(res, rec(A, f(A), N - 1));
}
deq.emplace_back(A);
}
std::cout << res << std::endl;
}
tottoripaper