URAL - 1055 Combinations

Combinations

As you have known MMM corporation lab researches the matter of haricot proportions in soup For every day. As we wrote in the previous problem (T) the ladle is placed down into the soup pan. But now we are not interested in the form and linear sizes of the ladle. This time the ladle holds exactly M haricot seeds of N got into the pan. All the seeds are of different size.

Experimenters calculate the quantity of possible methods to proportion M seeds in the pan. Requisite quantity of methods is calculated with the formula: C = N!/( M!·( N− M)!). The main feature of these experiments is the quantity of different prime divisors of number C.

Example.
N = 7, M = 3. C = 7!/(3!4!) = 5040/(624) = 35 = 5*7.
This example shows that the quantity of different prime divisors is 2.

Lest money would be spent for programmer, MMM corporation board decided to make necessary estimating during trial tour of quarterfinal world programming contest in Rybinsk.

Problem

Thus, your aim is to find the quantity of different prime divisors of number C.

Input
contains integers N and M. You may assume that 1 ≤ M < N ≤ 50000.

Output

should contain one integer.

Example

input
7 3
ourput
2

题意

计算组合数 Cm/n可以拆分成不同的 素数的种类数目
例如,C10/3 = 10! / 7!*3! = 120 = 2 * 2 * 2 * 3 * 5为三种。

题解
(1)计算n-m+1 到 n 每个数可以拆分的素因子数目,
(2)减去1 到 n 每个数可以拆分的素因子数目。
(3)如果,该因子数量大于0,则ans++。

原理:
每个数可以用素数表示,这样(1)可以记录n!/(n-m)!素因子中的个数。
同理(2)操作减去素因子。

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX = 5e4 + 5;

int pri[MAX];
bool is_pri[MAX] = {1, 1, 0};
int cnt = 0;

void prime() {
	for (int i = 2; i <= MAX; i++) {
		if(is_pri[i]) continue;
		pri[cnt++] = i;
		for(int j = i; j <= MAX; j += i) 
			is_pri[j] = 1;
	}	 
}

int a[MAX];

int main() {
	prime();
	int n, m;
	while(scanf("%d%d", &n, &m) != EOF) {
		memset(a, 0, sizeof(a));
		for(int i = n - m + 1; i <= n; i++) {
			if(!is_pri[i]) a[i]++;
			else {
				int temp = i;
				for(int k = 0; pri[k] <= temp;) {
					if(temp % pri[k] == 0) {
						a[pri[k]]++;
						temp = temp / pri[k];
					}
					else k++;
				}
			}
		}
		
		for (int i = 2; i <= m; i++) {
			if(!is_pri[i] && a[i] > 0) a[i]--;
			else {
				int temp = i;
				for(int k = 0; pri[k] <= temp;) {
					if(temp % pri[k] == 0 && a[pri[k]] > 0) {
						a[pri[k]]--;
						temp = temp / pri[k];
					}
					else k++;
				}
			}
		}

		int ans = 0;
		for (int i = 2; i <= n; i++) {
			if(a[i] > 0) 
				ans++;
			//cout << i << " " << a[i] << endl;
		}		
		printf("%d\n", ans);
	}
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值