-
Notifications
You must be signed in to change notification settings - Fork 22
/
NMNMX
128 lines (110 loc) · 2.95 KB
/
NMNMX
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll crr[5005][5005]={0};
//*****************************************************************************
ll modInv(ll a,ll m=1000000007)
{
ll m0 = m;
ll y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1){
// q is quotient
ll q = a / m;
ll t = m;
// m is remainder now, process same as
// Euclid's algo
m = a % m, a = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
{
x += m0;
}
return x;
}
ll modDivide(ll a,ll b,ll m=1000000007)
{
// a = a % m;
ll inverse= modInv(b,m);
return (inverse*a)%m;
}
//******************************************************************************
ll modex(ll x,ll y,ll lebhai)
{
if(y<=0)
return 1;
else if(y%2==0)
return modex((x%lebhai*x%lebhai)%lebhai,y/2,lebhai);
else
return (x%lebhai*(modex((x%lebhai*x%lebhai)%lebhai,(y-1)/2,lebhai))%lebhai)%lebhai;
}
int main()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t,p=1000000006;
for (ll i = 0; i <= 5000; i++)
{
for (ll j= 0; j <= i; j++)
{
if (i == j||j == 0 )
{crr[i][j] = 1;
}
else
{crr[i][j] = (crr[i-1][j-1] + crr[i-1][j])%p;
}}
}
cin>>t;
while(t--)
{
ll n,k,it,in,pr=1,p3,p4,p2;
cin>>n>>k;
ll a[n];
for(it=0;it<n;it++)
{
cin>>a[it];
}
sort(a,a+n);
// if(n%2!=0)
// {
for( in=1;in<n-1;in++)
{ p2=(n-in-1)>=(k-1)? crr[n-in-1][k-1]:0;
p3=(in>=(k-1))?crr[in][k-1]:0;
p4=((n-1)>=(k-1))?crr[n-1][k-1]:0;
ll ans1=modex(a[in],p4%p,1000000007);
ll ans2=modex(a[in],((p2+p3)%p),1000000007);
pr= (pr%1000000007*(modDivide(ans1,ans2,1000000007))%1000000007)%1000000007;
// b[in]=(p4- p2-p3)%1000000006;
// b[n-in-1]=b[in];
}
// }
/*else
{
for( in=1;in<=(n/2)-1;in++)
{ p2=(n-in-1)>=(k-1)? crr[n-in-1][k-1]:0;
p3=(in>=(k-1))?crr[in][k-1]:0;
p4=((n-1)>=(k-1))?crr[n-1][k-1]:0;
// b[in]=(p4- p2-p3)%1000000006;
// b[n-in-1]=b[in];
}
}*/
/*for( in=1;in<=n-2;in++)
{cout<<b[in]<<" ";
}
cout<<"hello\n"<<endl;*/
/* for(in=1;in<n-1;in++)
{
ll ans1=modex(a[in],p4,1000000007);
ll ans2=modex(a[in],((p2+p3)%p),1000000007);
pr= (pr%1000000007*(modDivide(ans1,ans2,1000000007))%1000000007)%1000000007;
//(pr%1000000007*(modex(a[in],b[in],1000000007))%1000000007)%1000000007;
}*/
cout<<pr<<endl;
}
return 0;
}