快速求阶乘的素因子---Divisors

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 2 63 - 1.

Sample Input

5 1
6 3
10 4

Sample Output

2
6
16

题目大意:

求解出组合数C(n,k) 的约数个数。

起初想用 求组合数+唯一分解定理 结果超时,附超时代码:

#include
#include
#include
#define ll long long
using namespace std;
const int maxn=1e7+10;
ll  c[500][500];
ll vis[maxn],p[maxn];
int k=0;
void build()
{
    c[0][0]=1;
    c[1][0]=c[1][1]=1;
    for(int i=2;i<440;i++)
    {
        c[i][0]=1;
        for(int j=0;j<440;j++)
        {
            c[i][j]=c[i-1][j-1]+c[i-1][j];
        }
    }
}
void find_prime(ll x)
{
    k=0;
    for(ll i=2;i1)
        s*=2;
    return s;
}
int main()
{
    int n,k;
    build();
    while(~scanf("%d%d",&n,&k))
   {
    ll x=c[n][k];
    find_prime(x);
    ll sum=slove(x);
    printf("%lld\n",sum);
   }
    return 0;

}

 正确写法:

快速求N!的素因子以及相应的幂+唯一分解定理

C(k , n) = n! / (k! * (n - k)!)

只要分别把分子分母的素因子的次数求出来,再用 分子的每个素因子的次数 减去 分母的每个素因子的次数 就可以得到C(n,k)的素数分解式,最后因数个数就等于(p1+1)(p2+1)*...*(pn+1).(唯一分解)

看别人的报告知道了求N!的某个素因子次数的递归算法,然后枚举每个素数,求出它在阶乘中的次数,就可以AC了。

对于不大于N的素数p,因为 1 *2*3*** N 中 存在N/p个数能够被p整除。所以N!中至少存在N/p个p。
提取出来上述能够被p整除的数, 然后再除以p, 就得到的 1 , 2 , … , N/pi 的序列。

同样的方法求(N/p)!中含有多少个p。
一直进行上述操作, 便能得到N!中素因子p的幂。
即 N!关于素数p的因子数

factor(N! , p) = N/p + N/(p^2) + N/(p^3) + ... + N/(p^m)

代码:

#include
#include
#include
#include
#define ll long long
using namespace std;
int jie[500][90];
ll zu[500][500];
int p[500],vis[500],num,k=0;

void find_prime()
{
    num=0;
    for(ll i=2;i<434;i++)
    {
        if(!vis[i])
        {
            p[num++]=i;
            for(ll j=2*i;j<434;j+=i)
            {
                vis[j]=1;
            }
        }
    }
}

void solve()
{

    //阶乘分解为 素因子相乘的形式jie[j][i]表示 j! 第i个素数的幂为多少
    //先当于factor(N! , p) = N/p + N/(p^2) + N/(p^3) + ... + N/(p^m)

    for(int i=0;i

 


 

 

你可能感兴趣的:(快速求阶乘的素因子---Divisors)