-
Notifications
You must be signed in to change notification settings - Fork 0
/
FUNNUMS.cpp
90 lines (87 loc) · 1.84 KB
/
FUNNUMS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// template by Utkarsh trivedi
// problem id- FUNNUMS Based on- MATH REASONING PERMUTATION
#include<stdio.h>
#define s(x) scanf("%d",&x)
#define p(x) printf("%d",x)
#define sl(x) scanf("%lld",&x)
#define pl(x) printf("%lld",x)
#define nl printf("\n")
#define chk printf("hello ")
typedef long long ll;
typedef unsigned long long ull;
#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
unsigned long long fact[16];
ll rank(char* str)
{
int i,j,l=strlen(str),count;
ll sum=0;
char c;
for(i=0;i<l;i++)
{
c=str[i]; count=0;
for(j=i+1;j<l;j++)
{
if(c>str[j])
{ count++;}
}
sum+=(count)*fact[l-i-1];
}
return (sum+1);
}
int main()
{
char str[20];
ll add,k;
string:: iterator p;
int t,l,i;
s(t);
fact[0]=1;
for(i=1;i<=15;i++)
fact[i]=i*fact[i-1];
while(t--)
{
cin>>str>>add;
l=strlen(str);
if(add==0) { cout<<str<<endl; continue; }
if(l==1) { cout<<str<<endl; continue; }
if(l==2) { if(add==1) cout<<str[1]<<str[0]<<endl; continue; }
add=rank(str)+add;
// cout<<add<<endl;
sort(str,str+l);
string str1(str);
while(1)
{
k=add/fact[l-1];
if(add%fact[l-1]==0) k--; // special case when add is divisible it gives one more to required.
cout<<str1[k];
p=str1.begin()+k;
str1.erase(p);
add=add%fact[l-1];
l--;
/* cout<<"(";
for(i=0;i<l;i++) cout<<str1[i];
cout<<")";*/
if(l==2)
{
if(add==1) cout<<str1[0]<<str1[1];
else if(add==0) cout<<str1[1]<<str1[0];
break;
}
if(add==0) // special case when number is last in its part as 3421(after 3), 625431(after 2) so direct reverse because add become 0
{
for(i=l-1;i>=0;i--)
cout<<str1[i];
//cout<<endl;
break;
}
}
nl;
}
}