HACKER RANK Project Euler 24 - Lexicographic Permutations - Solution.'C++'
Project Euler Challenges-24 - Lexicographic Permutations Solution
Problem Statement
This problem is a programming version of Problem 24 from projecteuler.net
A permutation is an ordered arrangement of objects. For example, dabc is one possible permutation of the word abcd . If all of the permutations are listed alphabetically, we call it lexicographic order. The lexicographic permutations of abc are:
abc acb bac bca cab cba
What is the Nth lexicographic permutation of the word abcdefghijklm ?
Input Format
The first line contains an integer T , i.e., number of test cases.
Next T lines will contain an integer N .
Output Format
Print the values corresponding to each test case.
Constraints
1≤T≤1000
1≤N≤13!
Sample Input
2
1
2
Sample Output
abcdefghijklm
abcdefghijkml
Problem Statement
This problem is a programming version of Problem 24 from projecteuler.net
A permutation is an ordered arrangement of objects. For example, dabc is one possible permutation of the word abcd . If all of the permutations are listed alphabetically, we call it lexicographic order. The lexicographic permutations of abc are:
What is the Nth lexicographic permutation of the word abcdefghijklm ?
Input Format
The first line contains an integerT , i.e., number of test cases.
NextT lines will contain an integer N .
The first line contains an integer
Next
Output Format
Print the values corresponding to each test case.
Print the values corresponding to each test case.
Constraints
1≤T≤1000
1≤N≤13!
Sample Input
2
1
2
Sample Output
abcdefghijklm
abcdefghijkml
Solution:
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(){
string v="abcdefghijklm";
int T;
long long _o=1;for(int i=1;i<=v.size();i++)_o*=i;
long long i=1000000,j;
for(scanf("%d",&T);T--;){
scanf("%lld",&i);
i-=1;
long long o=_o;
string V=v,a;
for(;V.size();){
//j=i%o/(o=o/V.size()); ///(o=o+3>>2);
j=i%o,o/=V.size(),j/=o;
a+=V[j];
V.erase(V.begin()+j);
}
puts(a.c_str());
}
}
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(){
string v="abcdefghijklm";
int T;
long long _o=1;for(int i=1;i<=v.size();i++)_o*=i;
long long i=1000000,j;
for(scanf("%d",&T);T--;){
scanf("%lld",&i);
i-=1;
long long o=_o;
string V=v,a;
for(;V.size();){
//j=i%o/(o=o/V.size()); ///(o=o+3>>2);
j=i%o,o/=V.size(),j/=o;
a+=V[j];
V.erase(V.begin()+j);
}
puts(a.c_str());
}
}
Comments
Post a Comment