Skip to content

Commit ca72a34

Browse files
solved second question of day 2
1 parent 730c3a5 commit ca72a34

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include <bits/stdc++.h>
2+
3+
#include <ext/pb_ds/assoc_container.hpp>
4+
#include <ext/pb_ds/tree_policy.hpp>
5+
6+
using namespace std;
7+
using namespace __gnu_pbds;
8+
9+
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
10+
// less makes it sorted greater makes it reverse sorted less equal makes it like multiset
11+
// we can also change the data type of the pbds
12+
13+
// Fast I/O
14+
#define FAST_IO \
15+
ios::sync_with_stdio(false); \
16+
cin.tie(NULL);
17+
#define pb push_back
18+
#define in insert
19+
#define int long long
20+
#define all(x) (x).begin(), (x).end()
21+
22+
// Typedefs for convenience
23+
typedef vector<int> vi;
24+
typedef pair<int, int> pii;
25+
26+
// Constants
27+
const int INF = 1e18;
28+
const int MOD = 1e9 + 7;
29+
30+
// Debug (can be disabled in contests)
31+
#ifdef DEBUG
32+
#define dbg(x) cerr << #x << " = " << x << '\n';
33+
#else
34+
#define dbg(x)
35+
#endif
36+
37+
// gcd
38+
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
39+
40+
// lcm
41+
int lcm(int a, int b)
42+
{
43+
if (a == 0 || b == 0)
44+
return 0;
45+
return abs(a * b) / gcd(a, b);
46+
}
47+
48+
// Binary exponentiation (modular)
49+
long long binexp(long long a, long long b, long long m)
50+
{
51+
a %= m;
52+
long long res = 1;
53+
while (b > 0)
54+
{
55+
if (b & 1)
56+
res = res * a % m;
57+
a = a * a % m;
58+
b >>= 1;
59+
}
60+
return res;
61+
}
62+
vi fac(1001);
63+
64+
void solve()
65+
{
66+
int n, k;
67+
cin >> n >> k;
68+
vi v(n + 1, 0);
69+
for (int i = 1; i <= n; i++)
70+
{
71+
cin >> v[i];
72+
}
73+
74+
sort(all(v));
75+
int j = n - k + 1;
76+
int ele = v[n - k + 1];
77+
map<int, int> mp;
78+
for (int i = 1; i <= n; i++)
79+
{
80+
mp[v[i]]++;
81+
}
82+
83+
int i;
84+
for (i = j; i <= n; i++)
85+
{
86+
if (v[i] != ele)
87+
{
88+
break;
89+
}
90+
}
91+
int left = i - j;
92+
int p = fac[mp[ele]];
93+
// cout<<p<<endl;
94+
int q = fac[left];
95+
int s = fac[mp[ele] - left];
96+
q = binexp(q, MOD - 2, MOD);
97+
s = binexp(s, MOD - 2, MOD);
98+
int res = (((p % MOD) * (q % MOD)) % MOD * (s % MOD)) % MOD;
99+
cout << res << endl;
100+
}
101+
102+
signed main()
103+
{
104+
FAST_IO
105+
fac[0] = 1;
106+
for (int i = 1; i <= 1000; i++)
107+
{
108+
fac[i] = (fac[i - 1] * i) % MOD;
109+
}
110+
111+
int t = 1;
112+
cin >> t;
113+
while (t--)
114+
solve();
115+
return 0;
116+
}
117+
/*
118+
So basically we're asked to count number of ways we can take k elements such that there sum is maximum now
119+
if all elements were unique we could have just taken last k elements and answer would be 1
120+
but since they are unique
121+
we can have something like n= 6 ;k=5; 1 1 1 2 2 2 where we can choose any two of the three ones hence
122+
we need to take how many of those last elments we are taking as left and total now many elements of last are there are ele
123+
hence the answer would be mp[ele]Cleft which is easily solvable by using fermet's little theorem to find modulo inverse
124+
125+
126+
time complexity o(1000)
127+
space complexity o(1000)
128+
129+
submission id :https://codeforces.com/contest/1475/submission/355268225
130+
131+
132+
133+
*/

0 commit comments

Comments
 (0)