Skip to content

Commit

Permalink
Added solution for NMNMX problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Suryamani123 authored Oct 30, 2018
1 parent 56a0433 commit 918ad91
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions NMNMX
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;

}

0 comments on commit 918ad91

Please sign in to comment.