結果

問題 No.25 有限小数
ユーザー te-sh
提出日時 2017-01-16 17:28:00
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 770 bytes
コンパイル時間 825 ms
コンパイル使用メモリ 116,236 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-12 06:26:25
合計ジャッジ時間 1,808 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!ulong.gcdImpl`

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.bigint;    // BigInt
import std.numeric;   // gcd

void main()
{
  auto n = readln.chomp.to!long;
  auto m = readln.chomp.to!long;

  auto g = gcd(n, m);
  n /= g;
  m /= g;

  writeln(calc(n.to!BigInt, m.to!BigInt));
}

auto calc(T)(T n, T m)
{
  if (n % m == 0)
    return dsr(n / m);

  auto f1 = powers(m, 2);
  auto f2 = powers(m, 5);

  if (m != 2.to!T ^^ f1 * 5.to!T ^^ f2)
    return -1;

  auto k = n.to!BigInt;
  if (f1 < f2) k *= 2.to!T ^^ (f2 - f1);
  if (f1 > f2) k *= 5.to!T ^^ (f1 - f2);
  return dsr(k);
}

auto powers(T)(T n, int d)
{
  auto c = 0;
  for (; n % d == 0; n /= d) ++c;
  return c;
}

auto dsr(T)(T n)
{
  while (n % 10 == 0) n /= 10;
  return (n % 10).to!int;
}
0