結果

問題 No.1501 酔歩
ユーザー te-shte-sh
提出日時 2021-06-07 14:18:46
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 5,258 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 113,108 KB
実行使用メモリ 9,688 KB
最終ジャッジ日時 2024-06-22 11:25:31
合計ジャッジ時間 5,132 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 70 ms
7,500 KB
testcase_06 AC 52 ms
6,944 KB
testcase_07 AC 68 ms
8,572 KB
testcase_08 AC 15 ms
6,940 KB
testcase_09 AC 60 ms
7,852 KB
testcase_10 AC 22 ms
6,944 KB
testcase_11 AC 54 ms
6,940 KB
testcase_12 AC 33 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 84 ms
8,736 KB
testcase_23 AC 89 ms
8,904 KB
testcase_24 AC 86 ms
9,688 KB
testcase_25 AC 84 ms
8,904 KB
testcase_26 AC 78 ms
8,916 KB
testcase_27 AC 85 ms
8,820 KB
testcase_28 AC 91 ms
8,764 KB
testcase_29 AC 80 ms
8,824 KB
testcase_30 AC 94 ms
8,880 KB
testcase_31 AC 79 ms
8,996 KB
testcase_32 AC 81 ms
8,812 KB
testcase_33 AC 89 ms
8,824 KB
testcase_34 AC 86 ms
9,120 KB
testcase_35 AC 83 ms
8,864 KB
testcase_36 AC 87 ms
8,948 KB
testcase_37 AC 78 ms
8,900 KB
testcase_38 AC 80 ms
8,900 KB
testcase_39 AC 93 ms
8,868 KB
testcase_40 AC 81 ms
8,924 KB
testcase_41 AC 88 ms
9,316 KB
testcase_42 AC 57 ms
9,268 KB
testcase_43 AC 1 ms
6,940 KB
testcase_44 AC 1 ms
6,940 KB
testcase_45 AC 59 ms
8,728 KB
testcase_46 AC 61 ms
8,960 KB
testcase_47 AC 53 ms
8,676 KB
testcase_48 AC 66 ms
8,964 KB
testcase_49 AC 75 ms
8,972 KB
testcase_50 AC 66 ms
9,060 KB
testcase_51 AC 66 ms
8,964 KB
testcase_52 AC 65 ms
9,148 KB
testcase_53 AC 1 ms
6,940 KB
testcase_54 AC 1 ms
6,940 KB
testcase_55 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(209): Deprecation: function `Main.IO!(makeGlobal, makeGlobal).IO.put!("{exit: true}", string).put.putMain!(c, string).putMain` function requires a dual-context, which is deprecated
Main.d-mixin-179(179):        instantiated from here: `putMain!(c, string)`
Main.d(13):        instantiated from here: `put!("{exit: true}", string)`
Main.d(209): Deprecation: function `Main.IO!(makeGlobal, makeGlobal).IO.put!("{exit: true}", int).put.putMain!(c, int).putMain` function requires a dual-context, which is deprecated
Main.d-mixin-179(179):        instantiated from here: `putMain!(c, int)`
Main.d(14):        instantiated from here: `put!("{exit: true}", int)`
Main.d(209): Deprecation: function `Main.IO!(makeGlobal, makeGlobal).IO.put!("{}", string).put.putMain!(c, string).putMain` function requires a dual-context, which is deprecated
Main.d-mixin-179(179):        instantiated from here: `putMain!(c, string)`
Main.d(30):        instantiated from here: `put!("{}", string)`
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!ulong.gcdImpl`

ソースコード

diff #

// URL: https://yukicoder.me/problems/no/1501

import std.algorithm, std.array, std.bitmanip, std.container, std.conv, std.format,
       std.functional, std.math, std.range, std.traits, std.typecons, std.stdio, std.string;

version(unittest) {} else
void main()
{
  int N, K; io.getV(N, K);
  long[] A; io.getA(N, A);

  if (K == 1) {
    if (N == 1) io.put!"{exit: true}"("1/1");
    else io.put!"{exit: true}"(0);
  }

  auto a = new Frac!long[](N), b = new Frac!long[](N);
  foreach (i; 1..N-1) {
    a[i] = frac(A[i-1], A[i-1]+A[i+1]);
    b[i] = frac(A[i+1], A[i-1]+A[i+1]);
  }

  auto c = new Frac!long[](N); c[0] = frac(0L, 1L);
  foreach (i; 1..N-1)
    c[i] = b[i] / (frac(1L, 1L) - a[i] * c[i-1]);

  auto r = frac(1L, 1L);
  foreach (i; K-1..N-1) r *= c[i];

  io.put(r.a.to!string ~ "/" ~ r.b.to!string);
}

struct Frac(T)
{
  import std.numeric : gcd;

  T a, b;

  pure nothrow @nogc @safe
  {
    this(T a, T b)
      in { assert(b != 0); }
    do
    {
      this.a = a; this.b = b;
    }

    bool opEquals(Frac!T r) const
    {
      return (a == 0 && r.a == 0) || (a == r.a && b == r.b);
    }
    int opCmp(Frac!T r) const
    {
      return a*r.b<r.a*b ? -1 : a*r.b>r.a*b ? 1 : 0;
    }

    Frac!T inv()
      in { assert(a != 0); }
    do
    {
      return Frac!T(b, a).normalizeSign;
    }
    Frac!T opUnary(string op: "-")()
    {
      return Frac!T(-a, b);
    }

    Frac!T opBinary(string op)(Frac!T r)
      if (op == "+" || op == "-")
    {
      auto g = gcd(b, r.b);
      return Frac!T(mixin("r.b/g*a"~op~"b/g*r.a"), b/g*r.b).reduction();
    }
    Frac!T opOpAssign(string op)(Frac!T r)
      if (op == "+" || op == "-")
    {
      auto g = gcd(b, r.b);
      a = mixin("r.b/g*a"~op~"b/g*r.a"); b = b/g*r.b;
      return reduction();
    }

    Frac!T opBinary(string op: "*")(Frac!T r)
    {
      auto g1 = gcd(a.abs, r.b), g2 = gcd(r.a.abs, b);
      return Frac!T((a/g1)*(r.a/g2), (b/g2)*(r.b/g1));
    }
    ref Frac!T opOpAssign(string op: "*")(Frac!T r)
    {
      auto g1 = gcd(a.abs, r.b), g2 = gcd(r.a.abs, b);
      a = (a/g1)*(r.a/g2); b = (b/g2)*(r.b/g1); return this;
    }

    Frac!T opBinary(string op: "/")(Frac!T r)
      in { assert(r.b != 0); }
    do
    {
      auto g1 = gcd(a.abs, r.a.abs), g2 = gcd(b, r.b);
      return Frac!T((a/g1)*(r.b/g2), (b/g2)*(r.a/g1)).normalizeSign();
    }
    ref Frac!T opOpAssign(string op: "/")(Frac!T r)
      in { assert(r.b != 0); }
    do
    {
      auto g1 = gcd(a.abs, r.a.abs), g2 = gcd(b, r.b);
      a = (a/g1)*(r.b/g2); b = (b/g2)*(r.a/g1);
      return normalizeSign();
    }

    private
    {
      ref Frac!T reduction()
      {
        auto g = gcd(a.abs, b);
        a /= g; b /= g;
        return this;
      }
      ref Frac!T normalizeSign()
      {
        if (b < 0) { a = -a; b = -b; }
        return this;
      }
    }
  }
}
pure nothrow @nogc @safe
{
  Frac!T frac(T)(T a, T b)
  {
    return Frac!T(a, b).normalizeSign().reduction();
  }
}

auto io = IO!()();
import std.stdio;
struct IO(alias IN = stdin, alias OUT = stdout)
{
  import std.meta : allSatisfy;
  import core.stdc.stdlib : exit;

  void getV(T...)(ref T v)
  {
    foreach (ref w; v) get(w);
  }
  void getA(T)(size_t n, ref T v)
    if (hasAssignableElements!T)
  {
    v = new T(n);
    foreach (ref w; v) get(w);
  }
  void getC(T...)(size_t n, ref T v)
    if (allSatisfy!(hasAssignableElements, T))
  {
    foreach (ref w; v) w = new typeof(w)(n);
    foreach (i; 0..n) foreach (ref w; v) get(w[i]);
  }
  void getM(T)(size_t r, size_t c, ref T v)
    if (hasAssignableElements!T && hasAssignableElements!(ElementType!T))
  {
    v = new T(r);
    foreach (ref w; v) getA(c, w);
  }
  template getS(E...)
  {
    void getS(T)(size_t n, ref T v)
    {
      v = new T(n);
      foreach (ref w; v) foreach (e; E) mixin("get(w."~e~");");
    }
  }

  const struct PutConf
  {
    bool newline = true, flush, exit;
    string floatFormat = "%.10f", delimiter = " ";
  }

  void put(alias conf = "{}", T...)(T v)
  {
    mixin("const PutConf c = "~conf~"; putMain!c(v);");
  }
  void putB(alias conf = "{}", S, T)(bool c, S t, T f)
  {
    if (c) put!conf(t);
    else put!conf(f);
  }
  void putRaw(T...)(T v)
  {
    OUT.write(v);
    OUT.writeln;
  }

  private
  {
    dchar[] buf;
    auto sp = (new dchar[](0)).splitter;

    void nextLine()
    {
      IN.readln(buf);
      sp = buf.splitter;
    }
    void get(T)(ref T v)
    {
      if (sp.empty) nextLine();
      v = sp.front.to!T;
      sp.popFront();
    }

    void putMain(PutConf c, T...)(T v)
    {
      foreach (i, w; v) {
        putOne!c(w);
        if (i+1 < v.length) OUT.write(c.delimiter);
      }
      static if (c.newline) OUT.writeln;
      static if (c.flush) OUT.flush();
      static if (c.exit) exit(0);
    }
    void putOne(PutConf c, T)(T v)
    {
      static if (isInputRange!T && !isSomeString!T) putRange!c(v);
      else static if (isFloatingPoint!T) OUT.write(format(c.floatFormat, v));
      else static if (hasMember!(T, "fprint")) v.fprint(OUT);
      else OUT.write(v);
    }
    void putRange(PutConf c, T)(T v)
    {
      auto w = v;
      while (!w.empty) {
        putOne!c(w.front); w.popFront();
        if (!w.empty) OUT.write(c.delimiter);
      }
    }
  }
}
0