X-factor Chains(poj3421)(素数因子)

题解:就是给你一个正整数,然后你求出他的因字串从小到大排列的最大长度,以及这个最大长度的种类数

所以就找所有的素数因子,素数因子数即为最大长度数,然后就是,这几个因字数的全排列,即为种类数(注意有重复因子,这就用到数学知识了,需要去重)

Language:Default

X-factor Chains

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9320   Accepted: 2981

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

#include<stdio.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
ll mult(int a)
{
	ll d=1;
	for(int i=2;i<=a;i++)
	{
		d=d*i;
	}
	return d;
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
	    map<int,int>ma;
	    int t=n;
	    int i;
		for(i=2;i*i<=n;i++)
		{
			while(t%i==0&&t)
			{
				t=t/i;
				ma[i]++;
			}
		 } 
		 if(t>1) ma[t]++;
		map<int,int>::iterator iter;
		int len=0;
		for(iter=ma.begin();iter!=ma.end();iter++)
		{
			len+=iter->second;
		}
		ll ans=mult(len);
		for(iter=ma.begin();iter!=ma.end();iter++)
		{
			ans=ans/mult(iter->second);
	
		}
		printf("%d %lld\n",len,ans);
	}
	return 0;
}
 

猜你喜欢

转载自blog.csdn.net/qq_42434171/article/details/87940171