CF 396A On Number of Decompositions into Multipliers 解题报告(质因数分解+组合数计算)

A. On Number of Decompositions into Multipliers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an integer m as a product of integers a1, a2, ... an . Your task is to find the number of distinct decompositions of number m into the product of n ordered positive integers.

Decomposition into n products, given in the input, must also be considered in the answer. As the answer can be very large, print it modulo 1000000007 (109 + 7).

Input

The first line contains positive integer n (1 ≤ n ≤ 500). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

In a single line print a single number k — the number of distinct decompositions of number m into n ordered multipliers modulo1000000007 (109 + 7).

Sample test(s)
input
1
15
output
1
input
3
1 1 2
output
3
input
2
5 7
output
4
Note

In the second sample, the get a decomposition of number 2, you need any one number out of three to equal 2, and the rest to equal 1.

In the third sample, the possible ways of decomposing into ordered multipliers are [7,5], [5,7], [1,35], [35,1].

A decomposition of positive integer m into n ordered multipliers is a cortege of positive integers b = {b1, b2, ... bn} such that . Two decompositions b and c are considered different, if there exists index i such that bi ≠ ci.


    解题报告:昨晚CF上的第3题,当时没有做出来。今天想了很久,终于搞定了。

    题意就不说了。首先对m进行质因数分解,得到每个质因子的幂之和m,再将这m个幂分配给n个位子即可,将所有质因子的情况数相乘。

    如何将m个幂分配给n个位置呢?m的取值范围是(0, 30*500),n的取值范围是(0, 500]。

    用C[m][n]表示将m个幂分配给n个位置,那么显然,C[m][n] = sum(C[k][n-1]), 0<=k<=m。递推求和即可,时间复杂度大概是15000*500=750000,空间复杂度相同。代码如下:

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;

const int mod = 1000000007;

const int maxn = 10013;
int Hash[maxn];
int save[maxn];

int sum[503][15003];
int C[16000][501];

int& pos(int n)
{
	int s = n%maxn;
	while (Hash[s] && save[s] != n)
		s = (s + 1) % maxn;
	save[s] = n;
	return Hash[s];
}

void add(int n, int num)
{
	pos(n) += num;
}

void calC()
{
	C[0][0] = 1;
	for (int i = 1; i < 16000; i++)
	{
		for (int j = 0; j <= 500; j++)
		{
			if (j == 0)
				C[i][j] = 1;
			else
				C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
		}
	}
}

int main()
{
#ifdef ACM
	freopen("in.txt", "r", stdin);
#endif
	map<int, int> table;
	map<int, int>::iterator it;

	calC();

	int n;
	scanf("%d", &n);

	for (int i = 0; i < n; i++)
	{
		int a;
		scanf("%d", &a);
		table[a]++;
	}

	for (it = table.begin(); it != table.end(); it++)
	{
		int a = it->first;
		int b = it->second;

		for (int i = 2; i*i <= a; i++)
		{
			int t = 0;
			while (a%i == 0)
			{
				a /= i;
				t++;
			}
			add(i, t * b);
		}

		if (a != 1)
			add(a, b);
	}

	int res = 1;
	for (int i = 0; i < maxn; i++) if (Hash[i])
		res = ((long long)res * C[n + Hash[i] - 1][n - 1]) % mod;

	printf("%d\n", res);
}

    可以总结出m个幂分配到n个位置就是求组合数C(n+m-1, n-1)。n - 1 < 500,可以使用公式,直接分解成质因子的幂的形式进行计算。空间复杂度大大降低,时间复杂度大致相同。代码如下:
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;

const int mod = 1000000007;

const int maxn = 10013;
int Hash[maxn];
int save[maxn];

int prime[160000];

void add(int n, int num)
{
	int s = n%maxn;
	while (Hash[s] && save[s] != n)
		s = (s + 1) % maxn;
	save[s] = n;
	Hash[s] += num;
}

int powMod(int a, int b)
{
	a %= mod;
	int res = 1;
	while (b)
	{
		if (b & 1)
			res = ((long long)res*a) % mod;
		b >>= 1;
		a = ((long long)a*a) % mod;
	}
	return res;
}

void decomposition(int a, int step)
{
	for (int i = 2; i*i <= a; i++)
	{
		int t = 0;
		while (a%i == 0)
		{
			a /= i;
			t++;
		}
		if (t)
		{
			prime[i] += t*step;
		}
	}
	if (a != 1)
	{
		prime[a] += step;
	}
}

int calC(int n, int m)
{
	memset(prime, 0, sizeof(prime));
	for (int i = 0; i < m; i++)
		decomposition(n - i, 1);
	for (int i = 2; i <= m; i++)
		decomposition(i, -1);

	int res = 1;
	for (int i = 2; i <= 16000; i++) if (prime[i])
	{
		res = ((long long)res * powMod(i, prime[i])) % mod;
	}
	return res;
}

int main()
{
#ifdef ACM
	freopen("in.txt", "r", stdin);
#endif

	map<int, int> table;
	map<int, int>::iterator it;

	int n;
	scanf("%d", &n);

	for (int i = 0; i < n; i++)
	{
		int a;
		scanf("%d", &a);
		table[a]++;
	}

	for (it = table.begin(); it != table.end(); it++)
	{
		int a = it->first;
		int b = it->second;

		for (int i = 2; i*i <= a; i++)
		{
			int t = 0;
			while (a%i == 0)
			{
				a /= i;
				t++;
			}
			add(i, t * b);
		}

		if (a != 1)
			add(a, b);
	}

	int res = 1;
	for (int i = 0; i < maxn; i++) if (Hash[i])
		res = ((long long)res * calC(n + Hash[i] - 1, n - 1)) % mod;

	printf("%d\n", res);
}

你可能感兴趣的:(质因子分解,组合数计算)