Skip to content

Commit 09c3e5f

Browse files
authored
Merge pull request #342 from Naman2251/main
Create day002sol02.cpp
2 parents ea59925 + aa42070 commit 09c3e5f

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

Problems/Mathematics/Day-02/sol/Naman Pal/day002sol01 renamed to Problems/Mathematics/Day-02/sol/Naman Pal/day002sol01.cpp

File renamed without changes.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Submission LInk: https://codeforces.com/contest/1475/submission/355652328
2+
3+
/*
4+
E. Advertising Agency: We are given n bloggers and each blogger has at max n followers. We have to find the number of ways to select k
5+
bloggers so that the total number of their followers is maximum possible.
6+
7+
So first I made an array to count the number of followers of each blogger in an array and then iterated through the array from last checking if the number of bloggers
8+
having that many followers are equal or greater or less than k. If they are less than k then all need to be counted in order to get the maximum so I subtracted
9+
those blogger's count from k. If the number of bloggers with index number of followers is equal to or greater than k then we have to select any k of them so we
10+
apply C(no.of blogger with given no. of follower, k) and then break through the loop as our work is done.
11+
Now we need to handle the time complexity. For nCr we need to again and again find factorial so in order to remove that problem I made an array of factorial from 0
12+
to 1000. Since array size is small therefore it take O(1) T.C. and we can get factorial of any number in O(1).Also while calculating factorial using dp I made sure
13+
to take mod in each multiplication step. Now to find nCr we use the formula (n! / (r! * (n-r)!)). However, since we are working with modulo arithmetic, division is
14+
not straightforward. We multiply by the modular inverse instead. By Fermat's Little Theorem, the modular inverse of a number a modulo m (where m is prime) is
15+
a^{m-2}. The function `binexp` calculates this power efficiently in O(log n) time.
16+
*/
17+
18+
#include <bits/stdc++.h>
19+
using namespace std;
20+
#define fastio ios::sync_with_stdio(false); cin.tie(NULL);
21+
22+
const long long mod=1e9+7;
23+
24+
vector<long long> fact(1001);
25+
26+
long long binexp(long long a, long long b) {
27+
long long ans = 1;
28+
a %= mod;
29+
while (b) {
30+
if (b & 1) ans = ans * a % mod;
31+
a = a * a % mod;
32+
b >>= 1;
33+
}
34+
return ans%mod;
35+
}
36+
37+
long long C(long long n, long long r) {
38+
if (r < 0 || r > n) return 0;
39+
return fact[n] * binexp(fact[r], mod - 2) % mod * binexp(fact[n - r], mod - 2) % mod;
40+
}
41+
42+
int main() {
43+
fastio;
44+
fact[0]=1;
45+
for(long long i=1; i<1001; i++) fact[i]=(fact[i-1]*i)%mod;
46+
long long t;
47+
cin>>t;
48+
while(t--) {
49+
long long n,k;
50+
cin>>n>>k;
51+
long long cnt[n+1]={0};
52+
for(long long i=0; i<n; i++) {
53+
long long a;
54+
cin>>a;
55+
cnt[a]++;
56+
}
57+
for(long long i=n; i>=0; i--) {
58+
if(cnt[i]>=k) {
59+
cout<<C(cnt[i], k)%mod<<endl;
60+
break;
61+
}
62+
else k-=cnt[i];
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)