|
| 1 | +/*Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n |
| 2 | + different bloggers. Blogger numbered i |
| 3 | + has ai |
| 4 | + followers. |
| 5 | +
|
| 6 | +Since Masha has a limited budget, she can only sign a contract with k |
| 7 | + different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers. |
| 8 | +
|
| 9 | +Help her, find the number of ways to select k |
| 10 | + bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers). |
| 11 | +
|
| 12 | +For example, if n=4 |
| 13 | +, k=3 |
| 14 | +, a=[1,3,1,2] |
| 15 | +, then Masha has two ways to select 3 |
| 16 | + bloggers with the maximum total number of followers: |
| 17 | +
|
| 18 | +conclude contracts with bloggers with numbers 1 |
| 19 | +, 2 |
| 20 | + and 4 |
| 21 | +. In this case, the number of followers will be equal to a1+a2+a4=6 |
| 22 | +. |
| 23 | +conclude contracts with bloggers with numbers 2 |
| 24 | +, 3 |
| 25 | + and 4 |
| 26 | +. In this case, the number of followers will be equal to a2+a3+a4=6 |
| 27 | +. |
| 28 | +Since the answer can be quite large, output it modulo 109+7 |
| 29 | +. |
| 30 | +
|
| 31 | +Input |
| 32 | +The first line contains one integer t |
| 33 | + (1≤t≤1000 |
| 34 | +) — the number of test cases. Then t |
| 35 | + test cases follow. |
| 36 | +
|
| 37 | +The first line of each test case contains two integers n |
| 38 | + and k |
| 39 | + (1≤k≤n≤1000 |
| 40 | +) — the number of bloggers and how many of them you can sign a contract with. |
| 41 | +
|
| 42 | +The second line of each test case contains n |
| 43 | + integers a1,a2,…an |
| 44 | + (1≤ai≤n |
| 45 | +) — the number of followers of each blogger. |
| 46 | +
|
| 47 | +It is guaranteed that the sum of n |
| 48 | + over all test cases does not exceed 1000 |
| 49 | +. |
| 50 | +
|
| 51 | +Output |
| 52 | +For each test case, on a separate line output one integer — the number of ways to select k |
| 53 | + bloggers so that the total number of their followers is maximum possible.*/ |
| 54 | + |
| 55 | +/*Approach : |
| 56 | + sort the array. |
| 57 | + let total elements be n and no of bloggers to be selected is k. |
| 58 | + mark the element which is the first element in the bloggers included. |
| 59 | + count the occurances of that element, say a. |
| 60 | + count the no of elements greater than that boundary element, say b. |
| 61 | + Since total required was k and b are greater than k , this implies we need to select remaining k-b elements from a. therefore ans will be combitorial(a,k-b).*/ |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +#include <iostream> |
| 69 | +#include <algorithm> |
| 70 | +using namespace std; |
| 71 | +const long long MOD = 1000000007; |
| 72 | +long long power(long long a, long long b) { |
| 73 | + long long res = 1; |
| 74 | + while (b > 0) { |
| 75 | + if (b & 1) res = (res * a) % MOD; |
| 76 | + a = (a * a) % MOD; |
| 77 | + b >>= 1; |
| 78 | + } |
| 79 | + return res; |
| 80 | +} |
| 81 | +long long modInverse(long long a) { |
| 82 | + return power(a, MOD - 2); |
| 83 | +} |
| 84 | + |
| 85 | +int main(){ |
| 86 | + int t; |
| 87 | + cin>>t; |
| 88 | + |
| 89 | + while(t--){ |
| 90 | + long long int n,k; |
| 91 | + cin>>n; |
| 92 | + cin>>k; |
| 93 | + int arr[n]; |
| 94 | + long long int count=0; |
| 95 | + long long int big=0; |
| 96 | + for(int i=0;i<n;i++){ |
| 97 | + cin>>arr[i]; |
| 98 | + } |
| 99 | + if(n==k){ |
| 100 | + cout<<1<<endl; |
| 101 | + }else{ |
| 102 | + sort(arr,arr+n); |
| 103 | + for(int i=n-1;i>=0;i--){ |
| 104 | + if(arr[i]==arr[n-k]){ |
| 105 | + count=(count+1)%MOD; |
| 106 | + } |
| 107 | + if(arr[i]>arr[n-k]){ |
| 108 | + big++; |
| 109 | + } |
| 110 | + } |
| 111 | + k=k-big; |
| 112 | + if(k>(count/2)){ |
| 113 | + k=count-k; |
| 114 | + } |
| 115 | + if(k==0){ |
| 116 | + cout<<1<<endl; |
| 117 | + } |
| 118 | + else{ |
| 119 | + long long int dup=count; |
| 120 | + for(int i=1;i<k;i++){ |
| 121 | + count = (count * (dup - i)) % MOD; |
| 122 | + count = (count * modInverse(i + 1)) % MOD; |
| 123 | + |
| 124 | + } |
| 125 | + cout<<count<<endl; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + return 0; |
| 131 | +} |
0 commit comments