POJ2992 Divisors 组合数,分解质因数

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


这道题是求组合数的约数个数,利用c(n,k)=(n-k+1)/k*c(n,k-1) 进行素因子的递推即可,不需要高精度。


#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
#include <map>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
#define LL long long
#define maxn 1001
int a[maxn];
LL f[maxn][maxn];
int prime[100],c;
int v[maxn];
void p()
{
    int i,j,n=maxn,m;
    c=0;
    m=(LL)sqrt(n+0.5);
    memset(v,0,sizeof(v));
    for(i=2;i<=m;i++)
        if(!v[i]){
            for(j=i*i;j<=n;j+=i)
                v[j]=1;
        }
    for(j=2;j<=n;j++){
        if(!v[j]){
            prime[c++]=j;
        }
    }
}

void ad(int n,int flag)
{
    if(flag==1){
        for(int i=0;i<c&&n>1;i++){
            while(n%prime[i]==0){
                n/=prime[i];
                a[i]++;
            }
        }
    }else{
        for(int i=0;i<c&&n>1;i++){
            while(n%prime[i]==0){
                n/=prime[i];
                a[i]--;
            }
        }
    }

}

void pr(int i,int j)
{
    f[i][j]=1;
    for(int t=0;t<c;t++){
        f[i][j]*=(a[t]+1);
    }
}

void fen()
{
    for(int i=0;i<=431;i++){
        memset(a,0,sizeof(a));
        f[i][0]=1;
        for(int j=1;j<=i;j++){
            ad(i-j+1,1);
            ad(j,0);
            pr(i,j);
        }
    }
}

int main()
{
    int n,k;
    p();
    fen();
    while(~scanf("%d%d",&n,&k)){
        printf("%lld\n",f[n][k]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值