結果

問題 No.774 tatyamと素数大富豪
ユーザー kotatsugamekotatsugame
提出日時 2020-04-22 15:55:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,205 bytes
コンパイル時間 1,098 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 13,952 KB
最終ジャッジ日時 2024-04-19 23:59:23
合計ジャッジ時間 7,453 ms
ジャッジサーバーID
(参考情報)
judge2 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:57:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   57 | 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)
{
	long ret=0;
	while(b)
	{
		if(b&1)
		{
			ret+=a;
			ret%=c;
		}
		a+=a;
		a%=c;
		b>>=1;
	}
	return ret;
}
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)
{
	long s=0,d=N-1;
	while(d%2==0)
	{
		s++;
		d/=2;
	}
	for(long a=2;a<=100&&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;
	vector<string>A(N);
	for(int i=0;i<N;i++)cin>>A[i];
	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;
	}
	long ans=-1;
	do{
		if(to_i(A.back())%2==0)continue;
		string now="";
		for(string a:A)now+=a;
		long X=to_i(now);
		if(isp(X)&&ans<X)ans=X;
	}while(next_permutation(A.begin(),A.end()));
	cout<<ans<<endl;
}
0