zoukankan      html  css  js  c++  java
  • POJ 3421分解质因数

    X-factor Chains
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7375   Accepted: 2340

    Description

    Given a positive integer X, an X-factor chain of length m is a sequence of integers,

    1 = X0X1X2, …, Xm = X

    satisfying

    Xi < Xi+1 and Xi | Xi+1 where a | b means a perfectly divides into b.

    Now we are interested in the maximum length of X-factor chains and the number of chains of such length.

    Input

    The input consists of several test cases. Each contains a positive integer X (X ≤ 220).

    Output

    For each test case, output the maximum length and the number of such X-factors chains.

    Sample Input

    2
    3
    4
    10
    100

    Sample Output

    1 1
    1 1
    2 1
    2 2
    4 6

    Source

    题意:
    1 = X0X1X2, …, Xm = X,X0~Xm都是X的因子并且递增,给出X求出最长的链,有几条最长的链。
    代码:
    //最长链就是X的素因子的个数,数量就是这些素因子的排列组合(重复的只算一个) 
    //(全部质因子个数的阶乘)/(每个质因子个数的阶乘)
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int MAXN = 2000000;
    int prime[MAXN+1];
    void getPrime()
    {
        memset(prime,0,sizeof(prime));
        for(int i = 2;i <= MAXN;i++)
        {
            if(!prime[i])prime[++prime[0]] = i;
            for(int j = 1;j <= prime[0] && prime[j] <= MAXN/i;j++)
            {
                prime[prime[j]*i] = 1;
                if(i % prime[j] == 0)break;
            }
        }
    }
    int factor[100][2];//factor[i][0]存素因子,factor[i][1]存素因子的个数
    int fatCnt;//不重复的素因子个数
    int getFactors(long long x)
    {
        fatCnt = 0;
        long long tmp = x;
        for(int i = 1; prime[i] <= tmp/prime[i];i++)
        {
            factor[fatCnt][1] = 0;
            if(tmp % prime[i] == 0 )
            {
                factor[fatCnt][0] = prime[i];
                while(tmp % prime[i] == 0)
                {
                    factor[fatCnt][1] ++;
                    tmp /= prime[i];
                }
                fatCnt++;
            }
        }
        if(tmp != 1)
        {
            factor[fatCnt][0] = tmp;
            factor[fatCnt++][1] = 1;
        }
        return fatCnt;
    }
    ll jc(int x){
        ll s=1;
        for(int i=2;i<=x;i++)
            s*=i;
        return s;
    }
    int main()
    { 
        getPrime();
        int x;
        while(scanf("%d",&x)==1){
            getFactors(x);
            int ans1=0;
            ll tmp=1;
            for(int i=0;i<fatCnt;i++){
                //cout<<factor[i][0]<<" "<<factor[i][1]<<endl;
                ans1+=factor[i][1];
                tmp*=jc(factor[i][1]);
            }
            printf("%d %lld
    ",ans1,jc(ans1)/tmp);
        }
        return 0;
    }
  • 相关阅读:
    chrome浏览器postman 插件安装
    使用poi解决导出excel内下拉框枚举项较多的问题
    Nginx(三)------nginx 反向代理
    webpack 内存溢出 Allocation failed
    Postman 安装及使用入门教程 (谷歌浏览器插件版)
    jquery 控制 video 视频播放和暂停
    百度编辑器ueditor 光标位置的坐标
    mkdocs 生成帮助文档
    js 日期 相关
    vue-cli3 第三版安装搭建项目
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/7226965.html
Copyright © 2011-2022 走看看