結果

問題 No.774 tatyamと素数大富豪
ユーザー 👑 tatyamtatyam
提出日時 2018-09-17 10:11:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 7,599 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 204,708 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-25 09:17:31
合計ジャッジ時間 4,231 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
using namespace std::chrono;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
#define _overload4(_1,_2,_3,_4,name,...) name
#define _overload3(_1,_2,_3,name,...) name
#define _overload2(_1,_2,name,...) name
#define _re(n) _rep(i,n)
#define _rep(i,n) _repi(i,0,n)
#define _repi(i,a,b) for(ll i=a;i<b;++i)
#define _repc(i,a,b,c) for(ll i=a;i<b;i+=c)
#define rep(...) _overload4(__VA_ARGS__,_repc,_repi,_rep,_re)(__VA_ARGS__)
#define _rre(n) _rrep(i,n)
#define _rrep(i,n) _rrepi(i,0,n)
#define _rrepi(i,a,b) for(ll i=b-1;i>=a;i--)
#define _rrepc(i,a,b,c) for(ll i=a+(b-a-1)/c*c;i>=a;i-=c)
#define rrep(...) _overload4(__VA_ARGS__,_rrepc,_rrepi,_rrep,_rre)(__VA_ARGS__)
#define rbr(i,a) for(auto&& i:a)
#define sum(...) accumulate(range(__VA_ARGS__),0)
#define _range(i) (i).begin(),(i).end()
#define _rangek(i,k) (i).begin(),(i).begin()+k
#define range(...) _overload2(__VA_ARGS__,_rangek,_range)(__VA_ARGS__)
#define LINF 0x3999999999999999LL
#define MOD ll(1000000007)
#define MODD ll(998244353)
#define INF 0x39999999
#define Yes(i) out(i?"Yes":"No")
#define YES(i) out(i?"YES":"NO")
#define elif else if
//#define START auto start=system_clock::now()
//#define END auto end=system_clock::now();cerr<<duration_cast<milliseconds>(end-start).count()<<" ms\n"
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,size) vector<type> name(size);
#define VEC(type,name,size) vector<type> name(size);in(name)
#define vi(name,size,value) vector<ll>name(size,value)
#define vv(type,name,h,w,value) vector<vector<type>>name(h,vector<type>(w,value))
struct aaa{aaa(){cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(15);};}aaaaaaa;
template<typename T>
inline constexpr T gcd (T a,T b) {if(a==b)return a;else return gcd(b,(a-1)%b+1);}
inline void in() {}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec);
template<typename T,size_t size>
istream& operator >> (istream& is, array<T,size>& vec);
template<typename T,typename L>
istream& operator >> (istream& is, pair<T,L>& p);
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec);
template<typename T,typename L>
ostream& operator << (ostream& os, pair<T,L>& p);
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){for(T& x: vec) is >> x;return is;}
template<typename T,typename L>
istream& operator >> (istream& is, pair<T,L>& p){is >> p.first;is >> p.second;return is;}
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec){os << vec[0];rep(i,1,vec.size()){os << ' ' << vec[i];}return os;}
template<typename T,typename L>
ostream& operator << (ostream& os, pair<T,L>& p){os << p.first << " " << p.second;return os;}


ostream &operator<<(ostream &dest, __uint128_t value) {
    ostream::sentry s(dest);
    if (s) {
        char buffer[128];
        char *d = end(buffer);
        do {
            --d;
            *d = "0123456789"[value % 10];
            value /= 10;
        } while (value != 0);
        int len = end(buffer) - d;
        if (dest.rdbuf()->sputn(d, len) != len) {
            dest.setstate(ios_base::badbit);
        }
    }
    return dest;
}


template <class Head, class... Tail>
inline void in(Head&& head,Tail&&... tail){cin>>head;in(move(tail)...);}
template <typename T>
inline void out(T t){cout<<t<<'\n';}
inline void out(){cout<<'\n';}
template <class Head, class... Tail>
inline void out(Head head,Tail... tail){cout<<head<<' ';out(move(tail)...);}

__uint128_t bigMul(__uint128_t a, __uint128_t b, __uint128_t m){
    __uint128_t base = 1e18;
    __uint128_t 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
__uint128_t bigPowMod(__uint128_t n, __uint128_t p, __uint128_t m){
    __uint128_t 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, __uint128_t d, __uint128_t n) {
    __uint128_t 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;
}
int test[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,-1};
bool MillerRabin(__uint128_t n) {
    if (n <= 1 || (n > 2 && n % 2 == 0)) return false;
    __uint128_t 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(__uint128_t n){
    if (n < 2) return false;
    if (n < 4) return true;
    if (!(n % 2)) return false;
    if (!(n % 3)) return false;
    __uint128_t sq = sqrt(n);
    for(int i = 5 ;i < 32; i += 4 - i % 6 / 2){
        if (sq < i) return true;
        if (!(n % i)) return false;
    }
    if(MillerRabin(n))return true;
    return false;
}

__uint128_t perm(int at,vector<int> list,__uint128_t s){
    if(at==0)return is_prime(s)?s:0;
    __uint128_t cnt;
    if(at==1){
        rep(i,0,14)if(list[i]){
            if((cnt=perm(0,vector<int>(),s*(i<10?10:100)+i)))return cnt;
            return 0;
        }
        return 0;
    }
    if(!list[1]&&!list[3]&&!list[7]&&!list[9]&&!list[11]&&!list[13])return 0;
    rrep(i,0,10){
        if(i-1){
            if(list[i]){
                list[i]--;
                if((cnt=perm(at-1,list,s*(i<10?10:100)+i)))return cnt;
                list[i]++;
            }
        }
        else rrep(j,0,10){
            if(j>3){
                if(list[1]&&list[j]){
                    list[1]--;
                    list[j]--;
                    if((cnt=perm(at-2,list,s*100+10+j)))return cnt;
                    list[1]++;
                    list[j]++;
                }
            }
            elif(j-1){
                if(list[10+j]){
                    list[10+j]--;
                    if((cnt=perm(at-1,list,s*100+10+j)))return cnt;
                    list[10+j]++;
                }elif(list[1]&&list[j]){
                    list[1]--;
                    list[j]--;
                    if((cnt=perm(at-2,list,s*100+10+j)))return cnt;
                    list[1]++;
                    list[j]++;
                }
            }else{
                cnt=0;
                __uint128_t cnt2=0;
                if(list[1]){
                    list[1]--;
                    cnt=perm(at-1,list,s*10+1);
                    list[1]++;
                }
                if(list[11]){
                    list[11]--;
                    cnt2=perm(at-1,list,s*100+11);
                    list[11]++;
                }
                if(max(cnt,cnt2))return max(cnt,cnt2);
            }
        }
    }
    return 0;
}
int main(){
    INT(n);
    vec(int,a,14);
    rep(n){
        INT(m);
        a[m]++;
    }
    __uint128_t cnt;
    if((cnt=perm(n,a,0)))out(cnt);
    else out(-1);
}






0