結果
| 問題 |
No.1501 酔歩
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-06-07 14:18:46 |
| 言語 | D (dmd 2.109.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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 53 |
コンパイルメッセージ
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`
ソースコード
// 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);
}
}
}
}