- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- ll permute[26];
- string s;
- ll nth;
- int a[26];
- void print(){
- for(int i = 0; i <26; i++) cout<<a[i]<<" ";
- }
- void per(){
- permute[0] = 1;
- for(int i = 1; i <26; i++) permute[i] = permute[i-1]*i;
- }
- ll possible(int n){
- ll ans = permute[n];
- for(int i = 0; i <26; i++) ans/= permute[a[i]];
- return ans;
- }
- string solve(int sz){
- string t;
- while(sz){
- ll upto = 0;
- bool found = false;
- for(int i = 0; i <26 && !found; i++){
- if(a[i]== 0)continue;
- a[i]--;
- ll now = possible(sz-1);
- //acout<<now<<" now upto "<<upto<<" nth "<<nth<<endl;
- if(now+upto>=nth){
- nth-= upto;
- t+= (char)('a'+i);
- found = true;
- sz--;
- }
- else{
- upto+=now;
- a[i]++;
- }
- }
- if(!found)break;
- }
- return t;
- }
- int main(){
- int ts;
- cin>>ts;
- per();
- for(int i = 1; i <=ts; i++){
- memset(a,0, sizeof(a));
- cin>>s;
- cin>>nth;
- for(int i = 0; i <s.size(); i++)a[s[i]-'a']++;
- if(nth>possible(s.size())) cout<<"Case "<<i<<": "<<"Impossible"<<endl;
- else cout<<"Case "<<i<<": "<<solve(s.size())<<endl;
- }
- return 0;
- }
Thursday, 27 July 2017
Lightoj 1060 - nth Permutation
Subscribe to:
Post Comments (Atom)
Most Featured Post
Lightoj 1159 - Batman
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
-
http://lightoj.com/volume_showproblem.php?problem=1382 Problem analysis: This is a rare problem i wrote about so far. After much strugg...
-
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
No comments:
Post a Comment