Codeforces 466 D. Increase Sequence, codeforcesincrease

Source: Internet
Author: User

Codeforces 466 D. Increase Sequence, codeforcesincrease


A good idea .....

D. Increase Sequencetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Peter has a sequence of integersA1, bytes,A2, middle..., middle ,...,AN. Peter wants all numbers in the sequence to equalH. He can perform the operation of "adding one on the segment [L, Bytes,R] ": Add one to all elements of the sequence with indices fromLToR(Aggressive ). 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, bytes,R1] and [L2, bytes,R2], where Peter added one, the following inequalities hold:L1 bytes = bytesL2 andR1 bytes = bytesR2.

How many distinct ways are there to make all numbers in the sequence equalH? Print this number of ways modulo 1000000007 (109 seconds + limit 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 integersN, Bytes,H(1 digit ≤ DigitN, Bytes,HMemory ≤ memory 2000). The next line containsNIntegersA1, bytes,A2, middle..., middle ,...,AN(0 bytes ≤ bytesAILimit ≤ limit 2000 ).

Output

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

Sample test (s) input
3 21 1 1
Output
4
Input
5 11 1 1 1 1
Output
1
Input
4 33 2 1 1
Output
0

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long int LL;const LL maxn=2200,MOD=1e9+7;LL a[maxn],b[maxn];int main(){int n,h;cin>>n>>h;for(int i=1;i<=n;i++) {cin>>a[i];a[i]=h-a[i];}for(int i=1;i<=n+1;i++){b[i]=a[i]-a[i-1];}LL ans=1,cnt=0;for(int i=1;i<=n+1;i++){if(b[i]==1){cnt++;}else if(b[i]==0){if(cnt) ans=(ans*(cnt+1))%MOD;}else if(b[i]==-1){ans=(ans*cnt)%MOD;cnt--;}else ans=0;}cout<<ans<<endl;return 0;}










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.