Gym - 101982C Contest Setting (动态规划)

A group of contest writers have written n problems and want to use k of them in an upcoming contest. Each problem has a difficulty level. A contest is valid if all of its k problems have different difficulty levels.

Compute how many distinct valid contests the contest writers can produce. Two contests are distinct if and only if there exists some problem present in one contest but not present in the other.

Print the result modulo 998,244,353.

Input

The first line of input contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 1000)

The next line contains n space-separated integers representing the difficulty levels. The difficulty levels are between 1 and 109 (inclusive).

Output

Print the number of distinct contests possible, modulo 998,244,353.

Sample Input

5 2

1 2 3 4 5

Sample  Output

10

题意:

选出k个难度不同的题,问有几种选法。

思路:

dp[i][j]表示从i种不同的题中选出j个不同的题的方法数。

当前题选的话,方法数是dp[i-1][j-1]*(当前难度题的个数)

当前题不选的话,方法数是dp[i-1][j].

dp[i][j]就是二者之和。

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
map<int,int>mp;
ll dp[][];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
ll ans=;
ll num=;
for(int i=;i<=n;i++){
int x;
scanf("%d",&x);
mp[x]++;
}
map<int,int>::iterator it=mp.begin();
int sz=mp.size();
for(int i=;i<=sz;i++){
dp[i][]=;
}
for(int i=;i<=sz;i++){
for(int j=k;j>=;j--){
dp[i][j]=dp[i-][j]+dp[i-][j-]*(it->second);
dp[i][j]%=mod;
}
it++;
}
printf("%lld\n",dp[sz][k]); }
上一篇:[HAOI 2016]食物链


下一篇:java由字符型强制转化为整型例题