結果

問題 No.391 CODING WAR
ユーザー mban
提出日時 2017-01-12 06:16:49
言語 C#
(mono 5.2.0)
結果
AC  
実行時間 207 ms
コード長 2761 Byte
コンパイル時間 293 ms
使用メモリ 18988 KB

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
00_sample_00.txt AC 28 ms
16640 KB
00_sample_01.txt AC 26 ms
14596 KB
00_sample_02.txt AC 28 ms
16624 KB
00_sample_03.txt AC 29 ms
14580 KB
01_small_00.txt AC 29 ms
18672 KB
01_small_01.txt AC 27 ms
16604 KB
01_small_02.txt AC 28 ms
16624 KB
01_small_03.txt AC 28 ms
14596 KB
01_small_04.txt AC 33 ms
16620 KB
10_large_00.txt AC 207 ms
15372 KB
10_large_01.txt AC 199 ms
17408 KB
10_large_02.txt AC 27 ms
14552 KB
10_large_03.txt AC 27 ms
16620 KB
10_large_04.txt AC 195 ms
15368 KB
11_random_00.txt AC 163 ms
15168 KB
11_random_01.txt AC 183 ms
17308 KB
11_random_02.txt AC 119 ms
17012 KB
11_random_03.txt AC 133 ms
15068 KB
11_random_04.txt AC 101 ms
18988 KB
11_random_05.txt AC 101 ms
16964 KB
テストケース一括ダウンロード

ソースコード

diff #
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;


class Program
{
    static Magatro M = new Magatro();
    static void Main()
    {
        M.Scan();
        M.Solve();
    }
}
class Magatro
{
    private long N, M;
    private const int Mod = 1000000007;
    private long[] Factorial;
    public void Scan()
    {
        Scanner sc = new Scanner();
        N = sc.NextLong();
        M = sc.NextLong();
    }
    public void Solve()
    {
        if (N < M)
        {
            Console.WriteLine(0);
            return;
        }
        FactorialCalc();
        long ans = 0;
        for(int i = 0; i < M; i++)
        {
            int plus;
            if (i % 2 == 0)
            {
                plus = 1;
            }
            else
            {
                plus = -1;
            }
            ans += Converter(plus * MCombination(i) * MyPow(M - i,N,Mod));
        }
        Console.WriteLine(ans%Mod);
    }
    private long Converter(long n)
    {
        n %= Mod;
        if (n < 0) n += Mod;
        return n;
    }
    private long MCombination(long n)
    {
        long bunshi = Factorial[M];
        long bunbo = (Factorial[n] * Factorial[M - n]) % Mod;
        return (bunshi * MyPow(bunbo, Mod - 2, Mod)) % Mod;
    }
    private void FactorialCalc()
    {
        Factorial = new long[M + 1];
        Factorial[0] = 1;
        for(int i = 1; i <= M; i++)
        {
            Factorial[i] = (Factorial[i - 1] * i) % Mod;
        }
    }
    private long MyPow(long a,long b,long mod)
    {
        long result = 1;
        while (b > 0)
        {
            if (b % 2 == 0)
            {
                a = (a * a) % mod;
                b /= 2;
            }
            else
            {
                result = (result * a) % mod;
                b--;
            }
        }
        return result;
    }
}
public class Scanner
{
    private string[] S;
    private int Index;
    private char Separator;
    public Scanner(char separator=' ')
    {
        Index = 0;
        Separator = separator;
    }
    private string[] Line()
    {
        return Console.ReadLine().Split(Separator);
    }
    public string Next()
    {
        string result;
        if (S == null || Index >= S.Length)
        {
            S = Line();
            Index = 0;
        }
        result = S[Index];
        Index++;
        return result;
    }
    public int NextInt()
    {
        return int.Parse(Next());
    }
    public double NextDouble()
    {
        return double.Parse(Next());
    }
    public long NextLong()
    {
        return long.Parse(Next());
    }
}
0