結果

問題 No.308 素数は通れません
ユーザー moti
提出日時 2015-12-09 19:58:45
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 2,472 bytes
コンパイル時間 928 ms
コンパイル使用メモリ 113,284 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-09-15 05:57:30
合計ジャッジ時間 3,602 ms
ジャッジサーバーID
(参考情報)
judge4 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16 TLE * 1 -- * 90
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}

}

namespace math {

ll modular_exp(ll base, ll exp, ll mod) {
  ll x = 1;
  ll y = base;
  while(exp > 0) {
    if(exp & 1) {
      x *= y; x %= mod;
    }
    y *= y; y %= mod;
    exp >>= 1;
  }
  return x % mod;
}

}

struct xor128 {
  unsigned x,y,z,w;
  xor128(): x(89165727), y(157892372), z(7777777), w(757328) {}
  unsigned next() {
    unsigned t=x^(x<<11);
    x=y;y=z;z=w;
    return w=w^(w>>19)^t^(t>>8);
  }
  unsigned next(unsigned k) {
    return next()%k;
  }
} rndgen;

namespace math {
namespace prime {

bool miller_rabin(ll p, int iteration) {
  if(p < 2) { return false; }
  if(p != 2 && p & 1) { return false; }
  ll s = p - 1;
  while(s & 1) { s >>= 1; }
  rep(_, iteration) {
    ll a = rndgen.next(p - 1) + 1;
    ll temp = s;
    ll mod = modular_exp(a, temp, p);
    while(temp != p - 1 && mod != 1 && mod != p - 1) {
      mod = mulmod(mod, mod, p);
      temp <<= 1;
    }
    if(mod != p - 1 && temp & 1) {
      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-1, 2)) && math::prime::miller_rabin(N-8, 2)) {
    cout << 14 << endl;
  }
  else {
    cout << 8 << endl;
  }

  return 0;
}
0