結果
問題 |
No.308 素数は通れません
|
ユーザー |
![]() |
提出日時 | 2015-12-09 20:47:56 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,052 bytes |
コンパイル時間 | 1,008 ms |
コンパイル使用メモリ | 113,800 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-15 05:59:06 |
合計ジャッジ時間 | 4,202 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 106 WA * 1 |
ソースコード
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <complex> #include <queue> #include <deque> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <iomanip> #include <assert.h> #include <array> #include <cstdio> #include <cstring> #include <random> #include <functional> #include <numeric> #include <bitset> using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define minimize(a, x) a = std::min(a, x) #define maximize(a, x) a = std::max(a, x) typedef __int128_t ll; int const inf = 1<<29; namespace math { ll mulmod(ll a, ll b, ll mod) { ll x = 0, y = a % mod; while(b > 0) { if(b & 1) { x = (x + y) % mod; } y = (y * 2) % mod; b /= 2; } return x % mod; } } template<class value_type = long long> value_type mod_pow(value_type x, value_type n, value_type mo) { value_type ret = 1; while(n > 0) { if(n & 1) { ret = ret * x % mo; } x = x * x % mo; n >>= 1; } return ret; } namespace math { namespace prime { bool miller_rabin(ll p,int iteration) { if(p < 2) { return false; } if (p != 2 && p % 2==0) { return false; } ll s = p - 1; while(s % 2 == 0) { s /= 2; } rep(_, iteration) { ll a = std::random_device()() % (p - 1) + 1, temp = s; // これもうわかんねぇな ll mod = mod_pow<ll>(a, temp, p); while (temp != p - 1 && mod != 1 && mod != p - 1) { mod = mulmod(mod, mod, p); temp *= 2; } if (mod != p - 1 && temp % 2 == 0) { return false; } } return true; } } } typedef ll Int; Int mulmod(Int a,Int n,Int Mod){ Int res=0; while(n){ if(n&1)res=(res+a)%Mod; a=(a+a)%Mod; n>>=1; } return res; } Int powmod(Int a,Int n,Int Mod){ Int res=1; while(n){ if(n&1)res=mulmod(res,a,Mod); a=mulmod(a,a,Mod); n>>=1; } return res; } bool miller_rabin(Int n){ Int d=n-1,s=0; while(d%2==0){ d/=2; s+=1; } for(int k=2;k<100;k++){ // if(!prime[k])continue; Int a=k; if(powmod(a,d,n)!=1){ bool b=true; for(Int r=0,_d=d;r<s;r++,_d<<=1){ b&=(powmod(a,_d,n)!=n-1); //print(powmod(a,_d,n)); if(powmod(a,_d,n)<0)cout<<"ERROR"<<endl; } if(b){return false;} } } return true; } map<ll, int> mp = { {4,3}, {6,5}, {8,7}, {9,7}, {10,7}, {12,11}, {14,13}, {15,7}, {16,7}, {18,8}, {20,19}, {21,19}, {22,7}, {24,23}, {25,23}, }; int main() { string s; cin >> s; ll N = 0; rep(i, s.size()) { N = N * 10; N = N + (s[i] - '0'); } if(mp.find(N) != mp.end()) { cout << mp[N] << endl; exit(0); } if((N % 8 == 1) && math::prime::miller_rabin(N-8, 10000)) { cout << 14 << endl; } else { cout << 8 << endl; } return 0; }