poj2992 divisors 求组合数的约数个数,阶乘的质因数分解

Your task in this problem is to determine the number of divisors of  Cnk. Just for fun -- or do you need any special reason for such a useful computation?

Input

The input consists of several instances. Each instance consists of a single line containing two integers n and k (0 ≤ k ≤ n ≤ 431), separated by a single space.

Output

For each instance, output a line containing exactly one integer -- the number of distinct divisors of  Cnk. For the input instances, this number does not exceed 2  63 - 1.

Sample Input

5 1
6 3
10 4

Sample Output

2
6
16
思路:计算约数个数的时候,可以类比数的唯一分解求约数个数;
s=2^p1*3^p2*5^p3*-----
num=(p1+1)*(p2+1)*(p3+1)*....
如果求分数类型的约数个数...
可以分解质因数之后在上面的个数-下面的个数
即s__=2^(p1-p1_)*3^(p2-p2_)*----
num=(p1-p1_+1)*(p2-p2_+1)*(p3-p3_+1)*---
阶乘的质因数分解:
  int cal(int n,int p) if(n<p)return 0;else return n/p+cal(n/p,p);
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int prime[500];bool vis[500];
typedef long long ll;int cnt;
void init(){
	for(int i=2;i<=431;i++){
		if(!vis[i]) for(int j=i*2;j<=431;j+=i) vis[j]=1;
	}
	for(int i=2;i<=431;i++){
		if(!vis[i]) prime[++cnt]=i;
	}
}
int num1[500],num2[500];
int cal(int n,int p){
	if(n<p) return 0;
	else return n/p+cal(n/p,p);
}
int main(){
	init();int n,m;
	//std::ios::sync_with_stdio(false);cin.tie(0);
	//for(int i=1;i<=83;i++) cout<<prime[i]<<endl;
	while(~scanf("%d%d",&n,&m)){
		ll ans=1;
		memset(num1,0,sizeof(num1));
		memset(num2,0,sizeof(num2));
		for(int i=1;prime[i]<=n&&i<=cnt;i++){
			num1[i]=cal(n,prime[i]);
			num2[i]=cal(m,prime[i])+cal(n-m,prime[i]);
			ans*=(num1[i]-num2[i]+1);
			//cout<<ans<<endl;		
		}
		printf("%lld\n",ans);
	}
	return 0;
} 

  

转载于:https://www.cnblogs.com/vainglory/p/9075013.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值