POJ 3421 X-factor Chains(数论)(筛法)()

X-factor Chains
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6370 Accepted: 1973

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

题意:给你一个长度为m的整数序列,即1 = X0X1X2, …, Xm = X

这个序列满足条件:Xi < Xi+1 and Xi | Xi+1 where a | b means a perfectly divides into b.每一个是前一个的倍数。(都是X的因子)

让你求最大长度,和最多有多少中这样长度的序列。

思路:首先筛法求素数;然后计算每个不同因子的指数和,即总因子数,就是最大长度了。

然后数量=幂和的阶乘/各个幂阶乘的和

已经AC大笑,心中痛苦啊,因为   const int N = 1100005;//此处的数据要重视,本人wa了n次啊,大于1100005,小于10^7。哭


#include<iostream>//poj 3421
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int N = 1100005;//此处的数据要重视,本人wa了n次啊
const int M = 10005;

int num,n,index;
int prime[M],flag[N],sum[M]; //存每个素因子的指数,即个数

void Init()//筛法求素数
{
    for(int i=2;i<=N;i++)
    {
        if(flag[i])
            continue;
        prime[num++]=i;
        for(int j=2;i*j<=N;j++)
            flag[i*j]=1;
    }
}

int main()
{
    Init();
    while(scanf("%d",&n)!=EOF)
    {
        index=0;//又一次放错了位置,哭死了
        memset(sum,0,sizeof(sum));//初始化

        for(int i=0;i<num;i++)
        {
            if(n%prime[i]==0)//找到所有因子
            {
                while(n%prime[i]==0)
                {
                    sum[index]++;//计算每个因子的指数和
                    n/=prime[i];
                }
                index++;//别放在大括号外面了
            }
            if(n==1)
                break;
        }
        //求所有因子的指数和
        ll s=0,ans=1;
        for(int i=0;i<index;i++)
        s+=sum[i];

        //求
        for(ll i=2;i<=s;i++)//次数使用int也可以
            ans*=i;
        for(int i=0;i<index;i++)
        {
            for(int j=2;j<=sum[i];j++)
            {
                ans/=j;
            }
        }
        printf("%lld %lld\n",s,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值