@@ -0,0 +1,1962 @@
+import java.util.*;%0Aimport static java.lang.Math.*;%0Aimport java.util.stream.*;%0Aimport java.math.BigInteger;%0A%0A/*%0AProblem name: 10460 Find the Permuted String%0AProblem url: https://uva.onlinejudge.org/external/104/10460.pdf%0AAuthor: Andrey Yemelyanov%0A*/%0Apublic class _10460_FindPermutedString %7B%0A  public static void main(String%5B%5D args)%7B%0A    Scanner s = new Scanner(System.in);%0A    int nTests = s.nextInt();%0A    while(nTests-- %3E  0)%7B%0A      String str = s.next();%0A      int n = s.nextInt();%0A      System.out.println(getNthPermutation(str, n));%0A    %7D%0A  %7D%0A%0A  static String getNthPermutation(String str, int n)%7B%0A    StringBuilder p = new StringBuilder();%0A    p.append(str.charAt(0));%0A    getNthPermutation(n, str, p, BigInteger.ONE, factorial(str.length()), 1);%0A    return p.toString();%0A  %7D%0A%0A  static BigInteger factorial(int n)%7B%0A    BigInteger f = BigInteger.ONE;%0A    for(int i = 1; i %3C= n; i++)%7B%0A      f = f.multiply(BigInteger.valueOf(i));%0A    %7D%0A    return f;%0A  %7D%0A%0A  static void getNthPermutation(int n, String str, StringBuilder p, BigInteger from, BigInteger to, int i)%7B%0A    if(p.length() == str.length()) return;%0A    BigInteger%5B%5D range = getRange(p.toString(), from, to, BigInteger.valueOf(n));%0A    p.insert(range%5B0%5D.intValue(), str.charAt(i));%0A    getNthPermutation(n, str, p, range%5B1%5D, range%5B2%5D, i + 1);%0A  %7D%0A%0A  static BigInteger%5B%5D getRange(String str, BigInteger from, BigInteger to, BigInteger n)%7B%0A    //System.out.println(%22from = %22 + from + %22 to = %22 + to);%0A    int nRanges = str.length() + 1;%0A    BigInteger nPerRange = to.subtract(from).add(BigInteger.ONE).divide(BigInteger.valueOf(nRanges));%0A    for(int range = 0; range %3C nRanges; range++)%7B%0A      BigInteger low = from.add(nPerRange.multiply(BigInteger.valueOf(range)));%0A      BigInteger high = low.add(nPerRange).subtract(BigInteger.ONE);%0A      if(n.compareTo(low) %3E= 0 && n.compareTo(high) %3C= 0)%7B%0A        return new BigInteger%5B%5D%7BBigInteger.valueOf(range), low, high%7D;%0A      %7D%0A    %7D%0A    return null;%0A  %7D%0A%7D%0A

UVA 10460 Find permuted string