Linear prime sieve + mass factor decomposition + combination number.
AC found this to be a little less efficient. 766ms.
#include <stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>#include<iostream>#include<algorithm>#include<cmath>using namespaceStd;typedefLong LongLL;intTol; LL factor[ -];Const intN =2000001; LL Prime[n]= {0},num_prime =0;intIsnotprime[n] = {1,1};voidFINDFAC (Long LongN) { for(intI=0; i<num_prime; i++) { if(n%prime[i]!=0)Continue; while(1) { if(n==1|| n%prime[i]!=0) Break; Factor[tol++]=Prime[i]; N=n/Prime[i]; } if(n==1) Break; }}intnum[ -],tot;Long Longc[ -][ -];voidinit () {c[1][1]=1; for(intI=1; i<= -; i++) c[i][0]=1; for(intI=2; i<= -; i++) { for(intj=1; j<= -; J + +) {C[i][j]=c[i-1][j-1]+c[i-1][j]; } } for(Longi =2; i < N; i + +) { if(!Isnotprime[i]) prime[num_prime++]=i; for(Longj =0; J < num_prime && I * prime[j] < N; J + +) {Isnotprime[i* Prime[j]] =1; if( ! IPrime[j])) Break; } }}intMain () {init (); Long LongN; while(~SCANF ("%lld",&N)) {if(n==1) {printf ("%d%lld\n",1,1); Continue; } Tol=0; FINDFAC (n); Sort (Factor,factor+tol); Tot=0, num[tot]=1; for(intI=1; i<tol;i++) { if(factor[i]==factor[i-1]) num[tot]++; Else{tot++; Num[tot]=1; }} tot++; intsum=Tol; intans1=Tol; Long LongAns2=1; for(intI=0; i<tot;i++) {Ans2=ans2*C[sum][num[i]]; Sum=sum-Num[i]; } printf ("%d%lld\n", ANS1,ANS2); } return 0;}
POJ 3421 X-factor Chains