結果

問題 No.411 昇順昇順ソート
ユーザー tactac
提出日時 2019-08-02 16:17:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,799 bytes
コンパイル時間 1,471 ms
コンパイル使用メモリ 165,432 KB
実行使用メモリ 15,604 KB
最終ジャッジ日時 2023-09-18 18:15:59
合計ジャッジ時間 3,131 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
15,356 KB
testcase_01 AC 12 ms
15,296 KB
testcase_02 AC 13 ms
15,360 KB
testcase_03 AC 13 ms
15,296 KB
testcase_04 AC 13 ms
15,296 KB
testcase_05 AC 13 ms
15,356 KB
testcase_06 AC 13 ms
15,584 KB
testcase_07 AC 13 ms
15,296 KB
testcase_08 AC 13 ms
15,276 KB
testcase_09 AC 13 ms
15,360 KB
testcase_10 AC 13 ms
15,352 KB
testcase_11 AC 13 ms
15,604 KB
testcase_12 AC 13 ms
15,300 KB
testcase_13 AC 13 ms
15,280 KB
testcase_14 AC 12 ms
15,296 KB
testcase_15 AC 13 ms
15,488 KB
testcase_16 AC 13 ms
15,380 KB
testcase_17 AC 13 ms
15,356 KB
testcase_18 AC 13 ms
15,356 KB
testcase_19 AC 13 ms
15,280 KB
testcase_20 AC 12 ms
15,384 KB
testcase_21 AC 13 ms
15,344 KB
testcase_22 AC 13 ms
15,488 KB
testcase_23 AC 13 ms
15,296 KB
testcase_24 AC 14 ms
15,352 KB
testcase_25 AC 14 ms
15,336 KB
testcase_26 AC 14 ms
15,280 KB
testcase_27 AC 13 ms
15,300 KB
testcase_28 AC 13 ms
15,492 KB
testcase_29 AC 13 ms
15,360 KB
testcase_30 AC 13 ms
15,356 KB
testcase_31 AC 13 ms
15,280 KB
testcase_32 AC 14 ms
15,344 KB
testcase_33 AC 13 ms
15,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep3(i, l, n) for (int i = l; i < n; ++i)
#define chmax(a, b) a = (a >= b ? a : b)
#define chmin(a, b) a = (a <= b ? a : b)
#define out(a) cout << a << endl
#define outa(a, n) rep(_, n) cout << a[_] << " "; cout << endl
#define SZ(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
#define FIX(a) fixed << setprecision(a)
string str;
int n, c, d, k;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}



int main() {
    COMinit();
    cin >> n >> k;
    // sample 3, 5 3
    if (k == 1) {
        c += n - k - 1; // 1 2を防ぐ
        rep3(i, 2, n) {
            ll tmp = COM(n - k, i) - 1;
            // out(tmp << " " << n << " " << k << " " << i << " " << COM(n - k, i));
            if (tmp <= 0) break;
            c += tmp;
        }
        out(c);
        
        return 0;
    }
    c = 1; // 3 1 2 4 5
    c += n - k; // 3 4, 3 5
    // 3 4 5
    
    rep3(i, 2, n) {
        ll tmp = COM(n - k, i);
        if (tmp <= 0) break;
        c += tmp;
    }
    out(c);
}
0