結果

問題 No.308 素数は通れません
ユーザー zerokugizerokugi
提出日時 2015-12-01 08:19:12
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,924 bytes
コンパイル時間 719 ms
コンパイル使用メモリ 73,588 KB
実行使用メモリ 4,480 KB
最終ジャッジ日時 2023-10-12 07:27:11
合計ジャッジ時間 4,323 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <set>
#include <vector>
using namespace std;

typedef __int128 Int;

Int mulMod(Int x, Int k, Int m) {
  if (k == 0)     return 0;
  if (k % 2 == 0) return mulMod((x+x) % m, k/2, m);
  else            return (x+mulMod(x, k-1, m)) % m;
}
Int powMod(Int x, Int k, Int m) {
  if (k == 0)     return 1;
  if (k % 2 == 0) return powMod(mulMod(x, x, m) % m, k/2, m);
  else            return mulMod(x, powMod(x, k-1, m), m);
}
bool suspect(Int a, int s, Int d, Int n) {
  Int x = powMod(a, d, n);
  if (x == 1) return true;
  for (int r = 0; r < s; ++r) {
    if (x == n - 1) return true;
    x = mulMod(x, x, n);
  }
  return false;
}
bool isPrime(Int n) {
  if (n <= 1 || (n > 2 && n % 2 == 0)) return false;
  int test[] = {2,3,5,7,11,13,17,19,23,27,29,31,-1};
  Int d = n - 1, s = 0;
  while (d % 2 == 0) ++s, d /= 2;
  for (int i = 0; test[i] < n && test[i] != -1; ++i)
    if (!suspect(test[i], s, d, n)) return false;
  return true;
}

vector<int> eratosthenes_sieve(int n){
	vector<int> res(n+1);
	if(n<2) return res;
	res[2]=1;
	for(int i=3;i<=n;i+=2){
		if(i*i<=n && !res[i]) for(int j=i+i+i;j<=n;j+=i+i) res[j]=1;
		res[i]=!res[i];
	}
	return res;
}
string s;
Int n;

int main(int argc, char *argv[]){
	vector<int> p = eratosthenes_sieve(100000);
	cin >> s;
	for(char c : s) n = n*10 + c - '0';
	for(int i=3;;i++){
		set<int> visited({1}), col({1}), q({1});
		int f = 0;
		while(!q.empty() && col.size() < i){
			int j = *q.begin(); q.erase(q.begin());
			if(j == n){
				f = 1;
				break;
			}
			vector<int> d;
			if(j%i != 0) d.push_back(j+1);
			if(i < j) d.push_back(j-i);
			if(j%i != 1) d.push_back(j-1);
			d.push_back(j+i);
			for(int v : d) if(v <= n && !visited.count(v) && !p[v]){
				visited.insert(v);
				col.insert(v%i);
				q.insert(v);
			}
		}
		
		if(!f && n % i != 1 && col.size() == i && !isPrime(n - i)) f = 1;
		
		if(f){
			cout << i << endl;
			return 0;
		}
	}
}
0