結果

問題 No.843 Triple Primes
ユーザー te-shte-sh
提出日時 2021-07-06 21:40:36
言語 Crystal
(1.11.2)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 5,320 bytes
コンパイル時間 20,509 ms
コンパイル使用メモリ 256,700 KB
実行使用メモリ 5,464 KB
最終ジャッジ日時 2023-09-14 04:31:05
合計ジャッジ時間 22,433 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,480 KB
testcase_01 AC 7 ms
5,372 KB
testcase_02 AC 3 ms
4,356 KB
testcase_03 AC 3 ms
4,404 KB
testcase_04 AC 3 ms
4,460 KB
testcase_05 AC 3 ms
4,408 KB
testcase_06 AC 3 ms
4,476 KB
testcase_07 AC 6 ms
5,108 KB
testcase_08 AC 7 ms
5,292 KB
testcase_09 AC 7 ms
5,444 KB
testcase_10 AC 6 ms
5,284 KB
testcase_11 AC 6 ms
5,340 KB
testcase_12 AC 7 ms
5,464 KB
testcase_13 AC 7 ms
5,376 KB
testcase_14 AC 7 ms
5,340 KB
testcase_15 AC 6 ms
5,116 KB
testcase_16 AC 6 ms
5,144 KB
testcase_17 AC 3 ms
4,476 KB
testcase_18 AC 3 ms
4,356 KB
testcase_19 AC 2 ms
4,356 KB
testcase_20 AC 4 ms
4,640 KB
testcase_21 AC 4 ms
4,560 KB
testcase_22 AC 6 ms
5,044 KB
testcase_23 AC 5 ms
4,924 KB
testcase_24 AC 4 ms
4,764 KB
testcase_25 AC 4 ms
4,632 KB
testcase_26 AC 7 ms
5,372 KB
testcase_27 AC 3 ms
4,560 KB
testcase_28 AC 7 ms
5,344 KB
testcase_29 AC 4 ms
4,612 KB
testcase_30 AC 7 ms
5,376 KB
testcase_31 AC 3 ms
4,496 KB
testcase_32 AC 3 ms
4,460 KB
testcase_33 AC 4 ms
4,620 KB
testcase_34 AC 5 ms
4,912 KB
testcase_35 AC 8 ms
5,452 KB
testcase_36 AC 4 ms
4,640 KB
testcase_37 AC 7 ms
5,216 KB
testcase_38 AC 5 ms
4,976 KB
testcase_39 AC 7 ms
5,384 KB
testcase_40 AC 3 ms
4,428 KB
testcase_41 AC 3 ms
4,392 KB
testcase_42 AC 6 ms
5,228 KB
testcase_43 AC 7 ms
5,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve(io)
  n = io.get

  io.put_e 0 if n == 1

  p = PrimeFactor.new(n).primes

  b = Array.new(n+1, false)
  p.each do |pi|
    b[pi] = true
  end

  s = 0
  p.each do |pi|
    q = pi+2
    r = q.isqrt
    if r**2 == q && b[r]
      s += 1
    end
  end

  io.put s*2-1
end

require "bit_array"

struct Number
  {% if compare_versions(env("CRYSTAL_VERSION") || "0.0.0", "0.36.0") < 0 %}
    def self.additive_identity
      zero
    end

    def self.multiplicative_identity
      new(1)
    end
  {% end %}
end

def powr(a : T, n : Int, i : T = T.multiplicative_identity) forall T
  powr(a, n, i) { |a, b| a * b }
end

def powr(a : T, n : Int, i : T = T.multiplicative_identity, &block) forall T
  return i if n == 0
  r, b = i, a
  while n > 0
    r = yield r, b if n.bit(0) == 1
    b = yield b, b
    n >>= 1
  end
  r
end

def ext_gcd(a : T, b : T) forall T
  if a == 0
    {b, T.new(0), T.new(1)}
  else
    g, x, y = ext_gcd(b%a, a)
    {g, y-(b//a)*x, x}
  end
end

class PrimeFactor
  def initialize(@n : Int32)
    s = (@n+1)//2
    sieve = BitArray.new(s, true)

    if @n < 2
      @primes = [] of Int32
      return
    end

    (1..(n.isqrt-1)//2).each do |p|
      if sieve[p]
        (p*3+1...s).step(p*2+1) do |q|
          sieve[q] = false
        end
      end
    end

    @primes = [2]
    (1...s).each do |p|
      @primes << p*2+1 if sieve[p]
    end
  end

  def self.sqrt(n : Int)
    self.new(n.isqrt.to_i32)
  end

  getter primes : Array(Int32)

  record Factor(T), prime : T, exp : Int32

  def div(x : T) forall T
    factors = [] of Factor(T)
    t = x.isqrt
    @primes.each do |p|
      break if p > t
      c = 0
      while x%p == 0
        c += 1
        x //= p
      end
      factors << Factor.new(T.new(p), c) if c > 0
      break if x == 1
    end
    factors << Factor.new(x, 1) if x > 1
    factors
  end

  def divisors(x : T) forall T
    factors = div(x)
    r = divisors_proc(factors, 0, T.multiplicative_identity)
    r.sort!
  end


  def divisors_proc(factors : Array(Factor(T)), i : Int32, c : T) forall T
    return [c] if i == factors.size
    r = [] of T
    (0..factors[i].exp).each do |j|
      r.concat(divisors_proc(factors, i+1, c * factors[i].prime**j))
    end
    r
  end
end

class Array
  macro new_md(*args, &block)
    {% if !block %}
      {% for arg, i in args[0...-2] %}
        Array.new({{arg}}) {
      {% end %}
      Array.new({{args[-2]}}, {{args[-1]}})
      {% for arg in args[0...-2] %}
        }
      {% end %}
    {% else %}
      {% for arg, i in args %}
        Array.new({{arg}}) { |_i{{i}}|
      {% end %}
      {% for block_arg, i in block.args %}
        {{block_arg}} = _i{{i}}
      {% end %}
      {{block.body}}
      {% for arg in args %}
        }
      {% end %}
    {% end %}
  end
end

struct Int
  def cdiv(b : Int)
    (self + b - 1) // b
  end

  {% if compare_versions(env("CRYSTAL_VERSION") || "0.0.0", "0.34.0") < 0 %}
    def bit_length : Int32
      x = self < 0 ? ~self : self

      if x.is_a?(Int::Primitive)
        Int32.new(sizeof(self) * 8 - x.leading_zeros_count)
      else
        to_s(2).size
      end
    end
  {% end %}
end

struct Int32
  SQRT_MAX = 46_340_i32

  def isqrt
    m = SQRT_MAX
    r = (1_i32..SQRT_MAX).bsearch { |i| i**2 > self }
    r.nil? ? SQRT_MAX : r - 1
  end
end

struct Int64
  SQRT_MAX = 3_037_000_499_i64

  def isqrt
    r = (1_i64..SQRT_MAX).bsearch { |i| i**2 > self }
    r.nil? ? SQRT_MAX : r - 1
  end
end

class ProconIO
  def initialize
    @buf = [] of String
    @index = 0
  end

  def get(k : T.class = Int32) forall T
    get_v(k)
  end

  def get(*ks : T.class) forall T
    ks.map { |k| get_v(k) }
  end

  macro define_getn
    {% for i in (2..9) %}
      def get{{i}}(k : T.class = Int32) forall T
        get({% for j in (1..i) %}k{% if j < i %}, {% end %}{% end %})
      end
    {% end %}
  end
  define_getn

  def get_a(n : Int, k : T.class = Int32) forall T
    Array.new(n) { get_v(k) }
  end

  def get_c(n : Int, k : T.class = Int32) forall T
    get_a(n, k)
  end

  def get_c(n : Int, *ks : T.class) forall T
    a = Array.new(n) { get(*ks) }
    ks.map_with_index { |_, i| a.map { |e| e[i] } }
  end

  macro define_getn_c
    {% for i in (2..9) %}
      def get{{i}}_c(n : Int, k : T.class = Int32) forall T
        get_c(n, {% for j in (1..i) %}k{% if j < i %}, {% end %}{% end %})
      end
    {% end %}
  end
  define_getn_c

  def get_m(r : Int, c : Int, k : T.class = Int32) forall T
    Array.new(r) { get_a(c, k) }
  end

  def put(*vs)
    vs.each.with_index do |v, i|
      put_v(v)
      print i < vs.size - 1 ? " " : "\n"
    end
  end

  def put_e(*vs)
    put(*vs)
    exit
  end


  private def get_v(k : Int32.class); get_token.to_i32; end
  private def get_v(k : Int64.class); get_token.to_i64; end
  private def get_v(k : String.class); get_token; end

  private def get_token
    if @buf.size == @index
      @buf = read_line.split
      @index = 0
    end
    v = @buf[@index]
    @index += 1
    v
  end

  private def put_v(vs : Enumerable)
    vs.each_with_index do |v, i|
      print v
      print " " if i < vs.size - 1
    end
  end

  private def put_v(v)
    print v
  end
end

macro min_u(a, b)
  {{a}} = Math.min({{a}}, {{b}})
end

macro max_u(a, b)
  {{a}} = Math.max({{a}}, {{b}})
end

solve(ProconIO.new)
0