题解:CF1811G2 Vlad and the Nice Paths (hard version)
思路
特殊条件子序列计数,考虑动态规划。
定义
考虑枚举
对于
至于为什么是
代码
#include <bits/stdc++.h>
using namespace std;
const long long N = 5010,mod = 1e9 + 7;
long long qmi(long long a,long long k){
long long res = 1;
while (k){
if (k & 1){
res = (res * a) % mod;
}
a = (a * a) % mod;
k >>= 1;
}
return res;
}
long long fact[N],infact[N];
long long n,k;
long long a[N];
long long f[N],cnt[N];
long long C(long long a,long long b){
return ((fact[a] * infact[b]) % mod) * infact[a - b] % mod;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
long long T;
cin >> T;
fact[0] = 1;
for (long long i = 1; i < N; i ++ ){
fact[i] = fact[i - 1] * i % mod;
}
for (long long i = N - 1; i >= 0; i -- ){
infact[i] = qmi(fact[i],mod - 2);
}
while (T -- ){
cin >> n >> k;
memset(f,0,sizeof f);
memset(cnt,0,sizeof cnt);
for (long long i = 1; i <= n; i ++ ){
cin >> a[i];
}
cnt[0] = 1;
for (long long i = k; i <= n; i ++ ){
long long s = 0;
for (long long j = i; j >= 0; j -- ){
if (s >= k){
long long c = f[j] + k,t = (C(s - 1,k - 1)) % mod;
if (c > f[i]){
f[i] = c;
cnt[i] = (cnt[j] * t) % mod;
}
else if (c == f[i]){
cnt[i] = (cnt[i] + (cnt[j] * t) % mod) % mod;
}
}
if (a[i] == a[j]){
s ++ ;
}
}
}
long long mx = -1e9,res = 0;
for (long long i = 0; i <= n; i ++ ){
if (f[i] > mx){
mx = max(mx,f[i]);
res = 0;
res = (res + cnt[i]) % mod;
}
else if (f[i] == mx){
res = (res + cnt[i]) % mod;
}
}
cout << res << endl;
}
return 0;
}