POJ 3421 X-factor Chains

Source: Internet
Author: User

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

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.