結果

問題 No.823 Many Shifts Easy
ユーザー kyort0nkyort0n
提出日時 2019-04-26 21:46:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 742 ms / 2,000 ms
コード長 1,549 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 166,192 KB
実行使用メモリ 27,000 KB
最終ジャッジ日時 2023-08-16 13:50:32
合計ジャッジ時間 6,678 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 233 ms
27,000 KB
testcase_01 AC 232 ms
26,852 KB
testcase_02 AC 238 ms
26,872 KB
testcase_03 AC 726 ms
26,792 KB
testcase_04 AC 234 ms
26,932 KB
testcase_05 AC 554 ms
26,840 KB
testcase_06 AC 720 ms
27,000 KB
testcase_07 AC 234 ms
26,876 KB
testcase_08 AC 742 ms
26,856 KB
testcase_09 AC 292 ms
26,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;
ll N, K;
ll inv[1000000];
ll FactorialInv[1000000];
ll Factorial[1000000];
ll beki(ll a, ll b){
    if(b == 0){
        return 1;
    }
    ll ans = beki(a, b / 2);
    ans = ans * ans % mod;
    if(b % 2 == 1){
        ans = ans * a % mod;
    }
    return ans;
}
void init_combination(){
    inv[1] = 1;
    FactorialInv[1] = 1;
    Factorial[1] = 1;
    Factorial[0] = 1;
    FactorialInv[0] = 1;
    inv[1] = 0;
    for(int i = 2; i < 1000000; i++){
        inv[i] = beki(i, mod - 2);
        Factorial[i] = Factorial[i - 1] * i % mod;
        FactorialInv[i] = FactorialInv[i - 1] * inv[i] % mod;
    }
}
ll combination(ll a, ll b){
    if(a < 0) return 0;
    if(b < 0) return 0;
    if(b > a) return 0;
    if((a == b) || (b == 0)){
        return 1;
    }
    ll ans = Factorial[a] * FactorialInv[b] % mod;
    ans = ans * FactorialInv[a - b] % mod;
    return ans;
}

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> K;
    init_combination();
    ll ans = 0;
    for(ll i = 1; i <= N; i++) {
        ans += (i * combination(N-1, K) % mod) * Factorial[K];
        ans %= mod;
        if(i < N) ans += (i * combination(N-2, K-2) % mod) * (inv[2] * Factorial[K] % mod);
        ans %= mod;
        cerr << i << " " << ans << endl;
    }
    cout << ans << endl;
    return 0;
}
0