小Z的袜子(hose)

来源:互联网 发布:网络赌钱软件 编辑:程序博客网 时间:2024/05/02 06:07

Description

作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……
具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬。
你的任务便是告诉小Z,他有多大的概率抽到两只颜色相同的袜子。当然,小Z希望这个概率尽量高,所以他可能会询问多个(L,R)以方便自己选择。

Input

输入文件第一行包含两个正整数N和M。N为袜子的数量,M为小Z所提的询问的数量。接下来一行包含N个正整数Ci,其中Ci表示第i只袜子的颜色,相同的颜色用相同的数字表示。再接下来M行,每行两个正整数L,R表示一个询问。

Output

包含M行,对于每个询问在一行中输出分数A/B表示从该询问的区间[L,R]中随机抽出两只袜子颜色相同的概率。若该概率为0则输出0/1,否则输出的A/B必须为最简分数。(详见样例)

Sample Input

6 41 2 3 3 3 22 61 33 51 6

Sample Output

2/50/11/14/15【样例解释】询问1:共C(5,2)=10种可能,其中抽出两个2有1种可能,抽出两个3有3种可能,概率为(1+3)/10=4/10=2/5。询问2:共C(3,2)=3种可能,无法抽到颜色相同的袜子,概率为0/3=0/1。询问3:共C(3,2)=3种可能,均为抽出两个3,概率为3/3=1/1。注:上述C(a, b)表示组合数,组合数C(a, b)等价于在a个不同的物品中选取b个的选取方案数。【数据规模和约定】30%的数据中 N,M ≤ 5000;60%的数据中 N,M ≤ 25000;

100%的数据中 N,M ≤ 50000,1 ≤ L < R ≤ N,Ci ≤ N。

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <queue>#include <math.h>#include <set>#include <iomanip>using namespace std;#define eqs 1e-9#define INF 0x3f3f3f3f#define MOD 100000000#define MAXN 100010#define ll long longint n,m,nn;ll ans;int num[MAXN];ll sum[MAXN];struct point{    ll x;    ll y;}cnt[MAXN];struct node{    int l,r,id;    bool operator <(const node b)const    {        return (l/nn!=b.l/nn)?l<b.l:r<b.r;    }}p[MAXN];void updata(int x,ll val){    ans -= (sum[num[x]]*sum[num[x]]);    sum[num[x]] += val;    ans += (sum[num[x]]*sum[num[x]]);}ll gcd(ll x,ll y){    return y==0?x:gcd(y,x%y);}int main(){    while( ~scanf("%d %d",&n,&m) )    {        for( int i = 1; i <= n; i++ )            scanf("%d",&num[i]);        for( int i = 0; i < m; i++ )        {            scanf("%d %d",&p[i].l,&p[i].r);            p[i].id = i;        }        nn = ceil(sqrt(n));        sort(p,p+m);        memset(sum,0,sizeof(sum));        int l = 1,r = 0;        ans = 0;        for( int i = 0; i < m; i++ )        {            if( p[i].l==p[i].r )            {                cnt[p[i].id].x = 0;                cnt[p[i].id].y = 1;                continue;            }            while( p[i].r > r )            {                r++;                updata(r,1);            }            while( p[i].r < r )            {                updata(r,-1);                r--;            }            while( p[i].l > l )            {                updata(l,-1);                l++;            }            while( p[i].l < l )            {                l--;                updata(l,1);            }            ll x = ans-(r-l+1);            ll y = (ll)(r-l+1)*(r-l);            ll g = gcd(x,y);            cnt[p[i].id].x = x/g;            cnt[p[i].id].y = y/g;        }        for( int i = 0; i < m; i++ )        {            printf("%lld/%lld\n",cnt[i].x,cnt[i].y);        }    }    return 0;}



0 0
原创粉丝点击