codeforce 1600
COFO::1475E Advertising Agency
cofo problem
COFO::1475E Advertising Agency
Problem
- level : 1600
- tag : combinatorics, math, sortings
- TIME
- to understand : 5
- to algorithm : 15
- to code : 5
- to debug : 3
- understanding edit : 0
Point
- n and k are given
- Find the maximum sum when k indices are chosen, and print how many ways there can be
Design
- Since we need to find the maximum sum, we have to fix larger number first.
- Then there will be last number on the list
- For this number,
- Let’s say the number is x and the count of number is y
- Then we have to choose which x’s we are going to use, because there are y poisitions
- This is exactly combinatorics problem, choosing ‘left count’ from x
- For this number,
- To do this, it’s necessary to get combinatorics first
- To acheive that, we use dp
- Since we can make combinatorics by adding two numbers like,
- nCr[i][j] = nCr[i-1][j-1] + nCr[i-1][j]
Complexity
- O(N^2)
Code
#include<bits/stdc++.h>
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(int i = (a); i <(b); i++)
#define r_rep(i, a, b) for(int i = (a); i >(b); i--)
typedef long long ll;
using namespace std;
const ll MOD = 1e9 + 7;
ll nCx[1003][1003];
void _init() {
nCx[1][0] = nCx[1][1] = 1;
rep(i, 2, 1001) {
nCx[i][0] = nCx[i][i] = 1;
rep(j, 1, i) {
nCx[i][j] = nCx[i-1][j-1] + nCx[i-1][j];
nCx[i][j] %= MOD;
}
}
}
void solve() {
ll n, k; cin >> n >> k;
vector<pair<int,int> > v(1001, {0, 0});
rep(i, 0, n) {
int x; cin >> x;
v[x].fi = x;
v[x].se++;
}
sort(v.rbegin(), v.rend());
ll ans = 0, cnted = 0;
rep(i, 0, n) {
if (v[i].se + cnted == k) {ans = 1; break;}
else if (v[i].se + cnted < k) {cnted += v[i].se;}
else {
ll x = v[i].se, y = k - cnted;
ans = nCx[x][y];
break;
}
}
cout << ans << '\n';
}
int main(){
_init();
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int tc; cin >> tc; while(tc--) solve();
return 0;
}