forked from shrox/Random-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calc_nCr.cpp
55 lines (49 loc) · 963 Bytes
/
Calc_nCr.cpp
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
#include<iostream>
#include<vector>
using namespace std;
const long long int MOD=1e9+7;
long long int n,t,m,ans;
long long in(long long int a)
{
long long b=MOD-2;
long long x=1,y=a;
while(b > 0)
{
if(b%2 == 1)
{
x=(x*y);
if(x>MOD) x%=MOD;
}
y = (y*y);
if(y>MOD) y%=MOD;
b /= 2;
}
return x;
}
long long int C(int n, int r)
{
vector<long long> f(n+1,1);
for(int i=2;i<n+1;i++)
{
f[i]=(f[i-1]*i)%MOD;
}
return((f[n]*((in(f[r])*in(f[n-r]))%MOD))%MOD);
}
int main()
{
cin>>t;
for(int i=0;i<t;i++)
{
cin>>n;
ans=0;
int a=1;
for(int j=0;j<n;j++)
{
cin>>m;
cout<<C(n-1,j)<<endl;
ans=(ans+(a*C(n-1,j)*m)%MOD)%MOD;
a=a*-1;
}
cout<<ans<<endl;
}
}