Advertisement
sidjha57

Paintball

Sep 19th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.86 KB | None | 0 0
  1. //template
  2.  
  3. #include<bits/stdc++.h>
  4. //#include<ext/pb_ds/assoc_container.hpp>
  5. //#include<ext/pb_ds/tree_policy.hpp>
  6. //#include <ext/pb_ds/trie_policy.hpp>
  7. //using namespace __gnu_pbds;
  8. using namespace std;
  9. //typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
  10. //typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pbtrie;
  11.  
  12. #define ll                       long long int
  13. #define ld                       long double
  14. #define mod                      1000000007
  15. #define inf                      1e18
  16. #define endl                     "\n"
  17. #define pb                       push_back
  18. #define vi                       vector<ll>
  19. #define vs                       vector<string>
  20. #define pii                      pair<ll,ll>
  21. #define ump                      unordered_map
  22. #define mp                       make_pair
  23. #define pq_max                   priority_queue<ll>
  24. #define pq_min                   priority_queue<ll,vi,greater<ll> >
  25. #define all(n)                   n.begin(),n.end()
  26. #define ff                       first
  27. #define ss                       second
  28. #define mid(l,r)                 (l+(r-l)/2)
  29. #define bitc(n)                  __builtin_popcount(n)
  30. #define SET(a)                   memset( a, -1, sizeof a )
  31. #define CLR(a)                   memset( a,  0, sizeof a )
  32. #define Pi                       3.141592653589793
  33. #define loop(i,a,b)              for(int i=(a);i<=(b);i++)
  34. #define looprev(i,a,b)           for(int i=(a);i>=(b);i--)
  35. #define _fast                    ios_base::sync_with_stdio(0);  cin.tie(0);
  36. #define iter(container,it)       for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
  37. #define log(args...)             {string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
  38. #define logarr(arr,a,b)          for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;
  39. template <typename T> T          gcd(T a, T b){if(a%b) return gcd(b,a%b);return b;}
  40. template <typename T> T          lcm(T a, T b){return (a*(b/gcd(a,b)));}
  41. vs tokenizer(string str,char ch) {std::istringstream var((str)); vs v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;}
  42.  
  43. void err(istream_iterator<string> it) {}
  44. template<typename T, typename... Args>
  45. void err(istream_iterator<string> it, T a, Args... args) {
  46. cout << *it << " = " << a << endl;
  47. err(++it, args...);
  48. }
  49. #define MAXN 1000005
  50. ll factorial[MAXN];
  51.  
  52. void precompute() { // all factorials are calculated and stored
  53.     factorial[0]=1,factorial[1]=1;
  54.     loop (i,2,MAXN) factorial[i] = (factorial[i-1]%mod*i%mod)%mod;
  55. }
  56.  
  57. ll binpow(ll a, ll b) { // calculates a^b in logN
  58.     a %= mod;
  59.     long long res = 1;
  60.     while (b > 0) {
  61.         if (b & 1)
  62.             res = res * a % mod;
  63.         a = a * a % mod;
  64.         b >>= 1;
  65.     }
  66.     return res;
  67. }
  68.  
  69. ll inverse_mod (ll n) { // Q^-1 % mod
  70.     return binpow (n,mod-2ll);
  71. }
  72.  
  73. ll Combinatrics (ll n, ll r) { // nCr
  74.     ll n_r = factorial[n-r]; // n_r => n-r
  75.     n = factorial[n], r = factorial[r];
  76.     ll ans =  (n*inverse_mod(r)) %mod;
  77.     ans = (ans*inverse_mod(n_r))%mod;
  78.     return ans;
  79. }
  80.  
  81.  
  82. void solve() {
  83.    
  84.     ll N,G,K; cin >> N >> K >> G;
  85.     if (K == N-1) {
  86.         cout << "1\n";
  87.         return ;
  88.     }
  89.     if (K == 0) {
  90.         cout << "0\n";
  91.         return ;
  92.     }
  93.     ll n = factorial[N],g = factorial[G], g_1 = factorial[G-1],M = N/G;
  94.     ll n_m = factorial[N - M], num_of_ways_of_selecting = Combinatrics ( (N-K-1) , (M-1) ); // (n-k-1)C(m-1)
  95.        M = factorial[M];
  96.     ll m_g = binpow (M,G), m_g_1 = binpow (M, (G-1) );// (m!)^g , (m!)^(g-1)  
  97.     ll total_num_teams =  (n * inverse_mod (g)) %mod;
  98.        total_num_teams = (total_num_teams * inverse_mod (m_g)) %mod ; // n!/((m!)^g*g!)
  99.     ll num_of_ways_ashwin_has_no_friends = (num_of_ways_of_selecting * n_m ) % mod;
  100.        num_of_ways_ashwin_has_no_friends= (num_of_ways_ashwin_has_no_friends * inverse_mod (m_g_1)) % mod;
  101.        num_of_ways_ashwin_has_no_friends = (num_of_ways_ashwin_has_no_friends * inverse_mod (g_1) ) % mod;
  102.     ll ans = (total_num_teams - num_of_ways_ashwin_has_no_friends + mod) % mod;
  103.        ans = (ans*inverse_mod (total_num_teams) ) % mod;
  104.     cout << ans << "\n";
  105.  
  106. }
  107.  
  108. // void solve () {
  109. //     ll N,G,K; cin >> N >> K >> G;
  110. //     ll M = N/G;
  111. //     if (M-1 > N-K-1) {
  112. //         cout << "1\n";
  113. //         return;
  114. //     }
  115. //     ll a = factorial[N-K-1], b = factorial[N-M], c = inverse_mod (factorial[N-M-K]), d = inverse_mod (factorial[N-1]);
  116. //     ll ans = (a*b)%mod;
  117. //     ans = (ans*c)%mod;
  118. //     ans = (ans*d)%mod;
  119. //     ans = (1 + mod - ans) % mod;
  120. //     cout << ans << "\n";
  121. // }
  122.  
  123. int main(int argc, char const *argv[]){
  124.     _fast
  125.     precompute();
  126.     ll t; cin>>t;
  127.     while(t--){
  128.      solve();
  129.     }
  130.   return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement