作者:天才野猪518 | 来源:互联网 | 2023-10-12 17:53
点击打开链接
Divisors
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 9856 |
|
Accepted: 2896 |
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 263 - 1.
Sample Input
5 1
6 3
10 4
Sample Output
2
6
16
Source
CTU Open 2005
求C(n,k)的约数的个数。
n和k都很大,直接求肯定不行。假设将一个数表示成它的质因数分解,如A=a^p1*b^p2*c^p3*...*n^pn.
那么它的约数个数就是:ans=(p1+1)*(p2+1)*(p3+1)*...*(pn+1).而C(n,k)=n!/[(k!*(n-k)!],c[n][k]代表n的阶乘时能够分解出几个k。那么只需要求出他们的阶乘对于每一个素数的个数就可以了。公式:ai=c[n][prime[i]]-c[k][prime[i]]-c[(n-k)][prime[i]]。ans=a1*a2.*...*ak
(k代表当prime[k]小于n的时候)。
//3624K 610MS
#include
#include
#define N 100007
bool visit[1010];
long long c[1500][1500];
long long prime[107];
void init_prim()//prime存的是下标,visit存的是数。visit[5]==true。
{
long long num=0;
memset(visit,true,sizeof(visit));
for(long long i=2;i<1007;++i)
{
if(visit[i]==true)
{
num++;
prime[num]=i;
}
for(long long j=1;((j<=num)&&(i*prime[j]<=10007));++j)
{
visit[i*prime[j]]=false;
if(i%prime[j]==0) break;
}
}
}
void init()
{
for(long long i=2;i<=437;i++)
for(long long j=1;prime[j]<=i;j++)
{
long long n=i,res=0;
while(n){n/=prime[j];res+=n;}
c[i][prime[j]]=res;
}
}
int main()
{
long long n,k;
init_prim();
init();
while(scanf("%I64d%I64d",&n,&k)!=EOF)
{
long long ans=1,a;
for(long long i=1;prime[i]<=n;i++)
{
a=c[n][prime[i]]-c[k][prime[i]]-c[n-k][prime[i]];
ans*=(1+a);
}
printf("%I64d\n",ans);
}
return 0;
}