December 10th 模拟赛C T3 X-因子链 Solution

题目空降

Description

给一个正整数X,一个长度为m的X-因子链是指这样一个序列: X0=1X1X2Xm=X 满足: Xi<Xi+1 同时 Xi+1 能被 Xi 整除

要求X-因子链的最大长度Len和长度为Len的X-因子链的数量。

Input

一个正整数 X(X<231)

Output

一行,两个整数,分别表示最大长度和该长度链的种数。

Analysis

由于要求Len最长,且为递增序列,所以分解质因数求全排列。

Implementation

Sum 表示质因子个数。
Xi 表示第i种质因子的个数。
Ans=Sum!÷X1!÷X2!÷÷Xn!

Tips

可以利用实数类型先除后乘,避免打高精除。

Code

C++

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

int x, d[40], sum;

int main(int argc, char const *argv[])
{
    freopen("init.in", "r", stdin);
    //freopen("factor.out","w",stdout);
    scanf("%d", &x);
    for (int i = 2; i <= sqrt(x); i++)
    {
        if (x % i == 0)
            d[0]++;
        while (x % i == 0)
        {
            x /= i;
            sum++;
            d[d[0]]++;
        }
    }
    if (x != 1)
    {
        sum++;
        d[++d[0]]++;
    }
    double ans = 1;
    for (int i = 1; i <= d[0]; i++)
        for (int j = 1; j <= d[i]; j++)
            ans /= j;
    for (int i = 1; i <= sum; i++)
        ans *= i;
    printf("%d %.0lf\n", sum, ans);
    return 0;
}

Pascal

uses
    math;

var 
    i,j,k,x,sum:longint;
    d:array [0..40] of longint;
    ans:double;

begin
    assign(input,'init.in');reset(input);
    //assign(output,'factor.out');rewrite(output);
    readln(x);
    for i:=2 to trunc(sqrt(x)) do 
    begin
        if x mod i=0 then
        begin
            inc(d[0]);
        end;
        while x mod i=0 do
        begin
            x:=x div i;
            inc(sum);
            inc(d[d[0]]);
        end;
    end;
    if x<>1 then
    begin
        inc(sum);
        inc(d[0]);
        inc(d[d[0]]);
    end;
    ans:=1;
    for i:=1 to d[0] do
        for j:=1 to d[i] do
            ans:=ans/j;
    for i:=1 to sum do
        ans:=ans*i;
    writeln(sum,' ',ans:0:0);
end.

猜你喜欢

转载自blog.csdn.net/ace_killing/article/details/53559318