Divisors
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10784 | Accepted: 3193 |
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 2
63 - 1.
Sample Input
5 1 6 3 10 4
Sample Output
2 6 16
① 由素数基本定理可知,一个数n可以唯一地表示成素数的乘积:n=(p1^a1)*(p2^a2)*(p3^a3)......*(pk^ak),其中pi是素数。
② 因数个数: 若n分解成 n=(p1^a1)*(p2^a2)*(p3^a3)......*(pk^ak),
则n的因子个数为:(a1+1)*(a2+1)*(a3+1)....*(ak+1)。 (理解:pi可以取0个,1个,2个,,,ai个,所以共ai+1种取法,由乘法原理,故全部的因子的个数是全部情况的乘积)
③ n的阶乘n!素数分解中,素数p的指数由下面的公式给出:[n/p]+[n/p^2] + [n/p^3] + ......
④ C(n,k)=n!/(k!*(n-k)!) , 故组合数的分解转换成了对阶乘的分解。
设ans=C(n,k), p为素数,假如n!的分解中p的次数是a,k!的分解中p的次数是b,(n-k)!的分解中p的次数是c,那么ans的分解中p的次数是:a-(b+c).
至此题目基本上就解出来了,另外就是要用线性筛法预处理出素数,不然会超时。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
typedef __int64 ll;
const int maxn=431;
int pri[maxn],tot;
bool is[maxn+5];
void prime_table()
{
int i,j;
memset(is,0,sizeof(is));
is[0]=is[1]=1;
for(i=2;i<=maxn;i++){
if(!is[i])
pri[tot++]=i;
for(j=0;j<tot && i*pri[j]<=maxn;j++){
is[i*pri[j]]=1;
if(!(i%pri[j])) break;
}
}
}
int call(int n, int p)
{
if (n < p)
return 0;
return n / p + call(n / p, p);
}
int a[3][maxn+5];
void cal(int n,int v)
{
int i,j;
memset(a[v],0,sizeof(a[v]));
for(i=0;i<tot;i++){
if(pri[i]>n) break;
a[v][pri[i]]=call(n,pri[i]);
}
}
int main()
{
int i,n,k;
ll ans;
tot=0;
prime_table();
while(scanf("%d%d",&n,&k)!=EOF){
cal(n,0);
cal(k,1);
cal(n-k,2);
ans=1;
for(i=2;i<=maxn;i++)
ans*=(a[0][i]-a[1][i]-a[2][i]+1);
printf("%I64d\n",ans);
}
return 0;
}