poj2992 divisors 求组合数的约数个数,阶乘的质因数分解
Posted vainglory
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj2992 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
思路:计算约数个数的时候,可以类比数的唯一分解求约数个数;
s=2^p1*3^p2*5^p3*-----
num=(p1+1)*(p2+1)*(p3+1)*....
如果求分数类型的约数个数...
可以分解质因数之后在上面的个数-下面的个数
即s__=2^(p1-p1_)*3^(p2-p2_)*----
num=(p1-p1_+1)*(p2-p2_+1)*(p3-p3_+1)*---
阶乘的质因数分解:
int cal(int n,int p) if(n<p)return 0;else return n/p+cal(n/p,p);
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int prime[500];bool vis[500]; typedef long long ll;int cnt; void init(){ for(int i=2;i<=431;i++){ if(!vis[i]) for(int j=i*2;j<=431;j+=i) vis[j]=1; } for(int i=2;i<=431;i++){ if(!vis[i]) prime[++cnt]=i; } } int num1[500],num2[500]; int cal(int n,int p){ if(n<p) return 0; else return n/p+cal(n/p,p); } int main(){ init();int n,m; //std::ios::sync_with_stdio(false);cin.tie(0); //for(int i=1;i<=83;i++) cout<<prime[i]<<endl; while(~scanf("%d%d",&n,&m)){ ll ans=1; memset(num1,0,sizeof(num1)); memset(num2,0,sizeof(num2)); for(int i=1;prime[i]<=n&&i<=cnt;i++){ num1[i]=cal(n,prime[i]); num2[i]=cal(m,prime[i])+cal(n-m,prime[i]); ans*=(num1[i]-num2[i]+1); //cout<<ans<<endl; } printf("%lld\n",ans); } return 0; }
以上是关于poj2992 divisors 求组合数的约数个数,阶乘的质因数分解的主要内容,如果未能解决你的问题,请参考以下文章
HDU 6069 Counting Divisors(区间素数筛法)
hdu-1492 The number of divisors(约数) about Humble Numbers---因子数公式