Poj3421 X-Factor chains (full arrangement of repeated elements)

Source: Internet
Author: User

Poj3421 X-Factor chains

Given a positive integer $ x (x <= 2 ^ {20}) $, evaluate the maximum length of the sequence in which $ x $ is composed of factors that satisfy any of the preceding conditions, and the number of subsequences that meet the maximum length.

Obviously, the maximum length is the number of prime factor values of $ x $ (add duck removal one by one)

The number of subsequences that meet the maximum length ....

Isn't that the full arrangement of repeated elements!

There is such a formula that sets the total number of elements $ N $, and the number of each repeated element $ M _ {I }$, a total of $ K $ different elements

Then the number of fully arranged instances $ =\ frac {n !} {\ Prod _ {I = 1} ^ {k} M _ {I }!} $

Found $ n! (N <= 20) $ is in the range of $ Longlong $ and can be processed directly.

A prime screening screen is OK.

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define re register 5 using namespace std; 6 #define N 1048577 7 int n,v[N],pri[N],cct,cnt; 8 long long fac[23],ans,tmp; 9 int main(){10     for(int i=2;i<N;++i){11         if(!v[i]) v[i]=pri[++cct]=i;12         for(int j=1;j<=cct;++j){13             if(pri[j]>i||pri[j]*i>=N) break;14             v[pri[j]*i]=pri[j];15         }16     }fac[0]=1;17     for(int i=1;i<=20;++i) fac[i]=fac[i-1]*i;18     while(cin>>n){19         cnt=0;tmp=1;20         for(int i=1,j=n;i<=cct&&j>1&&pri[i]<=n;++i){21             int a=0;22             while(j%pri[i]==0&&j>1) ++a,++cnt,j/=pri[i];23             tmp*=fac[a];24         }ans=fac[cnt]/tmp;25         cout<<cnt<<" "<<ans<<endl;26     }return 0;27 }
View code

 

Poj3421 X-Factor chains (full arrangement of repeated elements)

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.