結果

問題 No.1311 Reverse Permutation Index
ユーザー ngtkanangtkana
提出日時 2020-06-15 01:25:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 1,256 bytes
コンパイル時間 2,199 ms
コンパイル使用メモリ 207,060 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 19:07:47
合計ジャッジ時間 2,936 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

/*
 * validator
 */

#include <bits/stdc++.h>

std::pair<long long, char> read() {
    long long x = 0;
    char c;
    while (true) {
        c = std::getchar();
        if (!std::isdigit(c)) {
            return std::pair(x, c);
        }
        x = 10 * x + c - '0';
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cout << std::setprecision(15) << std::fixed;

    char c;
    long long N; int n;
    std::tie(N, c) = read();
    std::tie(n, c) = read();
    assert(std::getchar()==EOF);

    std::vector<int> pinv(n), ckd(n);
    std::vector<long long> fact(n, 1);
    for (int i=1; i<n; i++) {
        fact.at(i) = fact.at(i-1) * i;
    }

    for (int i=0; i<n; i++) {
        long long f = fact.at(n-1-i);
        int q = N / f;
        for (int j=0, k=0; j<n; j++) {
            if (!ckd.at(j) && k++==q) {
                pinv.at(j) = i;
                ckd.at(j) = true;
                break;
            }
        }
        N -= f * q;
    }

    ckd.assign(n, false);
    N = 0;
    for (int i=0; i<n; i++) {
        ckd.at(pinv.at(i)) = true;
        int q = 0;
        for (int j=0; j<pinv.at(i); j++) if (!ckd.at(j)) q++;
        N += fact.at(n-1-i) * q;
    }
    std::cout << N << '\n';
}
0