POJ2992

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 263 - 1.

Sample Input

5 1
6 3
10 4

Sample Output

2
6
16
题意:给一个数n,k;求组合数C(n,k)的约数的个数。题目要求的是1000ms用求解约数个数的一般方法一定会超时。现在用数学中的积性函数的性质来解决这个问题。
解:
C(N,K)=N!/(K!(N-K)!) f(n)=f(a) f(b),a与b互质且a*b=n。设f(n)表示的是n约数的个数则C(N,K)约数的个数为f(N!)-f(K!)-f((N-K)!).
根据求解约数个数的公式:设正整数n的所有素因子分解n=p1^a1*p2^a2*p3^a3****Ps^as,那么
T(n)=(a1+1)(a2+1)(a3+1)*(an+1);(求因子的个数的公式)
如果n为N!则
ei=[N/pi^1]+ [N/pi^2]+ …… + [N/pi^n] 其中[]为取整,pi代表的是小于N的质数。
那么下面就可以求题目的解了。
AC代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define N 500
using namespace std;
typedef long long ll;
bool b[N];
int n,k,i,num[N],prime[N],top;
__int64 sum;
int main()
{
    top=0;///素数筛法
    b[0]=b[1]=false;
    b[2]=true;
    for(i=3; i<440; i++)
        if(i%2==0) b[i]=false;
        else b[i]=true;
    double t=sqrt(440*1.0);
    for(i=3; i<=t; i++)
    {
        if(b[i])
        {
            for(int j=i*i; j<440; j=j+i)
                b[j]=false;
        }
    }
    for(i=2;i<=440;i++)
    {
        if(b[i])
        {
            prime[top++]=i;
        }
    }///素数筛法
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        sum=1;
        memset(num,0,sizeof(num));
        int X=0,t,z;
        for(i=prime[0];i<=n;i=prime[++X])
        {
            t=n;
            z=i;
            while(t/z)///核心部分求对应的每一个ei
            {
                num[i]+=t/z;
                z*=i;
            }
        }
        X=0;
        for(i=prime[0];i<=n-k;i=prime[++X])
        {
            t=n-k;
            z=i;
            while(t/z)
            {
                num[i]-=t/z;
                z*=i;
            }
        }
        X=0;
        for(i=prime[0];i<=k;i=prime[++X])
        {
            t=k;
            z=i;
            while(t/z)
            {
                num[i]-=t/z;
                z*=i;
            }
        }
        for(int i=2; i<=n; i++)
        {
            if(num[i])
            {
                sum*=(num[i]+1);
            }
        }
        printf("%I64d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值