1. 程式人生 > >POJ2992 Divisors 組合數,分解質因數

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