結果
問題 | No.774 tatyamと素数大富豪 |
ユーザー |
![]() |
提出日時 | 2018-12-25 16:24:44 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 944 ms / 2,000 ms |
コード長 | 1,187 bytes |
コンパイル時間 | 803 ms |
コンパイル使用メモリ | 59,316 KB |
実行使用メモリ | 17,408 KB |
最終ジャッジ日時 | 2024-10-01 13:25:44 |
合計ジャッジ時間 | 4,400 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 14 |
ソースコード
#include <stdio.h> #include <algorithm> #include <vector> #include <set> using namespace std; using ll = long long; ll p[]={2,3,5,7,11,13,17,19,23};ll sqa(ll a,ll b,ll c){ll ret = 1;while(b){if(b&1)ret=(__int128)ret*a%c;a=(__int128)a*a%c;b>>=1;}return ret;}bool ML(ll x){if(x==2)return true;if(x<2||!(x&1))return false;ll y = x-1;int s = 0;while((y&1)==0)y>>=1,s++;for(ll c :p){if(c>=x)return true;ll d = sqa(c,y,x);if(d==1)continue;for(int j=0;j<s;j++){ll t = (__int128)d*d%x;if(t==1 && d!=1 && d!=x-1)return false;d = t;}if(d!=1)return false;}return true;}ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll div(ll x){ll a=rand()%(x-1)+1,b=a,i=1,k=2;ll c=1;do{i++;ll d = gcd(b-a+x,x);if(d!=1 && d!=x)return d;if(i==k)b=a,k<<=1;a=((__int128)a*a+x-c)%x;}while(a!=b);return x;} int main() { int N; scanf ("%d",&N); int A[10]; for (int i=0;i<N;i++) scanf ("%d",&A[i]); long long ans = -1; set<long long> chk; do{ long long a = 0; for (int i=0;i<N;i++){ if (A[i] < 10) a = a * 10 + A[i]; else a = a * 100 + A[i]; } if (chk.count(a)) continue; else{ chk.insert(a); if (ML(a)) ans = max(ans,a); } }while(next_permutation(A,A+N)); printf ("%lld\n",ans); return 0; }