Nth Permutation LightOJ, permutationlightoj

Source: Internet
Author: User

Nth Permutation LightOJ, permutationlightoj

Nth Permutation LightOJ-1060

Question: Given a string consisting of lower-case letters, all the letters in the string are arranged (the arrangement of the combination). All Generated letters are sorted in the lexicographically sorted order.

Method: generate by bit.

First, calculate the total number of columns that can be formed. If it is less than n, it is Impossible.

Otherwise, from the first place, each digit must be listed in the range of all the remaining letters from small to large. Here is a formula that uses the number of sorted distinct sets (the factorial of the total number/the product of the number of factorial of each element) calculate the number of positions after this digit that can be formed with the remaining letters, and then add this value to the number of the current column. If the number of the current column is greater than n after a certain addition, it indicates that the current bit is the letter listed in the current enumeration. Then, remove the added value and start enumerating the next bit.

Number of errors: 2

Cause of error: I want to use brute force (generated one by one ).

 1 #include<cstdio> 2 #include<cstring> 3 typedef long long LL; 4 LL fac[]={1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000,20922789888000,355687428096000,6402373705728000,121645100408832000,2432902008176640000}; 5 LL T,TT,x,len; 6 char s[100]; 7 LL num[30],arr[30]; 8 LL get_num() 9 {10     LL i,a1=0,b1=1;11     for(i=0;i<26;i++)12         a1+=num[i],b1*=fac[num[i]];13     return fac[a1]/b1;14 }15 int main()16 {17     LL i,j,t1,now;18     scanf("%lld",&T);19     for(TT=1;TT<=T;TT++)20     {21         scanf("%s%lld",s+1,&x);22         len=strlen(s+1);23         memset(num,0,sizeof(num));24         for(i=1;i<=len;i++)25             num[s[i]-'a']++;26         now=0;27         t1=get_num();28         printf("Case %lld: ",TT);29         if(t1<x)30         {31             puts("Impossible");32             continue;33         }34         for(i=1;i<=len;i++)35         {36             for(j=0;j<26;j++)37                 if(num[j])38                 {39                     num[j]--;40                     t1=get_num();41                     if(now+t1>=x)42                     {43                         arr[i]=j;44                         break;45                     }46                     num[j]++;47                     now+=t1;48                 }49         }50         for(i=1;i<=len;i++)51             putchar(arr[i]+'a');52         puts("");53     }54 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.