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