POJ3421 X-factor Chains 数学

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
 
    这题就是对X进行质因数分解,然后将1分别累乘它的质因数会得到一个序列,序列长度+1就是最终要求的最长数,至于有多少个这样长度的序列,实际上就是对质因数进行排列,因为存在相同的质因数。所以记录相同的质因数后,再去掉重复的排列法就可以了。
#ifndef HEAD
#include <stdio.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <iostream>
#include <queue>
#include <list>
#include <algorithm>
#include <stack>
#include <map>

using namespace std;
#endif // !HEAD

long long product(int i)
{
	long long res = 1;
	for (int c = 1; c <= i;c++)
	{
		res *= c;
	}
	return res;
}

int main()
{
#ifdef _DEBUG
	freopen("d:\\in.txt", "r", stdin);
#endif
	int a;

	map<int, int> mapPrimeNumber;
	while (scanf("%d\n", &a) != EOF)
	{
		mapPrimeNumber.clear();
		int countofprime = 0;
		for (int i = 2; i * i <= a; i++)
		{
			if (a % i == 0)
			{
				a /= i;
				if (mapPrimeNumber.find(i) != mapPrimeNumber.end())
				{
					mapPrimeNumber[i] ++;
				}
				else
					mapPrimeNumber[i] = 1;
				countofprime++;
				i--;
			}
			
		}
		if (mapPrimeNumber.find(a) != mapPrimeNumber.end())
		{
			mapPrimeNumber[a] ++;
		}
		else
			mapPrimeNumber[a] = 1;
		countofprime++;
		long long count = 1;
		for (int i = 1; i <= countofprime; i++)
		{
			count *= i;
		}
		for (map<int, int> ::iterator it = mapPrimeNumber.begin(); it != mapPrimeNumber.end();++it)
		{
			count /= product(it->second);
		}
		printf("%d %d\n", countofprime, count);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值