51nod-1296: a restricted arrangement

Source: Internet
Author: User

"Portal: 51nod-1296" Brief test Instructions:

There is a collection of 1 to N of the number in the collection

Given a constraint, A[i] indicates that the number of positions in the a[i] is smaller than the number of adjacent positions

Given the b constraint, B[i] indicates that the number of positions in the b[i] is greater than the number of adjacent positions

Find the number of sequences that match a condition

The following:

Dp

Set F[I][J] is the i digit, the last one is the number of cases of J

We have to put J reasonable at the end, so we can put the number of the front >=j +1, so we can empty the number of J.

Set P[i] for the first position of the constraints, P[i]=-1 indicates that the first position is less than the i-1 position, P[i]=1 is greater than, p[i]=0 means no restriction conditions

In order to be able to better transfer, set $s[j]=\sum_{k=1}^{j}f[i-1][k]$

Can be transferred in three kinds of situations

Reference Code:
#include <cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespaceStd;typedefLong LongLL;intp[5100];intf[5100][5100],s[5100];intmod=1e9+7;intMain () {intn,k,l; scanf ("%d%d%d",&n,&k,&l); BOOLbk=false;  for(intI=1; i<=k;i++)    {        intx; scanf ("%d", &x); x + +; if(p[x]==1) bk=true; P[X]=-1; if(x+1>n)Continue; if(p[x+1]==-1) bk=true; P[x+1]=1; }     for(intI=1; i<=l;i++)    {        intx; scanf ("%d", &x); x + +; if(p[x]==-1) bk=true; P[X]=1; if(x+1>n)Continue; if(p[x+1]==1) bk=true; P[x+1]=-1; }    if(bk==true) {printf ("0\n");return 0;} f[1][1]=1;  for(intI=2; i<=n;i++)    {         for(intj=1; j<i;j++) s[j]= (LL) s[j-1]+ (LL) f[i-1][J])%Mod;  for(intj=1; j<=i;j++)        {            if(p[i]==1) f[i][j]=s[j-1]; Else if(p[i]==-1) f[i][j]= (LL) s[i-1]-(LL) s[j-1]+ (LL) Mod)%Mod; Elsef[i][j]=s[i-1]; }    }    intans=0;  for(intI=1; i<=n;i++) ans= (LL) ans+ (LL) f[n][i])%Mod; printf ("%d\n", ans); return 0;}

51nod-1296: a restricted arrangement

Related Article

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.