結果

問題 No.823 Many Shifts Easy
ユーザー face4face4
提出日時 2019-09-27 11:46:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,532 bytes
コンパイル時間 916 ms
コンパイル使用メモリ 70,616 KB
実行使用メモリ 4,876 KB
最終ジャッジ日時 2023-10-24 16:01:24
合計ジャッジ時間 1,848 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 4 ms
4,876 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 4 ms
4,876 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 4 ms
4,876 KB
testcase_09 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;

const ll mod = 1000000007;
ll modpow(ll a, ll b, ll p = 1e9+7){
    if(b == 0)  return 1;

    if(b % 2 == 0){
        ll d = modpow(a, b/2, p);
        return (d*d) % p;
    }else{
        return (a%p * modpow(a, b-1, p)) % p;
    }
}

struct ModComb{
    vector<ll> po, inv;
    ll N;

    ModComb(ll n) : N(n), po(n), inv(n) {
        po[0] = 1;
        for(int i = 1; i < N; i++)  po[i] = (po[i-1] * i) % mod;
        inv[N-1] = modpow(po[N-1], mod-2, mod);
        for(int i = N-2; i >= 0; i--)   inv[i] = (inv[i+1] * (i+1)) % mod;
    }
    
    ll nCk(ll n, ll k){
        if(k == 0)  return 1;
        if(n < k)   return 0;
        return (((po[n]*inv[n-k])%mod)*inv[k])%mod;
    }

    ll nPk(ll n, ll k){
        if(k == 0)  return 1;
        if(n < k)   return 0;
        return (po[n]*inv[n-k])%mod;
    }

    ll nHk(ll n, ll k){
        if(n == 0 && k == 0)    return 1;
        return nCk(n+k-1, k-1);
    }
};

int main(){
    ll n, k;
    cin >> n >> k;

    // if(n == 1){
    //     cout << 0 << endl;
    //     return 0;
    // }else if(k == 1){
    //     cout << n*(n+1)/2%mod*(n-1)%mod << endl;
    // }

    ModComb mc(n+1);
    ll a = (mc.nPk(n, k) - mc.nPk(n-1, k) + mod) % mod;
    ll b = mc.nCk(k, 2)*mc.nPk(n-2, k-2)%mod;
    ll c = (a-b+mod)%mod;
    ll ans = (n-1)*n%mod*modpow(2,mod-2)%mod * ((mc.nPk(n,k)-c+mod)%mod) % mod; 
    ans += n * mc.nPk(n-1, k) % mod;
    ans %= mod;
    cout << ans << endl;
    return 0;
}
0