Cute sequence
description
Counting ducks like sequences, he thinks the following sequence is cute
1: The elements in the sequence are greater than or equal to \ (0 \) and less than or equal to \ (40 \)
2: Each element in the sequence is not greater than the average of the previous numbers
3: There are no three consecutive decreasing numbers
There is a sequence \ (a (−1≤a_i≤40-1) \)
You can change \ (-1 \) to any integer and find out how many different cute sequences the \ (a \) sequence can become. The answer is modulo \ (10 ^ 9 + 7 \)
Input
Enter an integer in the first line \ (n \)
Enter \ (n \) integers in the second line
Output
Output an integer
data range
100% data: \ (n ≤ 40 \)
answer
Cute you horse.
Set \ (dp [i] [j] [0/1] [k] \) to select \ (i \) , and \ (j \) for \ (i \) , whether this bit is Less than the previous one, the sum of the selected numbers
The transfer equation is obvious.
#include<bits/stdc++.h>
using namespace std;
#define long long int
const int mod=1e9+7;
int n;
int a[50];
int dp[50][50][2][1610];
int main(){
scanf("%lld",&n);
for(int i=1;i<=n;++i){
scanf("%lld",&a[i]);
}
if(a[1]!=-1){
dp[1][a[1]][0][a[1]]=1;
}
else {
for(int i=0;i<=40;++i){
dp[1][i][0][i]=1;
}
}
for(int i=2;i<=n;++i){
if(a[i]!=-1){
for(int j=a[i]*(i-1);j<=1600-a[i];++j){
for(int k=0;k<=a[i];++k){
dp[i][a[i]][0][j+a[i]]+=dp[i-1][k][0][j];
dp[i][a[i]][0][j+a[i]]%=mod;
dp[i][a[i]][0][j+a[i]]+=dp[i-1][k][1][j];
dp[i][a[i]][0][j+a[i]]%=mod;
}
for(int k=a[i]+1;k<=40;++k){
dp[i][a[i]][1][j+a[i]]+=dp[i-1][k][0][j];
dp[i][a[i]][1][j+a[i]]%=mod;
}
}
}
else {
for(a[i]=0;a[i]<=40;++a[i]){
for(int j=a[i]*(i-1);j<=1600-a[i];++j){
for(int k=0;k<=a[i];++k){
dp[i][a[i]][0][j+a[i]]+=dp[i-1][k][0][j];
dp[i][a[i]][0][j+a[i]]%=mod;
dp[i][a[i]][0][j+a[i]]+=dp[i-1][k][1][j];
dp[i][a[i]][0][j+a[i]]%=mod;
}
for(int k=a[i]+1;k<=40;++k){
dp[i][a[i]][1][j+a[i]]+=dp[i-1][k][0][j];
dp[i][a[i]][1][j+a[i]]%=mod;
}
}
}
}
}
int ans=0;
for(int i=0;i<=40;++i){
for(int j=0;j<=40*n;++j){
ans+=dp[n][i][0][j];
ans%=mod;
ans+=dp[n][i][1][j];
ans%=mod;
}
}
printf("%lld\n",ans);
}