-
Notifications
You must be signed in to change notification settings - Fork 0
/
nCr.c
57 lines (53 loc) · 876 Bytes
/
nCr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<stdio.h>
unsigned long long GCD1(unsigned long long a,unsigned long long b)
{
unsigned long long t=0;
while(b>0)
{
t=a%b;
a=b;
b=t;
}
return a;
}
void DIVIDE(unsigned long long &a,unsigned long long &b)
{
unsigned long long h=GCD1(a,b);
a/=h;
b/=h;
}
int main()
{
int t;
unsigned long long i,n,k,d;
scanf("%d",&t);
while(t--)
{
scanf("llu",&n);
scanf("%llu",&k);
if(k==0 || n==k)
{
printf("1\n");
continue;
}
if(k>n)
{
printf("0\n");
continue;
}
unsigned long long num1=1,den=1,mul,div;
if(k>n/2)k=n-k;
for(i=k;i;i--)
{
mul=n-k+i;
div=i;
DIVIDE(mul,div);
DIVIDE(mul,den);
DIVIDE(num1,div);
num1*=mul;
den*=div;
}
printf("%llu\n",num1/den);
}
return 0;
}