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)