結果
問題 | No.774 tatyamと素数大富豪 |
ユーザー | tatyam |
提出日時 | 2018-12-22 00:23:55 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 7 ms / 2,000 ms |
コード長 | 4,736 bytes |
コンパイル時間 | 2,704 ms |
コンパイル使用メモリ | 233,372 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-01 13:21:06 |
合計ジャッジ時間 | 3,388 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 6 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 4 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 4 ms
5,248 KB |
testcase_06 | AC | 1 ms
5,248 KB |
testcase_07 | AC | 1 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 1 ms
5,248 KB |
testcase_10 | AC | 7 ms
5,248 KB |
testcase_11 | AC | 6 ms
5,248 KB |
testcase_12 | AC | 5 ms
5,248 KB |
testcase_13 | AC | 4 ms
5,248 KB |
testcase_14 | AC | 2 ms
5,248 KB |
testcase_15 | AC | 7 ms
5,248 KB |
testcase_16 | AC | 1 ms
5,248 KB |
testcase_17 | AC | 4 ms
5,248 KB |
testcase_18 | AC | 7 ms
5,248 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(a) for(int i=0;i<a;i++) 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; } __uint128_t bigMul(__uint128_t a,__uint128_t b,__uint128_t m){ __uint128_t a_low=a&0xffffffffffffffff,a_high=a>>64,b_low=b&0xffffffffffffffff,b_high=b>>64,result; result=a_high*b_high%m; rep(64)result=result*2%m; result=(result+a_low*b_high+b_low*a_high)%m; rep(64)result=result*2%m; result=(result+a_low*b_low)%m; return result; } __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); 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 = bigMul(x, x, n); } return false; } const vector<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}; bool MillerRabin(__uint128_t n){ if(n<=100){ for(auto&i:test)if(i==n)return true; return false; } if(n%2==0)return false; __uint128_t d = n - 1; int s = 0; while (d%2==0){ s++; d /= 2; } for(auto&i:test)if(!suspect(i, s, d, n))return false; return true; } priority_queue<tuple<string,__uint128_t,vector<int>,bool>,vector<tuple<string,__uint128_t,vector<int>,bool>>,greater<tuple<string,__uint128_t,vector<int>,bool>>>q; set<pair<string,vector<int>>>s; bool check(string num,__uint128_t num_i,vector<int>& cards){ if(s.count({num,cards}))return false; s.insert({num,cards}); if(accumulate(cards.begin(),cards.begin()+10,0)==0){ if(accumulate(cards.begin()+10,cards.end(),0)==0)return true; if((int(num_i%11)-cards[10]+cards[12]+cards[13]*2+11)%11==0)return false; } bool flag=false; for(int i=1;i<14;i+=2)if(i!=5&&cards[i]){ flag=true; break; } return flag; } string to_s(int a){ string s=to_string(a); for(char&i:s)i='0'+('9'-i); return s; } signed main(){ int n; cin>>n; if(n==1){ int a; cin>>a; if(MillerRabin(a)){ cout<<a<<endl; return 0; } return puts("-1")&0; } vector<int>cards(14); vector<int>a(n); rep(n)cin>>a[i]; if(accumulate(a.begin(),a.end(),0)%3==0)return puts("-1")&0; rep(n)cards[a[i]]++; q.push({"",0,cards,false}); while(!q.empty()){ string num=get<0>(q.top()); __uint128_t num_i=get<1>(q.top()); vector<int>cards=get<2>(q.top()); bool one=get<3>(q.top()),flag=true; q.pop(); for(int i=2;i<14;i++){ if(i==11){ if(cards[1]){ flag=false; if(one)continue; string num2=num; __uint128_t num_i2=num_i; int one=cards[1],eleven=cards[11]; while(cards[1]||cards[11]){ num_i2=num_i2*10+1; num2+=to_s(1); if(one!=cards[1]&&cards[11]){ cards[1]++; cards[11]--; } else cards[1]--; if(check(num2,num_i2,cards))q.push({num2,num_i2,cards,true}); } cards[1]=one; cards[11]=eleven; } else if(cards[11]){ flag=false; if(one)continue; cards[i]--; if(check(num+to_s(i),num_i*(i<10?10:100)+i,cards))q.push({num+to_s(i),num_i*(i<10?10:100)+i,cards,false}); cards[i]++; } } if(cards[i]){ flag=false; cards[i]--; if(check(num+to_s(i),num_i*(i<10?10:100)+i,cards))q.push({num+to_s(i),num_i*(i<10?10:100)+i,cards,false}); cards[i]++; } } if(flag&&MillerRabin(num_i)){ cout<<num_i<<endl; return 0; } } cout<<-1<<endl; }