Codeforces Round #266 (Div. 2)D(DP)

D. Increase Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Peter has a sequence of integers a1, a2, ..., an. Peter wants all numbers in the sequence to equal h. He can perform the operation of "adding one on the segment [l, r]": add one to all elements of the sequence with indices from l to r (inclusive). At that, Peter never chooses any element as the beginning of the segment twice. Similarly, Peter never chooses any element as the end of the segment twice. In other words, for any two segments [l1, r1] and [l2, r2], where Peter added one, the following inequalities hold: l1 ≠ l2 and r1 ≠ r2.

How many distinct ways are there to make all numbers in the sequence equal h? Print this number of ways modulo 1000000007 (109 + 7). Two ways are considered distinct if one of them has a segment that isn't in the other way.

Input

The first line contains two integers n, h (1 ≤ n, h ≤ 2000). The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 2000).

Output

Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

Sample test(s)
input
3 2
1 1 1
output
4
input
5 1
1 1 1 1 1
output
1
input
4 3
3 2 1 1
output
0

题意:RT

思路:DP[i][j]表示到第i个位置,前面还有j个点未被选(不包括i)

            转移有两种情况

           1. a[i]+j==h  表示可以从前面j个点中任意选一个点与i匹配或者不选
 
           2. a[i]+j+1==h 表示可以从前面j个点以及i中任意选一个点与后面的点匹配或者不选

           具体看代码吧~~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
using namespace std;

typedef long long ll;
const int MOD = 1000000007;
const int MAXN = 2010;
int dp[MAXN][MAXN];
int a[MAXN];
int n,h;

int dfs(int i,int k)
{
    if(dp[i][k])return dp[i][k];
    if(i==n)return dp[i][k]= k==0 ?  1 : 0;
    int res=0;
    if(a[i]+k==h)res=((ll)dfs(i+1,k-1)*k % MOD+dfs(i+1,k)) % MOD;
    else if(a[i]+k+1==h)res=((ll)dfs(i+1,k)*(k+1) % MOD + dfs(i+1,k+1) ) %MOD;
    return dp[i][k]=res;
}

int main()
{
    scanf("%d%d",&n,&h);
    for(int i=0;i
       
       
         h){ printf("0\n"); return 0; } } printf("%d\n",dfs(0,0)); return 0; } 
       
      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值