#include <bits/stdc++.h>
#define ll long long
#define N 1000001
using namespace std;
const ll mod=round(1e9)+7;
ll gt[N],ng[N],sum,f[2001][2001];
long long C(int k,int n)
{
if(k>n) return 0;
return gt[n]*ng[k]%mod*ng[n-k]%mod;
}
int i,j,k,n,m;
struct pt { int l,r; };
pt b[N],a[N];
set <int> s;
ll lt(ll a,ll b)
{
if(b==0) return 1;
ll tg=lt(a,b/2);
if(b%2==0) return tg*tg%mod;
return tg*tg%mod*a%mod;
}
int main()
{
// freopen("ntu.inp","r",stdin);
//freopen("ntu.out","w",stdout);
sum=0;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i].l>>a[i].r;
s.insert(a[i].l); s.insert(a[i].r);
sum+=a[i].r-a[i].l+1;
}
gt[0]=1;
for(i=1;i<=sum;i++) gt[i]=gt[i-1]*i%mod;
ng[sum]=lt(gt[sum],mod-2);
for(i=sum-1;i>=0;i--) ng[i]=ng[i+1]*(i+1)%mod;
for(set <int>::iterator it=s.begin();it!=s.end();it++)
{
b[++m]={ (*it),(*it) };
set <int>::iterator it2=it; it2++;
if(it2!=s.end() && (*it)+1<(*it2)) b[++m]={ (*it)+1,(*it2)-1 };
}
for(i=0;i<=n;i++) f[i][0]=1;
for(i=0;i<=n;i++)
for(j=1;j<=m;j++)
{
f[i][j]=(f[i][j-1]+f[i-1][j]-f[i-1][j-1])%mod;
for(k=i;i-k<=b[j].r-b[j].l && k>=1;k--)
{
if(a[k].r<b[j].l || a[k].l>b[j].r) break;
f[i][j]=(f[i][j]+f[k-1][j-1]*C(i-k+1,b[j].r-b[j].l+1))%mod;
}
// cerr<<i<<" "<<j<<" "<<f[i][j]<<'\n';
}
cout<<(f[n][m]-1+mod)%mod;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
6400 KB |
Output is correct |
2 |
Correct |
12 ms |
6272 KB |
Output is correct |
3 |
Correct |
12 ms |
6272 KB |
Output is correct |
4 |
Correct |
11 ms |
6272 KB |
Output is correct |
5 |
Correct |
12 ms |
6272 KB |
Output is correct |
6 |
Correct |
12 ms |
6272 KB |
Output is correct |
7 |
Correct |
12 ms |
6272 KB |
Output is correct |
8 |
Correct |
11 ms |
6272 KB |
Output is correct |
9 |
Correct |
12 ms |
6272 KB |
Output is correct |
10 |
Correct |
12 ms |
6272 KB |
Output is correct |
11 |
Correct |
12 ms |
6272 KB |
Output is correct |
12 |
Correct |
11 ms |
6272 KB |
Output is correct |
13 |
Correct |
12 ms |
6400 KB |
Output is correct |
14 |
Correct |
11 ms |
6272 KB |
Output is correct |
15 |
Correct |
12 ms |
6400 KB |
Output is correct |
16 |
Correct |
7 ms |
3072 KB |
Output is correct |
17 |
Correct |
7 ms |
3072 KB |
Output is correct |
18 |
Correct |
7 ms |
3072 KB |
Output is correct |
19 |
Correct |
7 ms |
3072 KB |
Output is correct |
20 |
Correct |
7 ms |
3072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
6400 KB |
Output is correct |
2 |
Correct |
12 ms |
6272 KB |
Output is correct |
3 |
Correct |
12 ms |
6272 KB |
Output is correct |
4 |
Correct |
11 ms |
6272 KB |
Output is correct |
5 |
Correct |
12 ms |
6272 KB |
Output is correct |
6 |
Correct |
12 ms |
6272 KB |
Output is correct |
7 |
Correct |
12 ms |
6272 KB |
Output is correct |
8 |
Correct |
11 ms |
6272 KB |
Output is correct |
9 |
Correct |
12 ms |
6272 KB |
Output is correct |
10 |
Correct |
12 ms |
6272 KB |
Output is correct |
11 |
Correct |
12 ms |
6272 KB |
Output is correct |
12 |
Correct |
11 ms |
6272 KB |
Output is correct |
13 |
Correct |
12 ms |
6400 KB |
Output is correct |
14 |
Correct |
11 ms |
6272 KB |
Output is correct |
15 |
Correct |
12 ms |
6400 KB |
Output is correct |
16 |
Correct |
7 ms |
3072 KB |
Output is correct |
17 |
Correct |
7 ms |
3072 KB |
Output is correct |
18 |
Correct |
7 ms |
3072 KB |
Output is correct |
19 |
Correct |
7 ms |
3072 KB |
Output is correct |
20 |
Correct |
7 ms |
3072 KB |
Output is correct |
21 |
Incorrect |
35 ms |
22008 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
28 ms |
16384 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
6400 KB |
Output is correct |
2 |
Correct |
12 ms |
6272 KB |
Output is correct |
3 |
Correct |
12 ms |
6272 KB |
Output is correct |
4 |
Correct |
11 ms |
6272 KB |
Output is correct |
5 |
Correct |
12 ms |
6272 KB |
Output is correct |
6 |
Correct |
12 ms |
6272 KB |
Output is correct |
7 |
Correct |
12 ms |
6272 KB |
Output is correct |
8 |
Correct |
11 ms |
6272 KB |
Output is correct |
9 |
Correct |
12 ms |
6272 KB |
Output is correct |
10 |
Correct |
12 ms |
6272 KB |
Output is correct |
11 |
Correct |
12 ms |
6272 KB |
Output is correct |
12 |
Correct |
11 ms |
6272 KB |
Output is correct |
13 |
Correct |
12 ms |
6400 KB |
Output is correct |
14 |
Correct |
11 ms |
6272 KB |
Output is correct |
15 |
Correct |
12 ms |
6400 KB |
Output is correct |
16 |
Correct |
7 ms |
3072 KB |
Output is correct |
17 |
Correct |
7 ms |
3072 KB |
Output is correct |
18 |
Correct |
7 ms |
3072 KB |
Output is correct |
19 |
Correct |
7 ms |
3072 KB |
Output is correct |
20 |
Correct |
7 ms |
3072 KB |
Output is correct |
21 |
Incorrect |
35 ms |
22008 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |