码迷,mamicode.com
首页 > 其他好文 > 详细

组合数学+整数分解 POJ 2992 Divisors

时间:2015-07-16 22:21:03      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

题意:给n,k,求C(n,k)的约数的个数。


由于C(n,k)=n!/(k!*(n-k)!),所以只要分别把分子分母的素因子的次数求出来,再用分子的每个素因子的次数减去分母的每个素因子的次数就可以得到C(n,k)的素数分解式,约数个数就等于(p1+1)(p2+1)*...*(pn+1).这道题n,k的范围都是四百多,按理说O(N^2)的算法可以过的,但是测试数据太多了,暴力的方法会TLE。看别人的报告知道了求N!的某个素因子次数的递归算法,然后枚举每个素数,求出它在阶乘中的次数,就可以AC了。


代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/




//int factor[100000][2];
//int cnt;
int p[83]={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};
int cal(int n,int p)
{if(n<p)
return 0;
return n/p+cal(n/p,p);

}
int cnt[90];

int main()
{
int n,k;
while(RII(n,k)!=EOF)
{long long int ans=1;
REP(i,0,83)
{
    cnt[i]=cal(n,p[i]);
    cnt[i]-=cal(n-k,p[i]);
    cnt[i]-=cal(k,p[i]);
    ans*=(cnt[i]+1);
}
printf("%I64d\n",ans);

}


        return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

组合数学+整数分解 POJ 2992 Divisors

标签:

原文地址:http://blog.csdn.net/u013840081/article/details/46916437

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!