Removal
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
Bobo has a sequence of integers s1, s2, ..., sn where 1 ≤ si ≤ k.
Find out the number of distinct sequences modulo (109+7) after removing exactly m elements.
输入描述:
The input consists of several test cases and is terminated by end-of-file. The first line of each test case contains three integers n, m and k. The second line contains n integers s1, s2, ..., sn.
输出描述:
For each test case, print an integer which denotes the result.
示例1
输入
3 2 2 1 2 1 4 2 2 1 2 1 2
输出
2 4
备注:
* 1 ≤ n ≤ 105 * 1 ≤ m ≤ min{n - 1, 10} * 1 ≤ k ≤ 10 * 1 ≤ si ≤ k * The sum of n does not exceed 106.
const int MAX=1010;
const int MOD=1e9;
int n,m,k;
int a[1000010];
int pre[100010];
int id[1000010];
int dp[MAX][20]; //dp[i][j]是指到第i位的已经删除了j位数字
//但是要注意当前位置的数字在之前已经删除过了 所以要判断当前位置的i-pre是否大于j
int main(){
while(cin>>n>>m>>k){
mms(pre,0);
mms(id,0);
for(int i=1;i<=n;i++)
{
cin>>a[i];
pre[i]=id[a[i]];
id[a[i]]=i;
}
mms(dp,0);
for(int i=0;i<=m;i++){
dp[i][i]=1;
}
for(int i=1;i<=n;i++){
int d=i-pre[i];
dp[i][0]=1;
for(int j=1;j<=m;j++){
if(j>i)break;
dp[i][j]=((dp[i-1][j-1]+dp[i-1][j])%MOD+MOD)%MOD;
if(pre[i]!=0&&d<=j){
dp[i][j]=((dp[i][j]-dp[pre[i]-1][j-d])%MOD+MOD)%MOD;
}
}
}
cout<<dp[n][m]<<endl;
}
}