Topic Link: Rowing
Reason good boat why to translate into rowing ... is not also rowing in the surface ...
Consider DP for this problem. As the number of rowing boats is too large, it must not be recorded directly in the DP state. So one idea is to disperse the numbers and record the number of each school. That is to say \ (F_{i,j,k}\) represents the former \ (i\) school, the number of rowing boats sent by the I\ school is within the interval \ (j\), and the interval \ (j\) has a total of (k\) the number of programs in the school. Then the classification discusses the transfer:
When \ (K\ne 1\), there are:
\begin{aligned}
F_{i,j,k} &=\frac{c (len_j,k)}{c (len_j,k-1)}\sum_{i ' =0}^{i-1}f_{i ', j,k-1} \ \
&=\frac{len_j-k+1}{k}\sum_{i ' =0}^{i-1}f_{i ', j,k-1}
\end{aligned}
When \ (k=1\), there are:
\begin{aligned}
F_{i,j,k}=\sum_{i ' =0}^{i-1}\sum_{j ' =0}^{j-1}\sum_{k ' =0}^{i '}f_{i ', J ', K '}
\end{aligned}
So the prefix and optimization can be. It can be found that logging two prefixes and after \ (f\) arrays is not necessary to record. Time complexity \ (o (n^3) \), spatial complexity \ (o (n^2) \).
Paste the code below (the constant is slightly larger):
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #define FILE (s) freopen (S ".", "R", stdin), Freopen (S ". Out", "w", stdout) #define N 510#define MoD 1000000007using namespace Std;typedef Long long llg;int le[n],ri[n],n,ni[n],d[n<<1],ld;llg s1[n][n<<1],s2 [N<<1] [N];void gi (int &x) {x=lower_bound (d+1,d+ld+1,x)-D;} int mi (llg a,int b) {llg s=1;while (b) {if (b&1) s=s*a%mod;a=a*a%mod; b>>=1;} return s;} int main () {File ("a"), scanf ("%d", &n), ni[0]=1;for (int i=1;i<=n;i++) {scanf ("%d%d", &le[i],&ri[i]);d [+ +ld]=le[i],d[++ld]=++ri[i];ni[i]=mi (i,mod-2);} Sort (d+1,d+ld+1); Ld=unique (d+1,d+ld+1)-d-1;for (int i=1;i<=n;i++) GI (le[i]), GI (Ri[i]), for (int i=0;i<=ld;i++) s1[0][i]=1;for ( int i=1,x,y;i<=n;i++) {for (int. j=ri[i]-1;x=d[j+1]-d[j],j>=le[i];j--) {for (int k=min (x,i); k>1;k--) {y=s2[j][ k-1]*ni[k]%mod* (x-k+1)%mod; (s1[i][j]+=y)%=mod, (s2[j][k]+=y)%=mod;} Y=s1[i-1][j-1]*x%mod; (s1[i][j]+=y)%=mod, (s2[j][1]+=y)%=mod;} s1[i][0]=1;for (int j=1;j<=ld;j++) {s1[i][j]+=s1[i-1][j]+s1[i][j-1];(s1[i][j]-=s1[i-1][j-1])%=mod;if (S1[i][j] <0) S1[i][j]+=mod;}} printf ("%lld", (s1[n][ld]+mod-1)%mod); return 0;}
Bzoj 4584 "APIO2016" rowing