Submission #14770926


Source Code Expand

// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on

/// x^^n % m
long powmod(long x, long n)
{
    alias T = long;
    T m = 10 ^^ 9 + 7;
    if (n < 1)
        return 1;
    if (n & 1)
    {
        return x * powmod(x, n - 1) % m;
    }
    T tmp = powmod(x, n / 2);
    return tmp * tmp % m;
}

long invmod(long x)
{
    return powmod(x, MOD - 2L);
}
// alias invmod = (long x, long m = 10 ^^ 9 + 7) => powmod(x, m - 2, m);

void main()
{
    long N, M;
    scan(N, M);

    auto memo = new long[](M + 1);
    memo[0] = 1;
    foreach (i; 1 .. M + 1)
        memo[i] = memo[i - 1] * i % MOD;
    auto inv = new long[](M + 1);
    foreach (i; 0 .. M + 1)
        inv[i] = invmod(memo[i]);

    long Permutation(long n, long r)
    {
        return memo[n] * inv[n - r] % MOD;
    }

    long Combination(long n, long r)
    {
        return (memo[n] * inv[n - r] % MOD) * inv[r] % MOD;
    }

    long a = Permutation(M, N);
    foreach (i; 1 .. N + 1)
    {
        long b = Permutation(M - i, N - i);
        // dprint(i, a, b, (N - i + 1));
        a += MOD;
        a += (b * Combination(N, i) % MOD) * ((i & 1) ? -1 : 1);
        a %= MOD;
    }
    writeln(a * Permutation(M, N) % MOD);
}

Submission Info

Submission Time
Task E - NEQ
User kotet
Language D (DMD 2.091.0)
Score 500
Code Size 1765 Byte
Status AC
Exec Time 173 ms
Memory 11392 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 12
Set Name Test Cases
Sample sample00, sample01, sample02
All handmade03, handmade04, handmade05, handmade06, handmade07, random08, random09, random10, random11, sample00, sample01, sample02
Case Name Status Exec Time Memory
handmade03 AC 9 ms 3416 KiB
handmade04 AC 158 ms 11392 KiB
handmade05 AC 91 ms 7636 KiB
handmade06 AC 142 ms 10116 KiB
handmade07 AC 173 ms 11304 KiB
random08 AC 163 ms 11132 KiB
random09 AC 72 ms 6452 KiB
random10 AC 148 ms 10248 KiB
random11 AC 78 ms 6412 KiB
sample00 AC 6 ms 3572 KiB
sample01 AC 2 ms 3496 KiB
sample02 AC 125 ms 9232 KiB