結果

問題 No.1311 Reverse Permutation Index
ユーザー yoshykaiyoshykai
提出日時 2020-12-08 11:49:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 58 ms / 1,500 ms
コード長 3,651 bytes
コンパイル時間 4,591 ms
コンパイル使用メモリ 85,476 KB
実行使用メモリ 53,572 KB
最終ジャッジ日時 2023-10-17 17:07:33
合計ジャッジ時間 4,592 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
53,564 KB
testcase_01 AC 57 ms
53,556 KB
testcase_02 AC 58 ms
53,556 KB
testcase_03 AC 58 ms
53,556 KB
testcase_04 AC 58 ms
53,560 KB
testcase_05 AC 57 ms
53,548 KB
testcase_06 AC 57 ms
53,552 KB
testcase_07 AC 58 ms
53,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
class Main{
  public static void main(String args[]){
    long n = readLong();
    int s=readI();
    ArrayList<Integer> list = new ArrayList<Integer>();
    for(int i=0;i<s;i++){list.add(i);}
    int ar[]=new int[s];
    long ff=fact(s-1);
    for(int i=0;i<s;i++){
      long t = n/ff;
      ar[i]=list.get((int)t);
      list.remove((int)t);
      n=n%ff;
      if(i!=s-1){
        ff/=(s-i-1);
      }
    }
    int ans[]=new int[s];
    for(int i=0;i<s;i++){
      ans[ar[i]]=i;
    }
    long res=0;
    list = new ArrayList<Integer>();for(int i=0;i<s;i++){list.add(i);}
    ff=fact(s-1);
    for(int i=0;i<s;i++){
      int t = list.indexOf(ans[i]);
      res += t*ff;
      list.remove(t);
      if(i!=s-1){
        ff/=(s-i-1);
      }
    }
    pl(res+"");
  }

  public static long fact(int i){
    if(i==1||i==0){
      return 1L;
    }
    return i*fact(i-1);
  }

  public static long gcd(long a,long b){
    if(a<b){return gcd(b,a);}
    long c=a%b;
    while(c!=0){
      a=b;b=c;
      c=a%b;
    }
    return b;
  }

  public static boolean next_permutation(int[] a) {
    for (int i = a.length - 2; i >= 0; i--) {
      if (a[i] < a[i + 1]) {
        for (int j = a.length - 1;; j--) {
          if (a[i] < a[j]) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            for (i++, j = a.length - 1; i < j; i++, j--) {
              temp = a[i];
              a[i] = a[j];
              a[j] = temp;
            }
            return true;
          }
        }
      }
    }
    return false;
  }

  static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  public static void pr(String str){
    System.out.print(str);
  }
  public static void pl(String str){
    System.out.println(str);
  }
  public static String read(){
    try{
      return ctos((char)br.read());
    }catch(IOException e){
      e.printStackTrace();
      return "";
    }
  }
  public static char readC(){
    try{
      return (char)br.read();
    }catch(IOException e){
      e.printStackTrace();
      return (char)-1;
    }
  }
  public static String readL(){
    try{
      return br.readLine();
    }catch(IOException e){
      e.printStackTrace();
      return "";
    }
  }
  public static String readS(){
    StringBuilder sb = new StringBuilder();
    while(true){
      try{
        int k = br.read();
        if(k==-1||(char)k==' '||(char)k=='\n'){break;}
        sb.append((char)k);
      }catch(IOException e){
        e.printStackTrace();
      }
    }
    return sb.toString();
  }
  public static int readI(){
    return stoi(readS());
  }
  public static long readLong(){
    return stol(readS());
  }
  public static long stol(String s){
    return Long.parseLong(s);
  }
  public static String[] readSs(){
    return readL().split(" ");
  }
  public static int[] readIs(){
    return stoi(readSs());
  }
  public static int stoi(String s){
    return Integer.parseInt(s);
  }
  public static int[] stoi(String s[]){
    int a[]=new int[s.length];
    for(int i=0;i<s.length;i++){
      a[i]=stoi(s[i]);
    }
    return a;
  }
  public static String itos(int i){
    return String.valueOf(i);
  }
  public static String[] itos(int[] a){
    String s[]=new String[a.length];
    for(int i=0;i<a.length;i++){
      s[i]=itos(a[i]);
    }
    return s;
  }
  public static String ctos(char c){
    return String.valueOf(c);
  }
  public static String cstos(char[] c){
    return new String(c);
  }
  public static char stoc(String s){
    return s.charAt(0);
  }
  public static char[] stocs(String s){
    return s.toCharArray();
  }
}
0