Skip to content

Commit e9dfb91

Browse files
committed
added solution Day2/q002
1 parent 6d8d4e3 commit e9dfb91

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)