結果

問題 No.774 tatyamと素数大富豪
ユーザー kotatsugamekotatsugame
提出日時 2020-04-22 16:11:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,544 bytes
コンパイル時間 1,194 ms
コンパイル使用メモリ 82,808 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 00:23:41
合計ジャッジ時間 2,027 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 28 ms
5,376 KB
testcase_13 AC 18 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 9 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:54:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   54 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
long to_i(string s)
{
	long ret=0;
	for(char c:s)ret=ret*10+c-'0';
	return ret;
}
long mul(long a,long b,long c)
{
	const long B=1e9;
	long aa=a/B,ab=a%B;
	long ba=b/B,bb=b%B;
	long up=aa*ba%c;
	for(int i=0;i<18;i++)up=up*10%c;
	long mid=(aa*bb+ab*ba)%c;
	for(int i=0;i<9;i++)mid=mid*10%c;
	long low=ab*bb%c;
	return(up+mid+low)%c;
}
long power(long a,long b,long c)
{
	if(b==0)return 1L;
	else if(b%2==0)return power(mul(a,a,c),b/2,c);
	else return mul(a,power(a,b-1,c),c);
}
bool isp(long N)
{
	if(N%7==0||N%11==0||N%13==0||N%17==0||N%19==0)return false;
	long s=0,d=N-1;
	while(d%2==0)
	{
		s++;
		d/=2;
	}
	for(long a=2;a<=84&&a<N;a++)
	{
		long ad=power(a,d,N);
		bool flag=ad!=1;
		if(flag)
		{
			for(int r=0;r<s;r++)
			{
				flag=flag&&ad!=N-1;
				ad=mul(ad,ad,N);
			}
		}
		if(flag)return false;
	}
	return true;
}
main()
{
	int N;cin>>N;
	int cs=0;
	vector<string>A(N);
	for(int i=0;i<N;i++)
	{
		cin>>A[i];
		for(char c:A[i])cs+=c-'0';
	}
	sort(A.begin(),A.end());
	if(N==1)
	{
		string s=A[0];
		if(s=="2"||s=="3"||s=="5"||s=="7"||s=="11"||s=="13")
		{
			cout<<s<<endl;
		}
		else
		{
			cout<<-1<<endl;
		}
		return 0;
	}
	if(cs%3==0)
	{
		cout<<-1<<endl;
		return 0;
	}
	long ans=-1;
	prev_permutation(A.begin(),A.end());
	do{
		if(to_i(A.back())%2==0||to_i(A.back())%5==0)continue;
		string now="";
		for(string a:A)now+=a;
		long X=to_i(now);
		if(ans<X&&isp(X))
		{
			ans=X;
		}
	}while(prev_permutation(A.begin(),A.end()));
	cout<<ans<<endl;
}
0