本题求C (n,k)的因子个数,开始我也是分别求n!/m!*(n-m)!分母和分子的素因子个数,再求因子个数果断tle了。。。
看过大牛们的discuss后,发现要打表,加上公式 c(n,k)=c(n,k-1)*(n-k+1)/k;求因子个数公式
设 N= P1^x1 * P2^x2* …… * Pn^xn;
则N的因子个数M为 M=(x1+1) * (x2+1) * …… *(xn+1);
附ac代码: (ps:有些变量名不规整,请不要在意细节←_←)
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int sign[500];
int pri[500];
int tot;
int map[500],map1[500];
long long c[500][500];
void getpri (){
    memset (sign,0,sizeof sign);
    tot=0;
    sign[0]=sign[1]=1;
    for (int i=2;i*i<500;i++){
        if (!sign[i]){
            for (int j=i*i;j<500;j+=i){
                sign[j]=1;
            }
        }
    }
    for (int i=2;i<500;i++){
        if (!sign[i]){
            pri[tot++]=i;
        }
    }
}
int main (){
    int n,k;
    long long ans,anss;
    getpri ();
    int nn=450;//long long ss[500][500];
    for (int o=0;o<nn;o++){
        c[o][0]=1;//ss[o][0]=1;
        memset (map,0,sizeof map);
            int f=0;
        for (int i=1;i<=o/2;i++){//ss[o][i]=1;
            memset (map1,0,sizeof map1);
            int temp=o-i+1;
            for (int j=0;pri[j]<=temp;j++){
                while (temp%pri[j]==0){
                    temp/=pri[j];
                    map[j]++;
                }
                f=max (f,j);
            }
            temp=i;
            for (int j=0;pri[j]<=temp;j++){
                while (temp%pri[j]==0){
                    temp/=pri[j];
                    map1[j]++;
                }
                f=max (f,j);
            }
            ans=1;
            for (int j=0;j<=f;j++){
                map[j]-=map1[j];
                map1[j]=0;
                if (map[j])
                    ans*=(map[j]+1);
            }
            c[o][i]=ans;
        }
    }
        while (cin>>n>>k){
            k=min (k,n-k);
            cout<<c[n][k]<<endl;
        }
    return 0;
}
原文地址:http://www.cnblogs.com/gfc-g/p/3842473.html