結果

問題 No.577 Prime Powerful Numbers
ユーザー Ryuhei MoriRyuhei Mori
提出日時 2017-10-28 11:56:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 3,932 bytes
コンパイル時間 593 ms
コンパイル使用メモリ 68,688 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-01 20:36:50
合計ジャッジ時間 1,225 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstdint>
#include <cmath>
#include <set>

typedef __int128 int128_t;
typedef unsigned __int128 uint128_t;

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

// k > 0
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 = MR((uint128_t)r*a, m, n);
    a = MR((uint128_t) a*a, m, n);
  }
  return r;
}

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

std::set<uint64_t> set_pp;

const int maxp = 7130;
//const int maxp = 1<<16;
//const int maxp = 2642246;
int sieve_p[maxp/2];

void make_set_pp(){
  uint32_t i;
  for(i=3;i<maxp;i+=2){
    if(!sieve_p[i/2]){
      uint64_t j;
      for(j=3*i;j<maxp;j+=2*i){
        sieve_p[j/2] = 1;
      }
      j=i;
      do { set_pp.insert(j);} while(!__builtin_umulll_overflow(j, i, (long long unsigned *)&j));
    }
  }
}

/*
uint64_t sieve;
void make_sieve(){
  const uint32_t ps[] = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127};
  int i;
  for(i=0;i<sizeof(ps)/sizeof(ps[0]);i++){
    sieve |= (1LL << (ps[i]/2));
  }
}

static inline int is_primesmall(uint32_t n){
  return !!(sieve & (1LL << (n/2)));
}
*/

int is_prime32(uint32_t n){
  static const uint32_t as32[] = {2, 7, 61};
  int i, j, r;
  uint32_t d;
  r = __builtin_ctz(n-1);
  d = (n-1) >> r;
  for(i=0;i<3;i++){
    uint32_t a = as32[i] % n;
    if(a == 0) return 1;
    uint32_t t = modpow32(a, d, n);
    if(t == 1) continue;
    for(j=0;t!=n-1;j++){
      if(j == r-1) return 0;
      t = (uint64_t) t * t % n;
      if(t == 1) return 0;
    }
  }
  return 1;
}


int is_prime64(uint64_t n){
  static const uint64_t as64[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
  int i, j, r;
  uint64_t d, one, mone, r2, m;
  if(n < (1LL << 32)) return is_prime32(n);
  r = __builtin_ctzll(n-1);
  d = (n-1) >> r;
  m = ex_gcd(n);
  one = -1ULL % n + 1;
  mone = n - one;
  r2 = (uint128_t) (int128_t) -1 % n + 1;
  for(i=0;i<7;i++){
    uint64_t a = RM(as64[i], r2, m, n);
    if(a == 0) return 1;
    uint64_t t = modpow64(a, d, m, n);
    if(t == one) continue;
    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;
    }
  }
  return 1;
}

int is_prime(uint64_t n){
  if(n <= 1) return 0;
  if(n <= 3) return 1;
  if(!(n & 1)) return 0;
//  if(n < 128) return is_primesmall(n);
  if(n < (1LL << 32)) return is_prime32(n);
  return is_prime64(n);
}


uint64_t is_oddprimepower64(uint64_t n){
  if(n == 1) return 0;
  if(set_pp.count(n)) return 1;
  uint32_t k = round(sqrt(n));
  if(k*k==n){
    n = k;
    k = round(sqrt(n));
    if(k*k == n) return is_prime(k);
    else return is_prime(n);
  }
  k = round(cbrt(n));
  if(k*k*k==n) return is_prime(k);

  return is_prime(n);
}



int main(){
  int i, q;

  make_set_pp();

/*
  for(uint64_t x: set_pp){
    printf("%lu\n", x);
  }
  printf("%u\n", set_pp.size());

return 0;
*/

  scanf("%d", &q);
  for(i=0;i<q;i++){
    uint64_t n;
    scanf("%ld", &n);
    if(n<=2) puts("No");
    else if((n&1)==0) puts("Yes");
    else {
      uint64_t j;
      for(j=2; j<n; j<<=1){
        if(is_oddprimepower64(n-j)){
          break;
        }
      }
      if(j>=n) puts("No"); else puts("Yes");
    }
  }
  return 0;
}
0