結果

問題 No.1501 酔歩
ユーザー te-shte-sh
提出日時 2021-06-07 14:16:55
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 5,197 bytes
コンパイル時間 763 ms
コンパイル使用メモリ 104,704 KB
実行使用メモリ 10,728 KB
最終ジャッジ日時 2023-09-04 12:49:12
合計ジャッジ時間 5,596 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 WA -
testcase_05 AC 71 ms
8,120 KB
testcase_06 AC 52 ms
6,608 KB
testcase_07 AC 68 ms
7,804 KB
testcase_08 AC 15 ms
4,380 KB
testcase_09 AC 61 ms
7,896 KB
testcase_10 AC 22 ms
5,116 KB
testcase_11 AC 54 ms
6,520 KB
testcase_12 AC 34 ms
6,540 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 85 ms
9,384 KB
testcase_23 AC 90 ms
9,436 KB
testcase_24 AC 87 ms
9,440 KB
testcase_25 AC 84 ms
9,332 KB
testcase_26 AC 80 ms
9,488 KB
testcase_27 AC 85 ms
9,420 KB
testcase_28 AC 91 ms
9,404 KB
testcase_29 AC 81 ms
10,036 KB
testcase_30 AC 95 ms
9,524 KB
testcase_31 AC 80 ms
9,384 KB
testcase_32 AC 81 ms
9,980 KB
testcase_33 AC 90 ms
9,472 KB
testcase_34 AC 87 ms
9,380 KB
testcase_35 AC 83 ms
9,380 KB
testcase_36 AC 88 ms
9,368 KB
testcase_37 AC 79 ms
9,392 KB
testcase_38 AC 81 ms
9,428 KB
testcase_39 AC 94 ms
9,424 KB
testcase_40 AC 82 ms
9,400 KB
testcase_41 AC 90 ms
9,856 KB
testcase_42 AC 57 ms
9,824 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 59 ms
9,356 KB
testcase_46 AC 62 ms
9,352 KB
testcase_47 AC 52 ms
9,340 KB
testcase_48 AC 68 ms
9,776 KB
testcase_49 AC 75 ms
9,580 KB
testcase_50 AC 66 ms
9,624 KB
testcase_51 AC 67 ms
9,628 KB
testcase_52 AC 67 ms
10,728 KB
testcase_53 WA -
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(206): 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-176(176):        instantiated from here: `putMain!(c, int)`
Main.d(12):        instantiated from here: `put!("{exit: true}", int)`
Main.d(206): 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-176(176):        instantiated from here: `putMain!(c, string)`
Main.d(27):        instantiated from here: `put!("{}", string)`
/dmd2/linux/bin64/../../src/phobos/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) 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