結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー Ryuhei MoriRyuhei Mori
提出日時 2017-10-30 19:27:24
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 25 ms / 9,973 ms
コード長 5,180 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 35,072 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-16 23:04:26
合計ジャッジ時間 881 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 17 ms
5,248 KB
testcase_05 AC 19 ms
5,248 KB
testcase_06 AC 14 ms
5,248 KB
testcase_07 AC 14 ms
5,248 KB
testcase_08 AC 14 ms
5,248 KB
testcase_09 AC 25 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <unistd.h>
#include <stdint.h>
#include <math.h>


typedef __int128 int128_t;
typedef unsigned __int128 uint128_t;

char ibuf[300000];
char *ibufe = ibuf-1;
char buf[300000];
char *bufe = buf;

static inline void readall(){
  int k, t = 0;
  while((k=read(STDIN_FILENO, ibuf+t, sizeof(ibuf)-t))>0) t += k;
}

static inline uint64_t read_uintll(){
  uint64_t x=0;
  while(*(++ibufe) <'0');
  do {
    x *= 10;
    x += *ibufe-'0';
  } while(*(++ibufe) >='0');

  return x;
}

static inline uint64_t readwrite_uintll(){
  uint64_t x=0;
  while(*(++ibufe) <'0');
  do {
    x *= 10;
    x += *ibufe-'0';
    *bufe++ = *ibufe;
  } while(*(++ibufe) >='0');
  *bufe++ = ' ';

  return x;
}


static inline void write_bitln(int x){
  *bufe++ = '0'+x;
  *bufe++ = '\n';
}

static inline void writeall(){
  int k, t = 0;
  while((k=write(STDOUT_FILENO, buf+t, bufe-buf-t))>0) t += k;
}


static inline uint64_t ex_gcd(uint64_t y){
  int i;
  uint64_t u, v;
  u = 1; v = 0;
  uint64_t x = 1LL<<63;

  for(i=0;i<64;i++){
    if(u&1){
      u = (u + y) / 2;
      v = v/2 + x;
    }
    else {
      u >>= 1; v >>= 1;
    }
  }

  return v;
} 


static inline uint64_t MR(uint128_t x, uint64_t m, uint64_t n){
  uint64_t z = ((uint128_t) ((uint64_t) x * m) * n + x) >> 64;
  return z < n ? z : z - n;
}

static inline uint64_t RM(uint64_t x, uint64_t r2, uint64_t m, uint64_t n){
  return MR((uint128_t) r2 * x, m, n);
}

static inline uint64_t mulmod64(uint64_t x, uint64_t y, uint64_t m, uint64_t n){
  return MR((uint128_t) x*y, m, n);
}

static inline uint64_t modpow64(uint64_t a, uint64_t k, uint64_t m, uint64_t n){
  uint64_t r;
  for(r=a,--k;k;k/=2){
    if(k&1) r = mulmod64(r, a, m, n);
    a = mulmod64(a, a, m, n);
  }
  return r;
}

int jacobi(int64_t a, uint64_t n){
  uint64_t t;
  int j = 1;
  while(a){
    if(a<0){
      a = -a;
      if((n&3)==3) j = -j;
    }
    int ba = __builtin_ctzll(a);
    a >>= ba;
    if((n%8==3||n%8==5) && (ba&1)) j = -j;
    if((a&n&3)==3) j = -j;
    t = a; a = n; n = t;
    a %= n;
    if(a>n/2) a-=n;
  }
  return n == 1 ? j : 0;
}


static inline uint64_t addmod64(uint64_t x, uint64_t y, uint64_t n){
  return x + y >= n ? x + y - n : x + y;
}

static inline uint64_t submod64(uint64_t x, uint64_t y, uint64_t n){
  return x >= y  ? x - y : x - y + n;
}

void lucasmod(uint64_t k, uint64_t m, uint64_t n, uint64_t q, uint64_t *u, uint64_t *v, uint64_t *pq){
  uint64_t a, b, c, d, ux, uy, vx, vy, r;
  uint64_t one = -1ULL % n + 1;
  uint64_t two = addmod64(one, one, n);
  a = one; b = n-q; c = one; d = 0;
  ux = 0; uy = one;
  vx = two; vy = one;

  r = q;
  *pq = one;

  for(;k;k/=2){
    if(k&1){
      uint64_t uyy = addmod64(mulmod64(a,uy,m,n), mulmod64(b,ux,m,n), n) ;
      ux = addmod64(mulmod64(c,uy,m,n), mulmod64(d,ux,m,n), n);
      uy = uyy;
      uint64_t vyy = addmod64(mulmod64(a,vy,m,n), mulmod64(b,vx,m,n), n);
      vx = addmod64(mulmod64(c,vy,m,n), mulmod64(d,vx,m,n), n);
      vy = vyy;
      *pq = mulmod64(r,*pq,m,n);
    }
    uint64_t aa = addmod64(mulmod64(a,a,m,n), mulmod64(b,c,m,n), n);
    uint64_t bb = addmod64(mulmod64(a,b,m,n), mulmod64(b,d,m,n), n);
    uint64_t cc = addmod64(mulmod64(c,a,m,n), mulmod64(d,c,m,n), n);
    uint64_t dd = addmod64(mulmod64(c,b,m,n), mulmod64(d,d,m,n), n);
    a = aa; b = bb; c = cc; d = dd;
    r = mulmod64(r,r,m,n);
  }

  *u = ux;
  *v = vx;
}

/*
static inline uint64_t modpow64_o(uint64_t a, uint64_t k, uint64_t n){
  uint64_t r;
  for(r=1;k;k/=2){
    if(k&1) r = mulmod64(r,a,n);
    a = mulmod64(a,a,n);
  }
  return r;
}
*/

int is_prime64(const uint64_t n){
//  static const uint32_t ps[] = {3, 7, 11, 13, 17, 19};
  int i, j, r;
  uint64_t d, one, r2, m;
  if(n <= 1) return 0;
  if(n <= 3) return 1;
  if(!(n & 1)) return 0;

/*
  for(i=0;i<sizeof(ps)/sizeof(ps[0]);i++){
    if(n==ps[i]) return 1;
    if(n%ps[i] == 0) return 0;
  }
*/

  one = -1ULL % n + 1;
  r2 = (uint128_t) (int128_t) -1 % n + 1;
  m = ex_gcd(n);
  {
    uint64_t a, t, mone;
    r = __builtin_ctzll(n-1);
    d = (n-1) >> r;
    mone = n - one;
    a = one << 1;
    if(a >= n) a -= n;
    if(a == 0) return 1;
    t = modpow64(a, d, m, n);
    if(t != one){
      for(j=0;t!=mone;j++){
        if(j == r-1) return 0;
        t = MR((uint128_t) t * t, m, n);
  //      if(t == one) return 0;
      }
    }
  }


  {
    int64_t D = 5;
    for(i=0;jacobi(D, n) != -1 && i<64;i++){
      if(i==16){
        uint32_t k = round(sqrtl(n));
        if(k*k == n) return 0;
      }
      if(i&1) D -= 2;
      else D += 2;
      D = -D;
    }
//if(i==64) puts("ERROR");

    uint64_t Q = RM(D < 0 ? (1-D)/4%n : n - (D-1)/4%n, r2, m, n);
    uint64_t d = (n+1) >> __builtin_ctzll(n+1);

    uint64_t u, v, Qn;
    lucasmod(d, m, n, Q, &u, &v, &Qn);
    if(u == 0 || v == 0) return 1;
    for(;d<n+1;d<<=1){
      u = mulmod64(u,v,m,n);
      v = submod64(submod64(mulmod64(v,v,m,n), Qn, n), Qn, n);
      if(v == 0) return 1;
      Qn = mulmod64(Qn, Qn, m,n);
    }
  }


  return 0;
}



int main(){
  int i, n;


  readall();
  n = read_uintll();
  for(i=0;i<n;i++){
    uint64_t x;
    x = readwrite_uintll();
    write_bitln(is_prime64(x));
  }
  writeall();
  return 0;
}
0