POJ 2992 Divisors (因子个数)

28 篇文章 0 订阅
Divisors
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10784 Accepted: 3193

Description

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

① 由素数基本定理可知,一个数n可以唯一地表示成素数的乘积:n=(p1^a1)*(p2^a2)*(p3^a3)......*(pk^ak),其中pi是素数
② 因数个数:   若n分解成 n=(p1^a1)*(p2^a2)*(p3^a3)......*(pk^ak),
则n的因子个数为:(a1+1)*(a2+1)*(a3+1)....*(ak+1)。    (理解:pi可以取0个,1个,2个,,,ai个,所以共ai+1种取法,由乘法原理,故全部的因子的个数是全部情况的乘积)
③ n的阶乘n!素数分解中,素数p的指数由下面的公式给出:[n/p]+[n/p^2] + [n/p^3] + ......
④ C(n,k)=n!/(k!*(n-k)!)  ,  故组合数的分解转换成了对阶乘的分解。
设ans=C(n,k), p为素数,假如n!的分解中p的次数是a,k!的分解中p的次数是b,(n-k)!的分解中p的次数是c,那么ans的分解中p的次数是:a-(b+c).

至此题目基本上就解出来了,另外就是要用线性筛法预处理出素数,不然会超时。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
typedef __int64 ll;
const int maxn=431;
int pri[maxn],tot;
bool is[maxn+5];

void prime_table()
{
	int i,j;
	memset(is,0,sizeof(is));
	is[0]=is[1]=1;
	for(i=2;i<=maxn;i++){
		if(!is[i])
			pri[tot++]=i;
			for(j=0;j<tot && i*pri[j]<=maxn;j++){
				is[i*pri[j]]=1;
				if(!(i%pri[j])) break;
			}
		
	}
}

int call(int n, int p)
{
    if (n < p)
        return 0;
    return n / p + call(n / p, p);
}

int a[3][maxn+5];

void cal(int n,int v)
{
	int i,j;
	memset(a[v],0,sizeof(a[v]));
	for(i=0;i<tot;i++){
		if(pri[i]>n) break;		
		a[v][pri[i]]=call(n,pri[i]);
	}
}

int main()
{
	int i,n,k;
	ll ans;
	tot=0;
	prime_table();
	while(scanf("%d%d",&n,&k)!=EOF){	
		cal(n,0);
		cal(k,1);
		cal(n-k,2);
		ans=1;
		for(i=2;i<=maxn;i++)
			ans*=(a[0][i]-a[1][i]-a[2][i]+1);
		printf("%I64d\n",ans);
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值