Victor and Toys
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 467 Accepted Submission(s): 157
Problem Description
Victor has
n
toys, numbered from
1
to
n
. The beauty of the
i
-th toy is
wi
.
Victor has a sense of math and he generates m intervals, the i -th interval is [li,ri] . He randomly picks 3 numbers i,j,k(1≤i<j<k≤m) , and selects all of the toys whose number are no less than max(li,lj,lk) and no larger than min(ri,rj,rk) . Now he wants to know the expected sum of beauty of the selected toys, can you help him?
Victor has a sense of math and he generates m intervals, the i -th interval is [li,ri] . He randomly picks 3 numbers i,j,k(1≤i<j<k≤m) , and selects all of the toys whose number are no less than max(li,lj,lk) and no larger than min(ri,rj,rk) . Now he wants to know the expected sum of beauty of the selected toys, can you help him?
Input
The first line of the input contains an integer
T
, denoting the number of test cases.
In every test case, there are two integers n and m in the first line, denoting the number of the toys and intervals.
The second line contains n integers, the i -th integer wi denotes that the beauty of the i -th toy.
Then there are m lines, the i -th line contains two integers li and ri .
1≤T≤10 .
1≤n,m≤50000 .
1≤wi≤5 .
1≤li≤ri≤n .
In every test case, there are two integers n and m in the first line, denoting the number of the toys and intervals.
The second line contains n integers, the i -th integer wi denotes that the beauty of the i -th toy.
Then there are m lines, the i -th line contains two integers li and ri .
1≤T≤10 .
1≤n,m≤50000 .
1≤wi≤5 .
1≤li≤ri≤n .
Output
Your program should print
T
lines : the
i
-th of these denotes the answer of the
i
-th case.
If the answer is an integer, just print a single interger, otherwise print an irreducible fraction like p/q .
If the answer is an integer, just print a single interger, otherwise print an irreducible fraction like p/q .
Sample Input
1 3 4 1 1 5 2 3 1 3 3 3 1 1
Sample Output
5/4
Source
问题描述
Victor有n个玩具,编号为1到n,其中编号为i的玩具有wi点有趣值。 Victor对数字特别敏感,他在头脑中生成了m个区间,其中第i个区间为[li,ri],并随机选取了3个互不相同的数i,j,k(1≤i<j<k≤m),将所有满足max(li,lj,lk)≤x≤min(ri,rj,rk)的编号为x的玩具取出,他想知道他取出的玩具的有趣值之和的期望是多少,你能帮帮他吗?
输入描述
第一行包含一个整数T,表示测试数据的组数。 每组测试数据的第一行有两个整数n, m,表示玩具的个数和区间的个数。 接下来一行有n个数,其中第i个数为wi。 接下来m行,每行两个整数,其中第i行表示li, ri。 1≤T≤10。 1≤n,m≤50000。 1≤wi≤5。 1≤li≤ri≤n。
输出描述
每组测试数据输出一行,若答案为整数,则输出一个整数,否则输出形如p/q的最简分数。
输入样例
1 3 4 1 1 5 2 3 1 3 3 3 1 1
输出样例
5/4
解题思路:和的期望 = (∑任意一种挑选方式的和x)/(满足i<=j<=k的挑选方式的总数y) = x / y 。分母y 就是m个数中取3个数的组合C(m,3),即y = m*(m-1)*(m-2)/6 ,对于分子x,如果第i个数出现的区间数是num(简言之就是有num个区间包含第i个数),那么第i个数对分子的贡献值是w[i]*C(num,3)。因此枚举i,累加和。(预处理过程与HDU 5124 line 类似)
代码如下:
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#include <ctime>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
int w[50005],dp[50005];
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
ll i,j,k,n,m,l,r,t;
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d%I64d",&n,&m);
for(i=1;i<=n;i++)
scanf("%I64d",&w[i]);
memset(dp,0,sizeof(dp));
for(i=0;i<m;i++)
{
scanf("%I64d%I64d",&l,&r);
dp[l]++;
dp[r+1]--;
}
ll x=0;
ll num=0;
for(i=1;i<=n;i++)
{
num+=dp[i];
if(num>=3)
x+=num*(num-1)*(num-2)/6*w[i];
}
ll y=m*(m-1)*(m-2)/6;
//printf("x=%I64d y=%I64d\n",x,y);
ll d=gcd(x,y);
if(d)
{
x=x/d;
y=y/d;
if(y==1)
printf("%I64d\n",x);
else
printf("%I64d/%I64d\n",x,y);
}
else
printf("0\n");
}
return 0;
}