# 【洛谷p3643】[APIO2016]划艇

## 题解

$f_{0,1} = 1$

$f_{i,j} = j∈I_i ? \sum_{k=1}^{j-1}\sum_{p=0}^{i-1}f_{p,k}:0$

$f_{i,j}=\sum_{k=1}^{j-1} \sum_{p=0}^{i-1}\binom{l+M-1}{M}f_{p,k}$

## 代码

#include <bits/stdc++.h>

const int maxn = 510;
const int mod = 1e9 + 7;
typedef long long ll;
using std::sort;

int n, m, i, j, k, N;
int l[maxn], r[maxn], a[maxn << 1];
int inv[maxn], c[maxn], f[maxn];

inline void get_inv(int n) {
inv[1] = 1;
for(int i = 2;i <= n;i++)
inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
return;
}
inline int add(ll a,ll b) {
ll c = a + b;
return c >= mod ? c - mod : c;
}

int main() {
scanf("%d",&N);
get_inv(N);
for(int i = 1;i <= N;i++)
scanf("%d %d",l + i,r + i), a[++n] = l[i], a[++n] = r[i] + 1;
sort(a + 1,a + n + 1);
n = std::unique(a + 1,a + n + 1) - a - 1;
for(int i = 1;i <= N;i++)
l[i] = std::lower_bound(a + 1,a + n + 1,l[i]) - a,
r[i] = std::lower_bound(a + 1,a + n + 1,r[i] + 1) - a;
c[0] = f[0] = 1;
for(int j = 1;j < n;j++) {
int le = a[j + 1] - a[j];
for(int i = 1;i <= N;i++)
c[i] = 1ll * c[i - 1] * (le + i - 1) % mod * inv[i] % mod;
for(int i = N;i >= 1;i--) {
if(l[i] <= j && j + 1 <= r[i]) {
int o = 0, p = 1, t = le;
for(int k = i - 1;~k;k--) {
o = add(1ll * o,1ll * t * f[k] % mod);
if(l[k] <= j && j + 1 <= r[k])
t = c[++p];
}