Submission #1415541


Source Code Expand

import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;

void main() {
    int N;
    readf("%d\n", &N);
    long[] xs = readln.split.to!(long[]);
    int M; long K;
    readf("%d %d\n", &M, &K);
    int[] as = readln.split.to!(int[]).map!"a-2".array;

    long[] ys = zip(xs[1..$], xs[0..$-1]).map!"a[0]-a[1]".array;

    int[] id() {
        return (N-1).iota.array.to!(int[]);
    }
    int[] mul(int[] us, int[] vs) {
        return us.length.iota.map!(i => us[i]).map!(i => vs[i]).array;
    }

    int[] pow(int[] vs, long n) {
        if (n==0) {
            return id();
        } else {
            int[] res = pow(mul(vs, vs), n/2);
            if (n%2 == 1) {
                res = mul(res, vs);
            }
            return res;
        }
    }

    pow(
        as.fold!(
            (vs, a) {
                swap(vs[a], vs[a+1]);
                return vs;
            }
        )(id()),
        K
    ).map!(
        b => ys[b]
    ).cumulativeFold!"a+b"(xs.front).array.pipe!(
        a => xs.front.to!long.only.array ~ a
    ).each!writeln;
}

// ----------------------------------------------

void times(alias fun)(int n) {
    // n.iota.each!(i => fun());
    foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
    // return n.iota.map!(i => fun()).array;
    T[] res = new T[n];
    foreach(ref e; res) e = fun();
    return res;
}

// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                return reduce!fun(tuple(seed), r);
            }
        }
    }
}

// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
    template cumulativeFold(fun...)
    if (fun.length >= 1)
    {
        import std.meta : staticMap;
        private alias binfuns = staticMap!(binaryFun, fun);

        auto cumulativeFold(R)(R range)
        if (isInputRange!(Unqual!R))
        {
            return cumulativeFoldImpl(range);
        }

        auto cumulativeFold(R, S)(R range, S seed)
        if (isInputRange!(Unqual!R))
        {
            static if (fun.length == 1)
                return cumulativeFoldImpl(range, seed);
            else
                return cumulativeFoldImpl(range, seed.expand);
        }

        private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
        {
            import std.algorithm.internal : algoFormat;

            static assert(Args.length == 0 || Args.length == fun.length,
                algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
                    Args.stringof, fun.length));

            static if (args.length)
                alias State = staticMap!(Unqual, Args);
            else
                alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);

            foreach (i, f; binfuns)
            {
                static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
                        { args[i] = f(args[i], e); }()),
                    algoFormat("Incompatible function/seed/element: %s/%s/%s",
                        fullyQualifiedName!f, Args[i].stringof, E.stringof));
            }

            static struct Result
            {
            private:
                R source;
                State state;

                this(R range, ref Args args)
                {
                    source = range;
                    if (source.empty)
                        return;

                    foreach (i, f; binfuns)
                    {
                        static if (args.length)
                            state[i] = f(args[i], source.front);
                        else
                            state[i] = source.front;
                    }
                }

            public:
                @property bool empty()
                {
                    return source.empty;
                }

                @property auto front()
                {
                    assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
                    static if (fun.length > 1)
                    {
                        import std.typecons : tuple;
                        return tuple(state);
                    }
                    else
                    {
                        return state[0];
                    }
                }

                void popFront()
                {
                    assert(!empty, "Attempting to popFront an empty cumulativeFold.");
                    source.popFront;

                    if (source.empty)
                        return;

                    foreach (i, f; binfuns)
                        state[i] = f(state[i], source.front);
                }

                static if (isForwardRange!R)
                {
                    @property auto save()
                    {
                        auto result = this;
                        result.source = source.save;
                        return result;
                    }
                }

                static if (hasLength!R)
                {
                    @property size_t length()
                    {
                        return source.length;
                    }
                }
            }

            return Result(range, args);
        }
    }
}

// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
    auto minElement(alias map, Range)(Range r)
    if (isInputRange!Range && !isInfinite!Range)
    {
        alias mapFun = unaryFun!map;
        auto element = r.front;
        auto minimum = mapFun(element);
        r.popFront;
        foreach(a; r) {
            auto b = mapFun(a);
            if (b < minimum) {
                element = a;
                minimum = b;
            }
        }
        return element;
    }
    auto maxElement(alias map, Range)(Range r)
    if (isInputRange!Range && !isInfinite!Range)
    {
        alias mapFun = unaryFun!map;
        auto element = r.front;
        auto maximum = mapFun(element);
        r.popFront;
        foreach(a; r) {
            auto b = mapFun(a);
            if (b > maximum) {
                element = a;
                maximum = b;
            }
        }
        return element;
    }
}

Submission Info

Submission Time
Task C - Rabbit Exercise
User arkark
Language D (DMD64 v2.070.1)
Score 800
Code Size 6884 Byte
Status AC
Exec Time 155 ms
Memory 41348 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 29
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt
Case Name Status Exec Time Memory
0_00.txt AC 1 ms 256 KB
0_01.txt AC 1 ms 256 KB
0_02.txt AC 1 ms 256 KB
1_00.txt AC 1 ms 256 KB
1_01.txt AC 1 ms 256 KB
1_02.txt AC 138 ms 35460 KB
1_03.txt AC 155 ms 41348 KB
1_04.txt AC 109 ms 26068 KB
1_05.txt AC 5 ms 3196 KB
1_06.txt AC 11 ms 4092 KB
1_07.txt AC 8 ms 2664 KB
1_08.txt AC 11 ms 4108 KB
1_09.txt AC 11 ms 3208 KB
1_10.txt AC 4 ms 1020 KB
1_11.txt AC 8 ms 3560 KB
1_12.txt AC 2 ms 380 KB
1_13.txt AC 13 ms 4092 KB
1_14.txt AC 8 ms 2684 KB
1_15.txt AC 25 ms 5252 KB
1_16.txt AC 9 ms 2812 KB
1_17.txt AC 73 ms 19132 KB
1_18.txt AC 91 ms 22632 KB
1_19.txt AC 123 ms 30004 KB
1_20.txt AC 45 ms 10508 KB
1_21.txt AC 52 ms 12124 KB
1_22.txt AC 116 ms 29236 KB
1_23.txt AC 128 ms 36128 KB
1_24.txt AC 93 ms 27128 KB
1_25.txt AC 138 ms 41148 KB