X Factor chain (number theory template)

Title: After a divisible can enter any positive integer satisfying X, factor demand is greater than 1 X composed strictly increasing before a maximum length sequence, and the sequence number that satisfies the maximum length.

               

Analysis: The maximum length is the number of seeking required quality factor and the factoring. And the length of the sequence number that satisfies (permutation problem).

         

Code: 

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
typedef long long LL;

const int N =(1<<20)+10;

int primes[N],cnt;
int minp[N]; //记录每个数的最小质因子
bool st[N];

//筛法求素数
void get_primes(int n)
{
    for(int i=2;i<=n;i++)
    {
        if(!st[i]) primes[cnt++]=i,minp[i]=i;
        
        for(int j=0;primes[j]*i<=n;j++)
        {
            st[primes[j]*i]=true;
            minp[primes[j]*i]=primes[j];
            if(i%primes[j]==0) break;
        }
    }
}


int main()
{
    get_primes(N-1);
    int x;
    int fact[30],sum[N];
    while (scanf("%d", &x) != -1)
    {
        //因式分解
        int k=0,tot=0;
        while(x>1)
        {
            int p=minp[x];
            
            fact[k]=p,sum[k]=0;
            while(x%p==0){
                x/=p;
                sum[k]++;
                tot++;
            }
            k++;
        }
        LL res=1;
        for(int i=1;i<=tot;i++)
        {
            res*=i;
        }
        for(int i=0;i<k;i++)
            for(int j=1;j<=sum[i];j++)
                res/=j;
       
        printf("%d %lld\n",tot,res);
    }
    
    return 0;
}

 

Published 22 original articles · won praise 7 · views 406

Guess you like

Origin blog.csdn.net/qq_40905284/article/details/104438749