結果

問題 No.1311 Reverse Permutation Index
ユーザー milanis48663220milanis48663220
提出日時 2020-12-08 00:48:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 1,568 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 97,888 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 16:49:06
合計ジャッジ時間 1,674 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

ll fac[21];

void init(){
    fac[0] = 1;
    for(int i = 1; i <= 20; i++) {
        fac[i] = fac[i-1]*i;
        assert(fac[i] < __LONG_LONG_MAX__);
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    init();
    ll n, s; cin >> n >> s;
    vector<int> idx(s);
    vector<int> p(s);
    vector<bool> used(s, false);
    for(int i = 0; i < s; i++){
        ll m = n/fac[s-i-1];
        n %= fac[s-i-1];
        int cnt = 0;
        for(int j = 0; ; j++){
            if(!used[j]) {
                if(cnt == m){
                    used[j] = true;
                    p[i] = j;
                    idx[j] = i;
                    break;
                }
                cnt++;
            }
        }
    }
    ll ans = 0;
    for(int i = 0; i < s; i++){
        int inv_cnt = 0;
        for(int j = i+1; j < s; j++){
            if(idx[i] > idx[j]) inv_cnt++;
        }
        ans += fac[s-i-1]*inv_cnt;
    }
    cout << ans << endl;
}
0