結果

問題 No.847 Divisors of Power
ユーザー te-shte-sh
提出日時 2021-07-07 20:14:19
言語 Crystal
(1.10.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 6,487 bytes
コンパイル時間 20,134 ms
コンパイル使用メモリ 263,496 KB
実行使用メモリ 4,664 KB
最終ジャッジ日時 2023-09-14 04:40:59
合計ジャッジ時間 19,461 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,444 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,444 KB
testcase_04 AC 2 ms
4,464 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,456 KB
testcase_07 AC 2 ms
4,576 KB
testcase_08 AC 3 ms
4,472 KB
testcase_09 AC 2 ms
4,644 KB
testcase_10 AC 2 ms
4,440 KB
testcase_11 AC 2 ms
4,480 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,448 KB
testcase_14 AC 3 ms
4,484 KB
testcase_15 AC 7 ms
4,412 KB
testcase_16 AC 3 ms
4,476 KB
testcase_17 AC 2 ms
4,452 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 3 ms
4,468 KB
testcase_20 AC 3 ms
4,416 KB
testcase_21 AC 4 ms
4,468 KB
testcase_22 AC 3 ms
4,504 KB
testcase_23 AC 3 ms
4,556 KB
testcase_24 AC 6 ms
4,496 KB
testcase_25 AC 3 ms
4,664 KB
testcase_26 AC 2 ms
4,440 KB
testcase_27 AC 2 ms
4,552 KB
testcase_28 AC 2 ms
4,492 KB
testcase_29 AC 3 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve(io)
  n, k, m = io.get3

  pf = PrimeFactor.sqrt(n)
  d = pf.div(n)

  num_divisors = uninitialized (Int32, Int64) -> Int32
  num_divisors = ->(i : Int32, j : Int64) do
    return 1 if i == d.size

    c = 0
    (0..d[i].exp.to_i64 * k).each do |e|
      nj = j * d[i].prime.to_i64 ** e
      break if nj > m
      c += num_divisors.call(i+1, nj)
    end

    c
  end

  io.put num_divisors.call(0, 1_i64)
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

  def bit?(i : Int)
    bit(i) == 1
  end

  def set_bit(i : Int)
    self | (self.class.new(1) << i)
  end

  def reset_bit(i : Int)
    self & ~(self.class.new(1) << i)
  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

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

class ProconIO
  def initialize(@ins : IO = STDIN, @outs : IO = STDOUT)
    @buf = [] of String
    @index = 0
  end

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

  macro define_get
    {% for i in (2..9) %}
      def get(
           {% for j in (1..i) %}
             k{{j}}{% if j < i %},{% end %}
           {% end %}
         )
        {
          {% for j in (1..i) %}
            get(k{{j}}){% if j < i %},{% end %}
          {% end %}
        }
      end
    {% end %}
  end
  define_get

  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

  macro define_get_c
    {% for i in (2..9) %}
      def get_c(
           n : Int,
           {% for j in (1..i) %}
             k{{j}}{% if j < i %},{% end %}
           {% end %}
         )
        a = Array.new(n) do
          get(
            {% for j in (1..i) %}
              k{{j}}{% if j < i %},{% end %}
            {% end %}
          )
        end
        {
          {% for j in (1..i) %}
            a.map { |e| e[{{j-1}}] }{% if j < i %},{% end %}
          {% end %}
        }
      end
    {% end %}
  end
  define_get_c

  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)
      @outs.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
      str = @ins.read_line
      @buf = str.split
      @index = 0
    end
    v = @buf[@index]
    @index += 1
    v
  end

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

  private def put_v(v)
    @outs.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

require "bit_array"

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

solve(ProconIO.new)
0