POJ3421 X-factor Chains
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
题意:
给出一个数字 n ,问 n 的因子组成的满足任意前一项都能整除后一项的序列的最大长度,以及所有不同序列的个数
题解:
分解成素因子后所有素因子的幂和是第一个答案,幂和的阶乘/(各幂的阶乘之积)是第二个答案。
引理一:质因数分解
此处幂为底数的个数,想得到更长的序列数,则需要使底数更小
例如n=100
100 用2分解100-> 2*2*25 得到了2个因数:2 2*2
用5分解25-> 2*2*5*5 得到了2个因数:2*2*5 2*2*5*5
此处运用埃氏筛法的思想,分解到sqrt(n)即可
引理二:有n个物体,共k类,第ki类物品数量为ni,则其全排列数为:
证明:先在n个空位中放入k1类共C(n,n1)种方法
再在n-n1个空位中放入k2类共C(n-n1,n2)种方法
.
.
.
最后一类 共C(n-n1-….-n(k-1),nl)种方法
用乘法法则:
CODE
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
#define maxn 2000
#define LL long long
LL f(LL a)
{
LL sum = 1;
for (int i = 2; i <= a; ++i)
sum *= i;
return sum;
}
int main()
{
/*
long long sum=2;
for(int i=1;i<=20;i++)
sum*=2;
sum=sqrt(sum);
cout << sum << endl;
*/
int n;
while(cin >> n)
{
LL Hash[maxn];
memset(Hash,0,sizeof(Hash));
int ans1 = 0;
for (int i = 2; i*i<=n; ++i)
while(n % i == 0)
{
Hash[i]++;
n /= i;
ans1++;
cout << i << endl;
}
if (n != 1)
ans1++;
LL ans2 = f(ans1);
for (int i = 2; i < maxn; ++i)
if (Hash[i])
ans2 /= f(Hash[i]);
printf("%d %lld\n", ans1 == 0 ? 1 : ans1, ans2);
}
return 0;
}
上一篇: AndroidX介绍及项目迁移
下一篇: 手机建站注意事项:内容为王
推荐阅读
-
【 HDU - 4460 】 G - Friend Chains (BFS)
-
11gv$wait_chains与hanganalyze_MySQL
-
【11g】Job Chains: Chains操作(Enabling、Dropping 、Running 、Disabling 、Pausing 、Stopping 、Running )
-
POJ 2248 Addition Chains 迭代加深
-
又是latch: cache buffers chains惹得祸
-
POJ3421 X-factor Chains
-
UVA529 Addition Chains (IDA*)
-
Oracle 根据v$wait_chains找到造成等待的SQL
-
Oracle 根据v$wait_chains找到造成等待的SQL
-
11gv$wait_chains与hanganalyze_MySQL