結果

問題 No.774 tatyamと素数大富豪
ユーザー tatyam
提出日時 2018-06-21 23:53:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,249 bytes
コンパイル時間 5,072 ms
コンパイル使用メモリ 197,356 KB
最終ジャッジ日時 2025-01-05 14:52:45
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 12 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define fcout cout << fixed << setprecision(10)
array<int, 14> card;
ll bigMul(ll a, ll b, ll m){
int base = (int)1e9;
ll a_low = a % base, a_high = a / base, b_low = b % base, b_high = b / base, result;
result = (a_high * b_high) % m;
rep(i, 0, 9) result = (result * 10) % m;
result = (result + a_low*b_high + b_low*a_high) % m;
rep(i, 0, 9) result = (result * 10) % m;
result = (result + a_low*b_low) % m;
return result;
}
//n**p % m
ll bigPowMod(ll n, ll p, ll m){
ll ans = 1, ln = n;
if(p <= 0) return 1;
while(p != 0){
if((p & 1) == 1) ans = bigMul(ans, ln, m); //ans = (ans*ln) % m;
//ln = (ln * ln) % m;
ln = bigMul(ln, ln, m);
p = p >> 1;
}
return ans;
}
bool suspect(int a, int s, ll d, ll n) {
ll x = bigPowMod(a, d, n);
if (x == 1) return true;
for (int r = 0; r < s; ++r) {
if (x == n - 1) return true;
//x = x * x % n;
x = bigMul(x, x, n);
}
return false;
}
//MillerRabin primality test
// {2,7,61,-1} is for n < 4759123141 (= 2^32)
// {2,3,5,7,11,13,17,19,23,-1} is for n < 10^16 (at least)
int test[] = {2,3,5,7,11,13,17,19,23,-1};
bool MillerRabin(ll n) {
if (n <= 1 || (n > 2 && n % 2 == 0)) return false;
ll d = n - 1;
int s = 0;
while (d % 2 == 0){
s++;
d /= 2;
}
for (int i = 0; test[i] < n && test[i] != -1; ++i)
if (!suspect(test[i], s, d, n)) return false;
return true;
}
bool is_prime(ll n){
if (n < 2) return false;
if (n < 4) {cout << n; return true;}
if (!(n % 2)) return false;
if (!(n % 3)) return false;
ll sq = sqrt(n);
for(int i = 5 ;i < 64; i += 4 - i % 6 / 2){
if (sq < i) {cout << n; return true;};
if (!(n % i)) return false;
}
if(MillerRabin(n)){cout << n; return true;}
return false;
}
ll putBack(ll a, int b){
return a * (b < 10 ? 10 : 100) + b;
}
bool ans(int n, ll cnt){
if(!n){
if(is_prime(cnt)) return true;
else return false;
}
if(n == 1) rep(i, 1, 14) if(card[i]){
if(ans(n - 1, putBack(cnt, i))) return true;
else return false;
}
for(int i = 9 ; i > 1 ; i--)if(card[i]){
card[i]--;
if(ans(n - 1, putBack(cnt, i))) return true;
card[i]++;
}
if(card[1]){
card[1]--;
for(int i = 9 ; i > 3 ; i--)if(card[i]){
card[i]--;
if(ans(n - 2, putBack(cnt, 10 + i))) return true;
card[i]++;
}
card[1]++;
}
for(int i = 3 ; i > -1 ; i--){
if(card[10 + i]){
card[10 + i]--;
if(ans(n - 1, putBack(cnt, 10 + i))) return true;
card[10 + i]++;
}
else if(card[1]){
card[1]--;
if(card[i]){
card[i]--;
if(ans(n - 2, putBack(cnt, 10 + i))) return true;
card[i]++;
}
card[1]++;
}
}
return false;
}
int main(){
int n, a;
cin >> n;
rep(i, 0, n){
cin >> a;
card[a]++;
}
if(!ans(n, 0))cout << -1;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0