Skip to content

Commit 7517f24

Browse files
authored
Merge pull request #288 from aricthecoder/day2sol2
Day2sol2
2 parents 876067f + e693a40 commit 7517f24

1 file changed

Lines changed: 147 additions & 0 deletions

File tree

  • Problems/Mathematics/Day-02/sol/Mohan
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
3+
Problem : E - Advertising Agency 1475
4+
5+
Approach : find no of element in which we have choice then apply nCr using MOD
6+
7+
Time Complexity : O(nlogn)
8+
Space Complexity : O(n)
9+
10+
https://codeforces.com/contest/1475/submission/355454137
11+
*/
12+
#include <bits/stdc++.h>
13+
#include <bits/stdc++.h>
14+
#include <ext/pb_ds/assoc_container.hpp>
15+
#include <ext/pb_ds/tree_policy.hpp>
16+
using namespace std;
17+
using namespace __gnu_pbds;
18+
19+
#define fast_io ios::sync_with_stdio(false); cin.tie(nullptr);
20+
#define int long long
21+
#define ull unsigned long long
22+
#define ld long double
23+
#define vi vector<int>
24+
#define vb vector<bool>
25+
#define vs vector<string>
26+
#define pii pair<int,int>
27+
#define pll pair<long long,long long>
28+
#define vpi vector<pii>
29+
#define vpl vector<pll>
30+
#define vvi vector<vi>
31+
#define mii map<int,int>
32+
#define si set<int>
33+
#define osi ordered_set<int>
34+
#define loop(i,n) for (int i = 0; i < n; i++)
35+
#define rloop(i,n) for (int i = n - 1; i >= 0; i--)
36+
#define loop1(i,a,b) for (int i = a; i <= b; i++)
37+
#define rloop1(i,a,b) for (int i = a; i >= b; i--)
38+
#define pb push_back
39+
#define ppb pop_back
40+
#define mp make_pair
41+
#define F first
42+
#define S second
43+
#define lb lower_bound
44+
#define ub upper_bound
45+
#define all(x) (x).begin(), (x).end()
46+
#define rall(x) (x).rbegin(), (x).rend()
47+
#define sz(x) ((int)(x).size())
48+
#define findbo(x) find_by_order(x) // returns iterator to the k-th largest element (0-based)
49+
#define orderbk(x) order_of_key(x) // returns number of items strictly smaller than x
50+
#define endl '\n'
51+
#define printy cout<<"YES"<<endl
52+
#define printn cout<<"NO"<<endl
53+
#define vout(a) for (int i = 0; i < a.size(); i++) cout << a[i] << ' '; cout << endl;
54+
#define vpout(a) loop(i,sz(a)) cout <<a[i].F<<' '<< a[i].S<< endl; cout << endl;
55+
#define rt(x){ cout<<x<<endl; return; }
56+
template<typename T>
57+
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // less_equal<T> for multiset, greater<T> for decreasing order
58+
59+
const int MOD = 1e9 + 7;
60+
const int INF = 1e9;
61+
62+
#ifndef ONLINE_JUDGE
63+
#define debug(x) cerr<<#x<<" = "; _print(x); cerr<<endl;
64+
template<typename T> void _print(const T& x){ cerr<<x; }
65+
template<typename T,typename U> void _print(const pair<T,U>& p){ cerr<<"("; _print(p.first); cerr<<", "; _print(p.second); cerr<<")"; }
66+
template<typename T> void _print(const vector<T>& v){ cerr<<"["; for(size_t i=0;i<v.size();++i){ _print(v[i]); if(i+1!=v.size()) cerr<<", "; } cerr<<"]"; }
67+
template<typename T> void _print(const set<T>& s){ cerr<<"{"; for(auto it=s.begin(); it!=s.end(); ++it){ _print(*it); if(next(it)!=s.end()) cerr<<", "; } cerr<<"}"; }
68+
template<typename T,typename U> void _print(const map<T,U>& m){ cerr<<"{"; for(auto it=m.begin(); it!=m.end(); ++it){ _print(it->first); cerr<<": "; _print(it->second); if(next(it)!=m.end()) cerr<<", "; } cerr<<"}"; }
69+
#else
70+
#define debug(x)
71+
#endif
72+
73+
#define sumV(a) accumulate(all(a), 0LL)
74+
#define minV(a) min_element(all(a))
75+
#define maxV(a) max_element(all(a))
76+
77+
int log2f(long long n) {
78+
if (n==0) return -1;
79+
int res = 0;
80+
while (n > 1){
81+
n >>= 1;
82+
res++;
83+
}
84+
return res;
85+
}
86+
87+
int binpow(int a, int n) {
88+
int result = 1;
89+
a %= MOD;
90+
while (n > 0) {
91+
if (n & 1)
92+
result = (result * a) % MOD;
93+
a = (a * a) % MOD;
94+
n >>= 1;
95+
}
96+
return result;
97+
}
98+
99+
int modinv(int a){
100+
return binpow(a,MOD-2);
101+
}
102+
103+
int nCr(int n,int r) {
104+
if (r<0 || r>n) return 0;
105+
if (r>n-r) r = n-r;
106+
107+
int res = 1;
108+
for(int i = 1; i<=r; i++){
109+
res = (res*(n-r+i))%MOD;
110+
res = (res * modinv(i))%MOD;
111+
}
112+
return res;
113+
}
114+
115+
116+
void solve(){
117+
int n,k;
118+
cin>>n>>k;
119+
vi a(n);
120+
map<int,int,greater<int>> mp;
121+
loop(i,n){
122+
cin>>a[i];
123+
mp[a[i]]++;
124+
}
125+
126+
int m;
127+
for(auto it : mp){
128+
if(it.S>k){
129+
m = it.S;
130+
break;
131+
}
132+
else k-=it.S;
133+
}
134+
135+
cout<< nCr(m,k) <<endl;
136+
137+
}
138+
139+
int32_t main(){
140+
fast_io;
141+
int t=1;
142+
cin>>t;
143+
while(t--){
144+
solve();
145+
}
146+
return 0;
147+
}

0 commit comments

Comments
 (0)