結果

問題 No.1311 Reverse Permutation Index
ユーザー MisterMister
提出日時 2021-01-05 19:47:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 1,101 bytes
コンパイル時間 666 ms
コンパイル使用メモリ 82,184 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 23:16:21
合計ジャッジ時間 1,218 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#line 1 "main.cpp"
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>

using namespace std;

using lint = unsigned long long;

lint fact(lint n) {
    lint p = 1;
    for (lint x = 1; x <= n; ++x) p *= x;
    return p;
}

void solve() {
    lint p;
    int n;
    cin >> p >> n;

    vector<int> ps(n);
    {
        vector<int> rem(n);
        iota(rem.begin(), rem.end(), 0);

        for (int i = 0, j = n - 1; i < n; ++i, --j) {
            lint x = p / fact(j);
            p %= fact(j);

            ps[i] = rem[x];
            rem.erase(rem.begin() + x);
        }
    }

    vector<int> qs(n);
    for (int i = 0; i < n; ++i) qs[ps[i]] = i;

    lint ans = 0;
    {
        vector<int> app;

        for (int i = 0, j = n - 1; i < n; ++i, --j) {
            int x = qs[i];
            for (auto y : app) {
                if (y < qs[i]) --x;
            }
            ans += x * fact(j);

            app.push_back(qs[i]);
        }
    }
    cout << ans << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0