結果

問題 No.1311 Reverse Permutation Index
ユーザー SSRSSSRS
提出日時 2020-12-08 00:09:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 770 bytes
コンパイル時間 1,546 ms
コンパイル使用メモリ 171,784 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 16:46:30
合計ジャッジ時間 2,147 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 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 <bits/stdc++.h>
using namespace std;
long long fact(int n){
  long long ans = 1;
  for (int i = 1; i <= n; i++){
    ans *= i;
  }
  return ans;
}
int main(){
  long long N;
  int S;
  cin >> N >> S;
  vector<int> p;
  vector<int> r;
  for (int i = 0; i < S; i++){
    r.push_back(i);
  }
  for (int i = 0; i < S; i++){
    int q = N / fact(S - 1 - i);
    p.push_back(r[q]);
    N %= fact(S - 1 - i);
    r.erase(r.begin() + q);
  }
  vector<int> pp(S);
  for (int i = 0; i < S; i++){
    pp[p[i]] = i;
  }
    vector<int> rr;
  long long ans = 0;
  for (int i = 0; i < S; i++){
    int cnt = pp[i];
    for (int j : rr){
      if (j < pp[i]){
        cnt--;
      }
    }
    ans += cnt * fact(S - 1 - i);
    rr.push_back(pp[i]);
  }
  cout << ans << endl;
}
0