提出 #1905586


ソースコード 拡げる

/+ dub.sdl:
    name "A"
    dependency "dunkelheit" version=">=0.9.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;

// import dkh.modint;
alias Mint = ModInt!(10^^9 + 7);

Mint naive(int n, int md, int k) {
    int ans = 0;
    foreach (a; iota(n).permutations) {
        int x = 0;
        foreach (i; 0..n) {
            foreach (j; i+1..n) {
                if (a[i] > a[j]) x++;
            }
        }
        if (x % md == k) ans++;
    }
    return Mint(ans);
}

Mint solve(long n, int md, int k) {
    if (n <= 10) {
        return naive(n.to!int, md, k);
    }
    if (10^^9 + 10 < n) {
        return Mint(0);
    }
    int B = n.to!int / 3000000;
    Mint ans = base[B];
    foreach (i; B*3000000+1..n+1) {
        ans *= Mint(i);
    }
    ans /= Mint(md);
    return ans;
}

void main() {
    long n; int md, k;
    sc.read(n, md, k);
    writeln(solve(n, md, k));
}

Scanner sc;
static this() {
    sc = new Scanner(stdin);
}

auto base = [
1,
5832229,
316220877,
980250487,
95936601,
598816162,
626497524,
245341950,
968999,
881746146,
76479948,
886294336,
544075857,
82926604,
721996174,
172827403,
294587621,
497478507,
228338256,
746536789,
27368307,
5974669,
86609485,
371263376,
808412394,
1669644,
595143852,
526807168,
459188207,
840398449,
888050723,
440902696,
56305184,
804805577,
934814019,
112249297,
47401357,
60168088,
14235602,
697066277,
661224977,
802214249,
583967898,
972424306,
243276029,
797848181,
159554990,
250388809,
82104855,
244109365,
261384175,
747664143,
80729842,
827348878,
457882808,
509096183,
677711203,
555972231,
349551356,
66247239,
547665832,
917404120,
23015220,
187348575,
105574531,
34538816,
630621321,
616368450,
557301633,
958992544,
724691727,
946237212,
492237273,
763017204,
740594930,
464456846,
116261993,
173784895,
481661251,
91068149,
136026497,
113570024,
270239666,
702807165,
103056890,
167240465,
479334442,
277717575,
353339603,
192345193,
217544623,
183011467,
898554793,
611251733,
160398980,
607730875,
648991369,
780122909,
460373282,
358700839,
668123525,
957633609,
841799766,
954061253,
299172297,
318951960,
204355779,
450398100,
472935553,
776264326,
522049725,
142946099,
332139472,
164752359,
580204973,
624577416,
836813268,
184419613,
715247054,
383388563,
189239124,
405755338,
825132993,
868553027,
882148737,
529726489,
241107368,
11633075,
115312728,
878471898,
917084264,
808651347,
59883031,
715317963,
316486674,
937409008,
833550543,
987314628,
274192146,
952831975,
358655417,
987597705,
125354499,
343043237,
209244402,
392838702,
263053337,
551028176,
155569943,
268844715,
462639908,
382776490,
675258797,
741114145,
325912072,
432030917,
754363835,
278704903,
424989675,
832069712,
99199382,
302020341,
726997875,
183888367,
732868367,
863250534,
352946234,
503105966,
16634739,
985594252,
97830135,
662741752,
167831173,
91352335,
385388084,
46819124,
442795120,
152028387,
553033642,
996049638,
141827977,
593938674,
565456578,
551096742,
370732451,
457469634,
777901604,
956856710,
462528877,
100498586,
811575797,
907720182,
508038818,
401382061,
34629406,
26011548,
206330671,
185646898,
523696723,
859369491,
724464507,
626663115,
596949867,
465070856,
48824684,
564188856,
484458461,
181594759,
965656187,
936229527,
456152084,
262322489,
483924582,
490222511,
368114731,
218107212,
322908524,
573074791,
744158074,
9416035,
769795511,
137603545,
919419361,
310317874,
256853930,
147050765,
180350107,
384547635,
682189174,
819114541,
825871994,
314738056,
815463367,
188054995,
933376898,
765215899,
632627667,
282092463,
827722926,
322096036,
852304035,
555842733,
212842957,
155133422,
206282795,
414236650,
534279149,
811053196,
219932130,
398782410,
217598709,
178598958,
812283640,
113917835,
564827277,
439411911,
48562889,
945204804,
897162044,
828883117,
624500515,
514359662,
628829100,
513196123,
574455974,
819801784,
15391652,
264582598,
986598821,
369832433,
423951674,
496709826,
541120825,
162683802,
269571439,
341080135,
751040886,
72482816,
100228913,
906233141,
814362881,
885537778,
983737173,
804930491,
946421040,
175638827,
885362182,
749816133,
281804989,
640266394,
256473217,
322633051,
304644842,
754787150,
799289297,
687265514,
701220427,
600624983,
759404319,
22556579,
586445753,
763514207,
351472920,
634359666,
364380585,
260466949,
237120480,
213092254,
793307102,
362928234,
778983779,
698308420,
713258160,
804861409,
217298111,
954913,
10430738,
567473423,
946149627,
462880311,
965785236,
881482632,
512634493,
967284733,
165393390,
532702135,
536841269,
301196854,
373451745,
576368335,
847549272,
216046746,
780882948,
869544707,
];
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;

 
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

     
    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
    
    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }  
    ref inout(T) front() inout { return this[0]; }  
    ref inout(T) back() inout { return this[$-1]; }  

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }  
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }  
     
    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }  
    alias opOpAssign(string op : "~") = insertBack;  
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }  
}

 
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/ldc/inline.d */
// module dkh.ldc.inline;

version(LDC) {
    pragma(LDC_inline_ir) R inlineIR(string s, R, P...)(P);
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/foundation.d */
 
// module dkh.foundation;

 
static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }
     
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/modint.d */
// module dkh.modint;

// import dkh.numeric.primitive;

 
struct ModInt(uint MD) if (MD < int.max) {
    import std.conv : to;
    uint v;
    this(int v) {this(long(v));}
    this(long v) {this.v = (v%MD+MD)%MD;}
    static auto normS(uint x) {return (x<MD)?x:x-MD;}
    static auto make(uint x) {ModInt m; m.v = x; return m;}
     
    auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}
     
    auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}
     
    auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!uint);}
     
    auto opBinary(string op:"/")(ModInt r) const {return this*inv(r);}
    auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}
     
    static ModInt inv(ModInt x) {return ModInt(extGcd!int(x.v, MD)[0]);}
    string toString() const {return v.to!string;}
}

 
 

 

 
struct DModInt(string name) {
    import std.conv : to;
    static uint MD;
    uint v;
    this(int v) {this(long(v));}
    this(long v) {this.v = ((v%MD+MD)%MD).to!uint;}
    static auto normS(uint x) {return (x<MD)?x:x-MD;}
    static auto make(uint x) {DModInt m; m.MD = MD; m.v = x; return m;}
     
    auto opBinary(string op:"+")(DModInt r) const {return make(normS(v+r.v));}
     
    auto opBinary(string op:"-")(DModInt r) const {return make(normS(v+MD-r.v));}
     
    auto opBinary(string op:"*")(DModInt r) const {return make((ulong(v)*r.v%MD).to!uint);}
     
    auto opBinary(string op:"/")(DModInt r) const {return this*inv(r);}
    auto opOpAssign(string op)(DModInt r) {return mixin ("this=this"~op~"r");}
     
    static DModInt inv(DModInt x) {
        return DModInt(extGcd!int(x.v, MD)[0]);
    }
    string toString() {return v.to!string;}
}

 
 

 

template isModInt(T) {
    const isModInt =
        is(T : ModInt!MD, uint MD) || is(S : DModInt!S, string s);
}


T[] factTable(T)(size_t length) if (isModInt!T) {
    import std.range : take, recurrence;
    import std.array : array;
    return T(1).recurrence!((a, n) => a[n-1]*T(n)).take(length).array;
}

T[] invFactTable(T)(size_t length) if (isModInt!T) {
    import std.algorithm : map, reduce;
    import std.range : take, recurrence, iota;
    import std.array : array;
    auto res = new T[length];
    res[$-1] = T(1) / iota(1, length).map!T.reduce!"a*b";
    foreach_reverse (i, v; res[0..$-1]) {
        res[i] = res[i+1] * T(i+1);
    }
    return res;
}

T[] invTable(T)(size_t length) if (isModInt!T) {
    auto f = factTable!T(length);
    auto invf = invFactTable!T(length);
    auto res = new T[length];
    foreach (i; 1..length) {
        res[i] = invf[i] * f[i-1];
    }
    return res;
}

 
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;

// import dkh.container.stackpayload;

 
class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray; 
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {
                 
                 
                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}


 
 

 
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/numeric/primitive.d */
// module dkh.numeric.primitive;

import std.traits;
import std.bigint;

 
Unqual!T pow(T, U)(T x, U n)
if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) {
    return pow(x, n, T(1));
}

 
Unqual!T pow(T, U, V)(T x, U n, V e)
if ((isIntegral!U || is(U == BigInt)) && is(Unqual!T == Unqual!V)) {
    Unqual!T b = x, v = e;
    Unqual!U m = n;
    while (m) {
        if (m & 1) v *= b;
        b *= b;
        m /= 2;
    }
    return v;
}

 

 
T powMod(T, U, V)(T x, U n, V md)
if (isIntegral!U || is(U == BigInt)) {
    T r = T(1);
    while (n) {
        if (n & 1) r = (r*x)%md;
        x = (x*x)%md;
        n >>= 1;
    }
    return r % md;
}

// import dkh.int128;

 
ulong ulongPowMod(U)(ulong x, U n, ulong md)
if (isIntegral!U || is(U == BigInt)) {
    x %= md;
    ulong r = 1;
    while (n) {
        if (n & 1) {
            r = mul128(r, x).mod128(md);
        }
        x = mul128(x, x).mod128(md);
        n >>= 1;
    }
    return r % md;
}

 
T lcm(T)(in T a, in T b) {
    import std.numeric : gcd;
    return a / gcd(a,b) * b;
}

 
 

 
 
T[3] extGcd(T)(in T a, in T b) 
if (!isIntegral!T || isSigned!T)  
{
    if (b==0) {
        return [T(1), T(0), a];
    } else {
        auto e = extGcd(b, a%b);
        return [e[1], e[0]-a/b*e[1], e[2]];
    }
}

 
 
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/int128.d */
 

// module dkh.int128;

version(LDC) {
//     import dkh.ldc.inline;
}

version(LDC) version(X86_64) {
    version = LDC_IR;
}

 
ulong[2] mul128(ulong a, ulong b) {
    ulong[2] res;
    version(LDC_IR) {
        ulong upper, lower;
        inlineIR!(`
            %r0 = zext i64 %0 to i128 
            %r1 = zext i64 %1 to i128
            %r2 = mul i128 %r1, %r0
            %r3 = trunc i128 %r2 to i64
            %r4 = lshr i128 %r2, 64
            %r5 = trunc i128 %r4 to i64
            store i64 %r3, i64* %2
            store i64 %r5, i64* %3`, void)(a, b, &lower, &upper);
        return [lower, upper];
    } else version(D_InlineAsm_X86_64) {
        ulong upper, lower;
        asm {
            mov RAX, a;
            mul b;
            mov lower, RAX;
            mov upper, RDX;
        }
        return [lower, upper];
    } else {
        ulong B = 2UL^^32;
        ulong[2] a2 = [a % B, a / B];
        ulong[2] b2 = [b % B, b / B];
        ulong[4] c;
        foreach (i; 0..2) {
            foreach (j; 0..2) {
                c[i+j] += a2[i] * b2[j] % B;
                c[i+j+1] += a2[i] * b2[j] / B;
            }
        }
        foreach (i; 0..3) {
            c[i+1] += c[i] / B;
            c[i] %= B;
        }
        return [c[0] + c[1] * B, c[2] + c[3] * B];
    }
}

 

 
ulong div128(ulong[2] a, ulong b) {
    version(LDC_IR) {
        return inlineIR!(`
            %r0 = zext i64 %0 to i128
            %r1 = zext i64 %1 to i128
            %r2 = shl i128 %r1, 64
            %r3 = add i128 %r0, %r2
            %r4 = zext i64 %2 to i128
            %r5 = udiv i128 %r3, %r4
            %r6 = trunc i128 %r5 to i64
            ret i64 %r6`,ulong)(a[0], a[1], b);
    } else version(D_InlineAsm_X86_64) {
        ulong upper = a[1], lower = a[0];
        ulong res;
        asm {
            mov RDX, upper;
            mov RAX, lower;
            div b;
            mov res, RAX;
        }
        return res;
    } else {
        if (b == 1) return a[0];
        while (!(b & (1UL << 63))) {
            a[1] <<= 1;
            if (a[0] & (1UL << 63)) a[1] |= 1;
            a[0] <<= 1;
            b <<= 1;
        }
        ulong ans = 0;
        foreach (i; 0..64) {
            bool up = (a[1] & (1UL << 63)) != 0;
            a[1] <<= 1;
            if (a[0] & (1UL << 63)) a[1] |= 1;
            a[0] <<= 1;

            ans <<= 1;
            if (up || b <= a[1]) {
                a[1] -= b;
                ans++;
            }
        }
        return ans;
    }
}


 
ulong mod128(ulong[2] a, ulong b) {
    version(D_InlineAsm_X86_64) {
        ulong upper = a[1], lower = a[0];
        ulong res;
        asm {
            mov RDX, upper;
            mov RAX, lower;
            div b;
            mov res, RDX;
        }
        return res;
    } else {
        return a[0] - div128(a, b) * b;
    }
}

 

/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/

提出情報

提出日時
問題 D - Inversion Number
ユーザ cookie_marisa
言語 D (DMD64 v2.070.1)
得点 100
コード長 17622 Byte
結果 AC
実行時間 63 ms
メモリ 256 KiB

ジャッジ結果

セット名 All
得点 / 配点 100 / 100
結果
AC × 29
セット名 テストケース
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, sample-01.txt, sample-02.txt, sample-03.txt
ケース名 結果 実行時間 メモリ
01.txt AC 1 ms 256 KiB
02.txt AC 1 ms 256 KiB
03.txt AC 1 ms 256 KiB
04.txt AC 1 ms 256 KiB
05.txt AC 1 ms 256 KiB
06.txt AC 1 ms 256 KiB
07.txt AC 1 ms 256 KiB
08.txt AC 1 ms 256 KiB
09.txt AC 2 ms 256 KiB
10.txt AC 63 ms 256 KiB
11.txt AC 63 ms 256 KiB
12.txt AC 1 ms 256 KiB
13.txt AC 1 ms 256 KiB
14.txt AC 1 ms 256 KiB
15.txt AC 1 ms 256 KiB
16.txt AC 1 ms 256 KiB
17.txt AC 1 ms 256 KiB
18.txt AC 1 ms 256 KiB
19.txt AC 1 ms 256 KiB
20.txt AC 1 ms 256 KiB
21.txt AC 1 ms 256 KiB
22.txt AC 12 ms 256 KiB
23.txt AC 10 ms 256 KiB
24.txt AC 11 ms 256 KiB
25.txt AC 25 ms 256 KiB
26.txt AC 16 ms 256 KiB
sample-01.txt AC 1 ms 256 KiB
sample-02.txt AC 2 ms 256 KiB
sample-03.txt AC 1 ms 256 KiB