forked from Prince-1501/Hello_world-Competiitve-Programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
56_codechef_Chef_and_Interesting_Subsequences.cpp
102 lines (82 loc) · 1.92 KB
/
56_codechef_Chef_and_Interesting_Subsequences.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// main.cpp
// code
//
// Created by Prince Kumar on 23/09/19.
// Copyright © 2019 Prince Kumar. All rights reserved.
//
// code of Chef and Interesting Subsequences
// ** --- September long challenge -- ** //
#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
ll int gcd(ll int a, ll int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
void nCr(int n, int r)
{
// p holds the value of n*(n-1)*(n-2)...,
// k holds the value of r*(r-1)...
long long p = 1, k = 1;
// C(n, r) == C(n, n-r),
// choosing the smaller value
if (n - r < r)
r = n - r;
if (r != 0) {
while (r) {
p *= n;
k *= r;
// gcd of p, k
long long m = gcd(p, k);
// dividing by gcd, to simplify product
// division by their gcd saves from the overflow
p /= m;
k /= m;
n--;
r--;
}
// k should be simplified to 1
// as C(n, r) is a natural number
// (denominator should be 1 ) .
}
else
p = 1;
// if our approach is correct p = ans and k =1
cout << p << endl;
}
int main()
{
int T; cin>>T;
while(T--)
{
vector<int> v;
int n,k;cin>>n>>k;
for(int i=0;i<n;i++)
{
int x; cin>>x;
v.push_back(x);
}
sort(v.begin(),v.end());
int last_index_of_k = v[k-1];
int count=0;
for(int i=0;i<n;i++)
{
if(v[i]==last_index_of_k)
count++;
}
// we get total number of last_element
int num=0;
for(int i=0;i<k;i++)
{
if(v[i]==last_index_of_k)
num++;
}
nCr(count,num); // countCnum
}
}