結果

問題 No.308 素数は通れません
ユーザー heno239heno239
提出日時 2019-04-02 21:26:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,835 bytes
コンパイル時間 1,239 ms
コンパイル使用メモリ 117,752 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 01:11:55
合計ジャッジ時間 4,482 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,384 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 1 ms
4,380 KB
testcase_53 AC 2 ms
4,376 KB
testcase_54 AC 1 ms
4,380 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 1 ms
4,380 KB
testcase_57 AC 1 ms
4,380 KB
testcase_58 AC 1 ms
4,380 KB
testcase_59 AC 1 ms
4,380 KB
testcase_60 AC 2 ms
4,384 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 1 ms
4,376 KB
testcase_63 AC 2 ms
4,380 KB
testcase_64 AC 1 ms
4,380 KB
testcase_65 AC 1 ms
4,380 KB
testcase_66 AC 1 ms
4,376 KB
testcase_67 AC 1 ms
4,380 KB
testcase_68 AC 1 ms
4,376 KB
testcase_69 AC 2 ms
4,380 KB
testcase_70 AC 2 ms
4,376 KB
testcase_71 AC 1 ms
4,376 KB
testcase_72 AC 1 ms
4,376 KB
testcase_73 AC 2 ms
4,380 KB
testcase_74 AC 2 ms
4,384 KB
testcase_75 AC 1 ms
4,376 KB
testcase_76 WA -
testcase_77 AC 1 ms
4,380 KB
testcase_78 AC 2 ms
4,376 KB
testcase_79 AC 2 ms
4,380 KB
testcase_80 WA -
testcase_81 WA -
testcase_82 AC 2 ms
4,376 KB
testcase_83 AC 1 ms
4,380 KB
testcase_84 WA -
testcase_85 AC 1 ms
4,384 KB
testcase_86 WA -
testcase_87 WA -
testcase_88 AC 2 ms
4,380 KB
testcase_89 AC 1 ms
4,376 KB
testcase_90 WA -
testcase_91 AC 1 ms
4,376 KB
testcase_92 AC 1 ms
4,380 KB
testcase_93 AC 2 ms
4,384 KB
testcase_94 AC 1 ms
4,376 KB
testcase_95 AC 2 ms
4,376 KB
testcase_96 AC 2 ms
4,376 KB
testcase_97 AC 1 ms
4,376 KB
testcase_98 AC 1 ms
4,380 KB
testcase_99 AC 1 ms
4,376 KB
testcase_100 AC 1 ms
4,376 KB
testcase_101 WA -
testcase_102 AC 2 ms
4,376 KB
testcase_103 AC 2 ms
4,380 KB
testcase_104 AC 2 ms
4,380 KB
testcase_105 AC 1 ms
4,376 KB
testcase_106 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ll INF = 1e+14;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second

ul mod_pow(ul x, ul n, ul m) {
	ul ret = 1;
	while (n) {
		if (n % 2)ret = ret * x%m;
		x = x * x%m; n >>= 1;
	}
	return ret;
}
bool intest(ul a, ul d, ul n,int s) {
	if (mod_pow(a, d,n) == 1)return false;
	rep(r, s) {
		ul d_ = ((ul)1 << r)*d;
		if (mod_pow(a, d_, n) == -1)return false;
	}
	return true;
}
//true means prime
bool miller_rabin_primality_test(ul n,int t=100) {
	if (n == 1)return false;
	int s = 0; ul d = n;
	while (d % 2 == 0) {
		d /= 2; s++;
	}
	random_device rnd;
	mt19937_64 mt(rnd());
	
	rep(i, t) {
		ul a = mt(); a %= (n - 1); a++;
		if (intest(a, d, n, s))return false;
	}
	return true;
}

const int mn = 2001;
bool isp[mn + 1];
void init() {
	fill(isp + 2, isp + mn + 1, true);
	Rep1(i, 2, mn) {
		if (!isp[i])continue;
		for (int j = 2 * i; j <= mn; j += i) {
			isp[j] = false;
		}
	}
}
bool testmincase(int n, int m) {
	if (isp[n])return false;
	vector<bool> used(n, false);
	queue<int> q; q.push(0); used[0] = true;
	rep(i, n) {
		if (isp[i + 1])used[i] = true;
	}
	while (!q.empty()) {
		int id = q.front(); q.pop();
		if (id%m) {
			if (!used[id - 1]) {
				used[id - 1] = true; q.push(id - 1);
			}
		}
		if ((id%m) < m - 1 && id < n - 1) {
			if (!used[id + 1]) {
				used[id + 1] = true; q.push(id + 1);
			}
		}
		if (id - m >= 0) {
			if (!used[id - m]) {
				used[id - m] = true; q.push(id - m);
			}
		}
		if (id + m < n) {
			if (!used[id + m]) {
				used[id + m] = true; q.push(id + m);
			}
		}
	}
	return used[n - 1];
}
void solve() {
	init();
	string s; cin >> s;
	if (s.length() > 18) {
		cout << 8 << endl; return;
	}
	else if (s.length() <= 2) {
		int n = stoi(s);
		rep1(i, n) {
			if (testmincase(n, i)) {
				cout << i << endl; return;
			}
		}
	}
	else {
		ul c = stoull(s); c -= 8;
		if (c%8==1&&miller_rabin_primality_test(c))cout << 14 << endl;
		else cout << 8 << endl;
	}
}
int main() {
	solve();
	//stop
	return 0;
}
0