rishu110067

innocent swaps and his emotions

Feb 18th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. ll mod=pow(10,9) +7;
  5.  
  6. ll modexp(ll x,ll y)
  7. {
  8. ll res=1;
  9. while(y>0)
  10. {
  11. if(y%2==1) res=(res*x)%mod;
  12.  
  13. y=y/2;
  14. x=(x*x)%mod;
  15. }
  16. return res;
  17. }
  18.  
  19. ll modinv(ll x)
  20. {
  21. return (modexp(x,mod-2)+mod)%mod;
  22. }
  23. //O(k)
  24. ll comb(ll n, ll k)
  25. {
  26. ll res = 1;
  27.  
  28. // Since C(n, k) = C(n, n-k)
  29. if ( k > n - k )
  30. k = n - k;
  31.  
  32. // Calculate value of
  33. // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
  34. ll den=1;
  35. for (ll i = 0; i < k; ++i)
  36. {
  37. res = (res*(n - i))%mod;
  38. den = (den*(i+1))%mod;
  39. }
  40.  
  41. res=(res*modinv(den))%mod;
  42.  
  43. return res;
  44. }
  45.  
  46. int main()
  47. {
  48. ios_base::sync_with_stdio(0);
  49. cin.tie(0); cout.tie(0);
  50.  
  51. int t; cin>>t;
  52. while(t--)
  53. {
  54. ll n,k; cin>>n>>k;
  55. ll nCk=comb(n,k);
  56. ll ans = (nCk*modexp(2,k))%mod;
  57. cout<<ans<<endl;
  58. }
  59. }
Add Comment
Please, Sign In to add comment