結果

問題 No.1311 Reverse Permutation Index
ユーザー kk
提出日時 2020-12-11 18:56:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 901 bytes
コンパイル時間 2,284 ms
コンパイル使用メモリ 206,992 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 01:37:03
合計ジャッジ時間 2,819 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  long long n;
  int s;
  cin >> n >> s;

  vector<int> v(s);
  vector<bool> used(s);
  vector<long long> f(s);
  f[0] = 1;
  for (int i = 1; i < s; i++)
    f[i] = f[i-1] * i;
  
  for (int i = 0; i < s; i++) {
    for (int j = 0; j < s; j++) {
      if (used[j]) continue;
      if (n == 0 || n < f[s-1-i]) {
        v[i] = j;
        used[j] = true;
        break;
      }
      else {
        n -= f[s-1-i];
      }
    }
  }

  vector<int> w(s);
  for (int i = 0; i < s; i++)
    w[v[i]] = i;

  long long ret = 0;
  vector<bool> added(s);
  for (int i = 0; i < s; i++) {
    added[w[i]] = true;
    for (int j = 0; j < w[i]; j++) {
      if (!added[j]) {
        ret += f[s-1-i];
      }
    }
  }

  cout << ret << endl;
  
  return 0;
}
0