結果

問題 No.1529 Constant Lcm
ユーザー yuruhiyayuruhiya
提出日時 2021-06-05 20:31:01
言語 Crystal
(1.11.2)
結果
AC  
実行時間 563 ms / 3,000 ms
コード長 4,276 bytes
コンパイル時間 12,665 ms
コンパイル使用メモリ 295,140 KB
実行使用メモリ 15,396 KB
最終ジャッジ日時 2024-05-01 14:30:17
合計ジャッジ時間 19,287 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,812 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 563 ms
13,320 KB
testcase_11 AC 174 ms
6,940 KB
testcase_12 AC 12 ms
6,940 KB
testcase_13 AC 382 ms
9,052 KB
testcase_14 AC 227 ms
9,088 KB
testcase_15 AC 261 ms
7,296 KB
testcase_16 AC 219 ms
7,808 KB
testcase_17 AC 105 ms
6,940 KB
testcase_18 AC 115 ms
6,940 KB
testcase_19 AC 244 ms
8,960 KB
testcase_20 AC 519 ms
14,764 KB
testcase_21 AC 524 ms
14,832 KB
testcase_22 AC 505 ms
14,764 KB
testcase_23 AC 537 ms
15,396 KB
testcase_24 AC 531 ms
14,760 KB
testcase_25 AC 487 ms
14,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# require "math/Sieve"
class Sieve
  getter size : Int32
  getter factor : Array(Int32)
  getter primes : Array(Int32)

  def initialize(@size)
    @factor = Array(Int32).new(@size + 1, 0)
    @primes = [] of Int32
    sqrt_size = Math.sqrt(@size).to_i + 1
    (2..size).each do |x|
      next unless @factor[x] == 0
      @factor[x] = x
      @primes << x
      next if sqrt_size < x
      (x * x).step(to: size, by: x) { |y|
        @factor[y] = x if @factor[y] == 0
      }
    end
  end

  def prime?(x)
    factor[x] == x
  end

  private class PrimeDivisionIterator
    include Iterator({Int32, Int32})

    def initialize(@current : Int32, @factor : Array(Int32))
    end

    def next
      return stop if @current == 1
      element = @factor[@current]
      count = 0
      while @current != 1 && @factor[@current] == element
        count += 1
        @current //= element
      end
      {element, count}
    end
  end

  def prime_division(x)
    PrimeDivisionIterator.new(x, factor)
  end

  def each_factor(x, &) : Nil
    while x > 1
      element = @factor[x]
      count = 0
      while x != 1 && @factor[x] == element
        count += 1
        x //= element
      end
      yield(element, count)
    end
  end
end

# require "math/Mint"
macro static_modint(name, mod)
  struct {{name}}
    MOD = Int64.new({{mod}})

    def self.zero
      new
    end

    def self.raw(value : Int64)
      result = new
      result.value = value
      result
    end

    @value : Int64

    def initialize
      @value = 0i64
    end

    def initialize(value)
      @value = value.to_i64 % MOD
    end

    def initialize(m : self)
      @value = m.value
    end

    protected def value=(value : Int64)
      @value = value
    end

    getter value : Int64

    def + : self
      self
    end

    def - : self
      self.class.raw(value != 0 ? MOD &- value : 0i64)
    end

    def +(v)
      self + v.to_m
    end

    def +(m : self)
      x = value &+ m.value
      x &-= MOD if x >= MOD
      self.class.raw(x)
    end

    def -(v)
      self - v.to_m
    end

    def -(m : self)
      x = value &- m.value
      x &+= MOD if x < 0
      self.class.raw(x)
    end

    def *(v)
      self * v.to_m
    end

    def *(m : self)
      self.class.new(value &* m.value)
    end

    def /(v)
      self / v.to_m
    end

    def /(m : self)
      raise DivisionByZeroError.new if m.value == 0
      a, b, u, v = m.to_i64, MOD, 1i64, 0i64
      while b != 0
        t = a // b
        a &-= t &* b
        a, b = b, a
        u &-= t &* v
        u, v = v, u
      end
      self.class.new(value &* u)
    end

    def //(v)
      self / v
    end

    def **(exponent : Int)
      t, res = self, self.class.raw(1i64)
      while exponent > 0
        res *= t if exponent & 1 == 1
        t *= t
        exponent >>= 1
      end
      res
    end

    def ==(m : self)
      value == m.value
    end

    def ==(m : Int)
      raise NotImplementedError.new("==")
    end

    {% for op in %w[< <= > >=] %}
      def {{op.id}}(other)
        raise NotImplementedError.new({{op}})
      end
    {% end %}

    def succ
      self.class.raw(value != MOD &- 1 ? value &+ 1 : 0i64)
    end

    def pred
      self.class.raw(value != 0 ? value &- 1 : MOD &- 1)
    end

    def abs
      self
    end

    def to_i64 : Int64
      value
    end

    delegate to_s, to: @value
    delegate inspect, to: @value
  end

  struct Int
    {% for op in %w[+ - * / //] %}
      def {{op.id}}(value : {{name}})
        to_m {{op.id}} value
      end
    {% end %}

    {% for op in %w[== != < <= > >=] %}
      def {{op.id}}(m : {{name}})
        raise NotImplementedError.new({{op}})
      end
    {% end %}

    def to_m : {{name}}
      {{name}}.new(self)
    end
  end

  class String
    def to_m : {{name}}
      {{name}}.new(self)
    end
  end
end

static_modint(Mint, 998244353)
n = read_line.to_i
sieve = Sieve.new(n)
max = Hash(Int32, Int32).new(0)
(1...n).each do |x|
  hash = Hash(Int32, Int32).new(0)
  sieve.each_factor(x) { |elem, cnt| hash[elem] += cnt }
  sieve.each_factor(n - x) { |elem, cnt| hash[elem] += cnt }
  hash.each { |elem, cnt| max[elem] = {max[elem], cnt}.max }
end
puts max.reduce(Mint.new(1)) { |acc, (elem, cnt)|
  acc * Mint.new(elem)**cnt
}
0