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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章