# 【HDU 6410】序列期望 百度之星复赛B （数学期望）

## 代码

#include <bits/stdc++.h>
using namespace std;

const int maxn=233, moder=int(1e9)+7;
inline int mul(int a,int b) {return 1ll*a*b%moder;}
inline int add(int a,int b) {return (a+b<moder)?(a+b):(a+b-moder);}
inline int les(int a,int b) {return (a<b)?(a-b+moder):(a-b);}

int n;
int l[maxn], r[maxn];

int fpow(int a,int k) {
int res=1;
for(;k;k>>=1,a=mul(a,a)) if(k&1) res=mul(res,a);
return res;
}

void work() {
scanf("%d",&n);
register int i,j;

int pro=1, maxx=0;
for(i=1;i<=n;++i) {
scanf("%d%d",&l[i],&r[i]);
pro=mul(pro,(r[i]-l[i]+1));
maxx=max(maxx,r[i]);
}

int ret=0;
for(i=1;i<=maxx;++i) {
int pre=1, cur=1;
for(j=1;j<=n;++j) {
int x=l[j], y=min(r[j],i);
int a=i-x+1, b=i-y+1;
if(a<b) cur=0;
else cur=mul(cur,(b+a)*(a-b+1)/2%moder);

x=l[j], y=min(r[j],i-1);
a=i-x+1, b=i-y+1;
if(a<b) pre=0;
else pre=mul(pre,(b+a)*(a-b+1)/2%moder);
}
}

ret=mul(ret,fpow(pro,moder-2));
cout<<ret<<endl;

return;
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
#endif
int T;
for(scanf("%d",&T);T;T--)
work();

return 0;
}

04-30 248
07-25 387
05-06 1434