X-factor Chains(素数)

原题链接X-factor Chains
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6575 Accepted: 2054
Description

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

1 = X0, X1, X2, …, 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

POJ Monthly–2007.10.06, ailyanlu@zsu

//http://poj.org/problem?id=3421
#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = (~(0ULL)>>1);
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=10000 + 5;
int n;
vector<int> res;
//得到一个数的阶乘
ull f(int x){
        ull ans=1;
        for(int i=2;i<=x;i++)
                ans*=i;
        return ans;
}
//得到一个数的所有质因数的幂
void solve(int x){
        for(int i=2;i*i<=x;i++){
                int t=0;//因为从低位开始,所以一定所有的进入循环的i都是质因子
                while(x%i==0) {t++;x/=i;}
                res.push_back(t);
        }
        if(x!=1) res.push_back(1);
        return;
}
int main(){
        while(scanf("%d",&n)==1){
                res.clear();
                solve(n);
                int len=res.size();
                int sum=0;//sum代表所有质因子的幂的和
                for(int i=0;i<len;i++)
                        sum+=res[i];
                ull ans=f(sum);//得到sum的阶乘
                for(int i=0;i<len;i++)
                        ans/=f(res[i]);//除以其中一个质因数的幂的阶乘
                cout << sum << " " << ans << endl;
        }
        return 0;
}
/*
因子链:将一个数X分解成从1到X的数列,前一个数可以整除后一个数,求最大链长和链的个数。
因为题目里要求Xi | Xi+1,而Xm又限定为X,所以我们可以想到Xm-1是X除以其某个约数得到的,Xm-1也是可以由Xm-2的到的,可以认为这些单位之间就是插入了一个乘数。
由此我们可以知道“X-factor Chains”是通过不断乘以X的约数得到的,为了长度最大,所以约数必须是素数。通过记录有哪些素因数,以及素因子的数量,我们就可以得到链的长度。
而长度最大的链的数量则是这些数的排列数,公式为素因子个数之和的阶乘/每个素因子个数的阶乘。
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门豪杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值