poj 2992 Divisors——数论

题意:求组合数C[n][k]的约数个数。(0<= k <= n <= 431)
思路:一个数num的约数个数为cnt,将num质因数分解,得num = p1^a1 * p2^a2 * p3^a3 * ……*pn^an.
则约数个数cnt = (a1 + 1) * (a2 + 1) * (a3 + 1) * …… *(an + 1).
C[n][k] = n ! / ((n - k) ! * k !).
cal(n, k) 函数求出 n! 时能分解出几个 k。
(1) 先预求 1 到 431 的素数表。
(2) 则C[n][k]中包含素数prime[i]的个数为ai = cal(n!, prime[i]) - cal((n - k)!, prime[i]) - cal(k!, prime[i])。

需要预处理。否则超时

a27400 2992 Accepted 2080K 594MS G++ 949B 2011-09-06 20:24:18
#include<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<cmath>
#include
<algorithm>

typedef
long long LL;

LL prime[
100]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,1000000};

LL CAL[
500][500];

void init(void)
{
LL i,j;
for(i=2;i<=431;i++)
for(j=0;prime[j]<=i;j++)//素数表最后是1000000是因为这里懒得再写j<blablabla了。。。
{
LL n
=i;
LL res
=0;
while(n)
{
n
/=prime[j];
res
+=n;
}
CAL[i][prime[j]]
=res;
}
}

int main(void)
{
LL n,k;
init();
while(~scanf("%lld %lld",&n,&k))
{
LL total
=1;
LL i;
for(i=0;prime[i]<=n;i++)
{
LL temp
=CAL[n][prime[i]]-CAL[k][prime[i]]-CAL[n-k][prime[i]];
total
*=(temp+1);
}
printf(
"%lld\n",total);
}
return 0;
}


你可能感兴趣的:(div)