Advertisement
erfanul007

UVa 941

Dec 5th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. string str;
  7. long long int i,j,t,n,ln,tln,div,x,y;
  8. long long int fac[30];
  9. fac[0]=1;
  10. for(j=1;j<=20;j++)
  11. fac[j]=fac[j-1]*j;
  12. cin>>t;
  13. while(t--){
  14. cin>>str;
  15. cin>>n;
  16. ln=str.size();
  17. for(i=0;i<ln;i++) {
  18. sort(str.begin() + i, str.end());
  19. if (n==0)
  20. break;
  21. x = i + n/fac[ln-i-1];
  22. swap(str[i], str[x]);
  23. n = n % fac[ln-i-1];
  24. }
  25. cout<<str<<endl;
  26. }
  27. return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement