結果

問題 No.391 CODING WAR
ユーザー mamekinmamekin
提出日時 2016-10-08 23:14:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 2,475 bytes
コンパイル時間 767 ms
コンパイル使用メモリ 105,484 KB
実行使用メモリ 4,704 KB
最終ジャッジ日時 2023-08-14 02:39:39
合計ジャッジ時間 2,026 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 47 ms
4,640 KB
testcase_10 AC 47 ms
4,696 KB
testcase_11 AC 39 ms
4,704 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 43 ms
4,692 KB
testcase_14 AC 35 ms
4,420 KB
testcase_15 AC 39 ms
4,376 KB
testcase_16 AC 23 ms
4,376 KB
testcase_17 AC 29 ms
4,380 KB
testcase_18 AC 19 ms
4,376 KB
testcase_19 AC 19 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

const int MOD = 1000000007;

long long extgcd(long long a, long long b, long long &x, long long &y) {
    long long g = a;
    if(b != 0){
        g = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }else{
        x = 1;
        y = 0;
    }
    return g;
}

long long mod_inverse(long long a, long long m)
{
    long long x, y;
    extgcd(a, m, x, y);
    return (x % m + m) % m;
}

class FactorialCalculation
{
private:
    const int mod;
    vector<long long> factorial;
    vector<long long> invFactorial;
public:
    FactorialCalculation(int n, int mod) : mod(mod)
    {
        factorial.resize(n+1, 1);
        invFactorial.resize(n+1, 1);
        for(int i=1; i<=n; ++i){
            factorial[i] = factorial[i-1] * i % mod;
            invFactorial[i] = mod_inverse(factorial[i], mod);
        }
    }
    long long getFactorial(int n){
        return factorial[n];
    }
    long long getPermutation(int n, int r){
        if(n < r)
            return 0;
        return factorial[n] * invFactorial[n-r] % mod;
    }
    long long getCombination(int n, int r){
        if(n < r)
            return 0;
        return getPermutation(n, r) * invFactorial[r] % mod;
    }
    long long getHomogeneous(int n, int r){
        return getCombination(n+r-1, r);
    }
};

long long power(long long a, long long b)
{
    long long ret = 1;
    long long tmp = a % MOD;
    while(b > 0){
        if(b & 1){
            ret *= tmp;
            ret %= MOD;
        }
        tmp *= tmp;
        tmp %= MOD;
        b >>= 1;
    }
    return ret;
}

int main()
{
    long long n;
    int m;
    cin >> n >> m;

    FactorialCalculation fc(m, MOD);

    long long ans = 0;
    for(int i=m; i>=1; --i){
        if((m - i) % 2 == 0){
            ans += fc.getCombination(m, i) * power(i, n);
            ans %= MOD;
        }
        else{
            ans -= fc.getCombination(m, i) * power(i, n);
            ans %= MOD;
            ans += MOD;
            ans %= MOD;
        }
    }
    cout << ans << endl;

    return 0;
}
0