###### TOJ 4111 Binomial efficient calculate C(n,k)

For Mod 2^n,you can also use &(2^n-1),it will more faster.

M % (2^n) = M & (2^n-1);

It's use a way to get all quality factors of N!.

The portal:http://acm.tju.edu.cn/toj/showp4111.html

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

const long long  N = 1000000;
long long tag[1000006],p[1000006],numtot;
long long Mod;

long long a1[1000006];

long long Fast_Power(long long n,long long k){
long long ans = 1;
while(k){
if(k & 1) ans = ( ans * n ) % Mod;
k /= 2;
n = ( n * n ) % Mod;
}
return ans;
}

void Get_prime()
{
long long  cnt = 0;
memset(tag,0,sizeof(tag));
for (long long  i = 2; i <= N; i++)
{
if (!tag[i])    p[cnt++] = i;
for (long long  j = 0; j < cnt && p[j] * i <= N; j++)
{
tag[i*p[j]] = 1;
if (i % p[j] == 0)
break;
}
}
numtot = cnt;
}

void Deal_with(){
long long  T,n,k;
scanf("%lld",&T);
while(T--){
scanf("%lld %lld",&n,&k);
//memset(a1,0,sizeof(a1)); Can't use memset,It's TLE.
int tempi;
for(long long  i=0;i<numtot;i++){
if(p[i] > n)break;
a1[p[i]] = 0;
for(long long  j=p[i];j<=n;j *= p[i])a1[p[i]] += n / j;
for(long long  j=p[i];j<=k;j *= p[i])a1[p[i]] -= k / j;
for(long long  j=p[i];j<=n-k;j *= p[i])a1[p[i]] -= (n-k) / j;
tempi = i;
}
Mod = 1;
for(long long  i=1;i<=32;i++){
Mod *= 2;
}
long long ans = 1;
for(long long  i=0;i<=tempi;i++){//It should use tempi,Because if i > tempi,it's no initialization.
ans = ( ans * Fast_Power(p[i],a1[p[i]]) ) % Mod;
}
printf("%lld\n",ans);
}
}

int main(void){
Get_prime();
Deal_with();
return 0;
}

#### Binomial Coefficients

2014-05-01 22:39:23

#### 二项分布，binomial(n,k,p)

2017-09-06 09:35:08

#### [BZOJ2629]binomial (高精度+Lucas定理+离散对数+FFT)

2016-05-29 23:11:11

#### TOJ 题目分类

2010-11-03 22:39:00

#### bzoj 2629: binomial （FFT+DP+Lucas定理+短除法）

2017-03-02 14:46:52

#### poj2249 Binomial Showdown 求组合数

2016-04-05 18:21:49

#### uva 10375 分解质因数算指数

2016-07-11 20:29:46

#### 二项分布（Binomial Distribution）

2017-09-15 12:48:16

#### Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge). In this problem, your task is to cal

2017-03-28 20:15:47

#### UVA10375唯一分解定理

2017-10-12 21:39:11

## 不良信息举报

TOJ 4111 Binomial efficient calculate C(n,k)