POJ 2992 Divisors(约数定理)

题目链接:

http://poj.org/problem?id=2992

解题思路:

求C(n,k)的因子个数。。。

将1!~431!的每个素因子的个数打表,而C(n,k)=n!/(k!*(n-k)!),所以再利用求因子个数的公式即可http://blog.csdn.net/piaocoder/article/details/47954385

AC代码:

#include 
#include 
#include 
#include 
using namespace std;

typedef long long ll;
vector v;
int prime[500];
int a[450][450];
int vis[500],nprime;
int sum[500];

void getprime(){
    nprime = 0;
    memset(vis,0,sizeof(vis));
    for(int i = 2; i <= 450; i++){
        int t = 450/i;
        for(int j = 2; j <=t; j++)
            vis[i*j] = 1;
    }
    for(int i = 2; i <= 450; i++){
        if(!vis[i])
            prime[nprime++] = i;
    }
}

void div(int x){
    v.clear();
    int t = x;
    for(int i = 0; i < nprime && prime[i]*prime[i] <= x; i++){
        while(t % prime[i] == 0){
            v.push_back(prime[i]);
            t /= prime[i];
        }
        if(t == 1)
            break;
    }
    if(t > 1)
        v.push_back(t);
}

void solve(){
    memset(a,0,sizeof(a));
    for(int i = 2; i <= 431; i++){
        memcpy(a[i],a[i-1],sizeof(a[i]));
        div(i);
        for(int j = 0; j < v.size(); j++)
            a[i][v[j]]++;
    }
}

int main(){
    getprime();
    solve();
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        memset(sum,0,sizeof(sum));
        for(int i = 1; i <= 431; i++)
            sum[i] += a[n][i]-a[k][i]-a[n-k][i];
        ll ans = 1;
        for(int i = 2; i <= 431; i++)
            ans *= (sum[i]+1);
        printf("%lld\n",ans);
    }
    return 0;
}


你可能感兴趣的:(约束定理)