結果

問題 No.443 GCD of Permutation
コンテスト
ユーザー yuruhiya
提出日時 2021-01-24 17:21:22
言語 Crystal
(1.19.1)
コンパイル:
crystal build -Donline_judge -o a.out --release --no-debug _filename_
実行:
./a.out
結果
MLE  
実行時間 -
コード長 1,167 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 10,974 ms
コンパイル使用メモリ 347,924 KB
実行使用メモリ 1,449,420 KB
最終ジャッジ日時 2026-03-21 08:48:39
合計ジャッジ時間 35,486 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 MLE * 1
other AC * 12 TLE * 9 MLE * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

lib C
  fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64
end

class String
  def to_i64
    C.strtoll(self, nil, 10)
  end
end

struct Int
  def prime_factor : Array(Tuple(self, Int32))
    result = [] of Tuple(self, Int32)
    n = self
    typeof(self).new(2).upto(Math.sqrt(self).ceil) do |x|
      count = 0
      while n % x == 0
        n //= x
        count += 1
      end
      result << {x, count} if count > 0
    end
    result << {n, 1} if n != 1
    result
  end

  def divisors : Array(self)
    result = [] of self
    each_divisor do |d|
      result << d
    end
    result
  end

  def each_divisor(&)
    tmp = [] of self
    typeof(self).new(1).upto(self) do |x|
      break if x * x > self
      if self % x == 0
        yield x
        tmp << x
      end
    end
    (0...tmp.size).reverse_each do |i|
      yield self // tmp[i] if tmp[i] * tmp[i] < self
    end
  end
end

require "big"

s = read_line
n = s.size
if s.count(s[0]) == n
  puts s
  exit
end

p (0...n).to_a.combinations(2).map { |(i, j)|
  ("#{s[i]}#{s[j]}".to_i - "#{s[j]}#{s[i]}".to_i).abs
}.reduce(0) { |i, j| i.gcd(j) }.divisors.select { |x|
  s.to_big_i % x == 0
}.max
0