結果

問題 No.1311 Reverse Permutation Index
ユーザー Mister
提出日時 2021-01-05 19:47:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 1,101 bytes
コンパイル時間 633 ms
コンパイル使用メモリ 78,524 KB
最終ジャッジ日時 2025-01-17 09:48:56
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 6
権限があれば一括ダウンロードができます

ソースコード

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