結果
| 問題 |
No.1311 Reverse Permutation Index
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-01-02 00:10:06 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 1,500 ms |
| コード長 | 965 bytes |
| コンパイル時間 | 1,714 ms |
| コンパイル使用メモリ | 171,084 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-27 00:31:07 |
| 合計ジャッジ時間 | 2,113 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 6 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ll n, s;
cin >> n >> s;
vector<ll> fact(s + 1);
fact[0] = 1;
for(int i = 1; i <= s; i++)fact[i] = i * fact[i - 1];
vector<int> p(s), p2(s);
function<void(int,int,ll)> dfs = [&](int i, int S, ll rem){
if(i == s)return;
ll v = 0;
int cand = -1;
for(int j = 0; j < s; j++){
if(S >> j & 1)continue;
if(rem >= fact[s - 1 - i]){
rem -= fact[s - 1 - i];
continue;
}
p[i] = j;
dfs(i + 1, S | (1 << j), rem);
break;
}
};
dfs(0, 0, n);
ll ans = 0;
for(int i = 0; i < s; i++)p2[p[i]] = i;
for(int i = 0, S = 0; i < s; i++){
for(int j = 0; j < p2[i]; j++){
if(S >> j & 1)continue;
ans += fact[s - 1 - i];
}
S |= 1 << p2[i];
}
cout << ans << endl;
}