码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-05-23 10:25:56      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:个数   \n   rmi   space   ever   useful   algorithm   class   any   

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

  

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

标签:个数   \n   rmi   space   ever   useful   algorithm   class   any   

原文地址:https://www.cnblogs.com/vainglory/p/9075013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!