uoj#204 "APIO2016" Boat

Source: Internet
Author: User

Time limit:70 Sec Memory limit:256 MB
submit:559 solved:248

Description in the Seoul city, the Han River traverses things. On the north bank of the Han River, a rowing school is scattered from west to east, numbered sequentially. Each school has several rowing boats. All the rowing boats in the same school have the same color, and the rowing colors of different schools differ. Rowing boats of the same color are considered to be the same. Each school can choose to send some rowing boats to participate in the festival celebrations, or choose not to send any rowing boats to participate. If the numbered schools choose to send rowing boats to participate in the celebrations, then the number of rowing boats sent can be arbitrarily selected between Ai and Bi (AI<=BI). It is worth noting that the school numbered I if choose to send a rowing boat to participate in the celebrations, then it sent the number of rowing boats must be larger than any one of the number of schools sent by the number of rowing. Enter the values of AI and bi for all schools, and find out how many possibilities are possible for a rowing boat, and must have at least one rowboat to attend the celebrations. The two cases are different if and only if there is a different number of rowing in a certain color of the celebration input

The first line includes an integer n, which indicates the number of schools. The next n rows, each line consists of two positive integers, used to describe a school. Where the first line includes the

Two positive integers represent ai,bi (1<=ai<=bi<=10^9), N<=500output

The output line, an integer, representing all the possible number of plans to send a rowing boat divided by 1,000,000,007 to get the remainder

Sample Input2
1 2
2 3Sample Output7Hintsource

Dynamic planning

Look at the puzzle x one hours, I am so weak is hopeless.

The number of enumerations is obviously weak, we have to consider the whole interval to be processed together, so first the interval discretization

To treat a ship as a number in a limited interval, set F[I][J][K] to indicate the current enumeration to the first school, the maximum number of ships in paragraph J, the number of programs selected in K.

$ f[i][j][k]=f[i-1][j][k]+f[i-1][j][k-1]* \frac {c(len[j],k)}{c(len[j ],k-1)} $

Special handling $ f[i][j][1]=f[i-1][j][1] + (\sum_{a=1}^{j-1} \sum_{k=0}^{i-1} F[i-1][a][k]) *len[j] $

The first dimension can be scrolled optimized out

The number of combinations of the first formula, numerator a bit into the (len-k+1)/k, preprocessing within 500 of the inverse can be.

The second formula is prefixed and optimized.

And then maybe it'll be over.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 #defineLL Long Long6 using namespacestd;7 Const intmod=1e9+7;8 Const intmxn=1105;9 intRead () {Ten     intx=0, f=1;CharCh=GetChar (); One      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} A      while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} -     returnx*F; - } the intINV[MXN]; - voidinit () { -inv[0]=inv[1]=1; -      for(intI=2; i<mxn;i++){ +Inv[i]= ((-mod/i) * (LL) inv[mod%i]%mod+mod)%MoD; -     } +     return; A } at intN; - intC[MXN][MXN]; - structseg{ -     intL,r; - }A[MXN]; - intpos[mxn],sz=0, LEN[MXN]; in intF[MXN][MXN],G[MXN]; - intNUM[MXN]; to intMain () { + //freopen ("boat.in", "R", stdin); - //freopen ("Boat.out", "w", stdout); the     inti,j; *n=read (); $ init ();Panax Notoginseng      for(i=1; i<=n;i++){ -A[i]. L=read ()-1; A[i]. R=read (); thePos[++sz]=a[i]. l;pos[++sz]=A[i]. R; +     } ASort (pos+1, pos+sz+1); theSz=unique (pos+1, pos+sz+1)-pos-1; + //for (i=1;i<=sz;i++) printf ("%d", pos[i]); -      for(i=1; i<=n;i++){ $A[i]. L=lower_bound (pos+1, pos+sz+1, A[i]. L)-Pos; $A[i]. R=lower_bound (pos+1, pos+sz+1, A[i]. R)-Pos; -     } -      for(i=1; i<=sz;i++) len[i]=pos[i]-pos[i-1]; thef[0][1]=1; -      for(i=1; i<=sz;i++) g[i]=1;Wuyi      for(i=1; i<=n;i++){ the          for(J=a[i]. L +1; J<=a[i]. r;j++){ -num[j]++; Wu              for(intK=num[j];k>1; k--) -f[j][k]= (LL) f[j][k]+ (LL) f[j][k-1]* (len[j]-k+1)%mod* (LL) inv[k])%MoD; Aboutf[j][1]= (LL) f[j][1]+ (LL) g[j-1]*LEN[J]%MOD)%MoD; $         } -          for(J=a[i]. L +1; j<=sz;j++){ -g[j]=g[j-1]; -              for(intk=num[j];k;k--){ Ag[j]= (LL) g[j]+f[j][k])%MoD; +             } the         } -     } $g[sz]--;if(g[sz]<=0) g[sz]+=MoD; theprintf"%d\n", G[sz]); the     return 0; the}

uoj#204 "APIO2016" Boat

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.