[Country Training team] Small Z socks (Mo team, probability)

Source: Internet
Author: User

Title Description

As a rambling person, little Z spends a lot of time every morning looking for a pair to wear from a bunch of colorful socks. Finally one day, little z can no longer endure this annoying to find socks process, so he decided to resign to fate ...

Specifically, small z to this n socks from 1 to n number, and then from the number L to R (L Although small z does not care about two socks is not a complete pair, even do not care about two socks whether a left and right, he is very concerned about the color of socks, after all, wearing two different color socks will be very embarrassing.

Your task is to tell Little Z how much he has the chance to draw two socks of the same color. Of course, Little Z wants this probability to be as high as possible, so he may ask multiple (l,r) to facilitate his choice.

However, in the case of l=r in the data, please judge this situation, output 0/1.

Input/output format

Input format:

The first line of the input file contains two positive integers n and M. n is the number of socks, M is the number of inquiries raised by small Z. The next line consists of n positive integer ci, where CI denotes the color of the sock, and the same color is represented by the same number. The next m line, two positive integer l for each line, R indicates a query.

Output format:

Contains m rows, for each query the output fraction of a line is a/b indicating the probability of randomly extracting two socks of the same color from the range [l,r] of the query. If the probability is 0 then output 0/1, otherwise the output of A/b must be the simplest fraction. (See examples)

Ideas:

Apparently a team of naked questions.

Two times the probability of taking a color is:

(A * (A-1))/((l-r+1) * (l-r) * *)

In that case, we'll spread him out.

The answer becomes(a^2+b^2+c^2+...x^2 − (R −l+1) Span class= "Mclose" >) / ( (r−l+1 ) ∗ (R −l) )

At this point, our purpose is to maintain intervals of squares of each color and

It's good to use Mo team.

Code:
#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>#defineRii Register int i#defineRij Register Int J#defineint long Longusing namespacestd;structquery{intWZ,L,R,FZ,FM;} x[50005];intn,m,sy[50005],color[50005],len,ans,sum[50005];BOOLCMP (query Lk,query KL) {if(sy[lk.l]==SY[KL.L]) {        returnlk.r<KL.R; }    returnlk.l<KL.L;}voidChangeintWzintval) {ans-=sum[color[wz]]*Sum[color[wz]]; SUM[COLOR[WZ]]+=Val; Ans+=sum[color[wz]]*Sum[color[wz]];}BOOLcmp1 (Query Lk,query KL) {returnlk.wz<Kl.wz;}intgcdintVintW) {    if(v==0)    {        returnW; }    if(w%v==0)    {        returnv; }    returnGCD (w%v,v);} Signed Main () {scanf ("%lld%lld",&n,&m); Len=sqrt (n);  for(rii=1; i<=n;i++) {scanf ("%lld",&Color[i]); Sy[i]=i/len+1; }     for(rii=1; i<=m;i++) {scanf ("%lld%lld",&x[i].l,&X[I].R); X[i].wz=i; } sort (x+1, x+m+1, CMP); intL=1, r=0;  for(rii=1; i<=m;i++)    {         while(l<X[I].L) {Change (L,-1); L++; }         while(l>X[I].L) {Change (L-1,1); L--; }         while(r<X[I].R) {Change (R+1,1); R++; }         while(r>X[I].R) {Change (R,-1); R--; }        if(x[i].l==X[I].R) {X[I].FZ=0; X[i].fm=1; Continue; } x[i].fm= (r-l+1) * (rl); X[I].FZ=ans-(r-l+1); intgcd1=gcd (x[i].fz,x[i].fm); X[I].FZ/=GCD1; X[i].fm/=GCD1; } sort (x+1, x+m+1, CMP1);  for(rii=1; i<=m;i++) {printf ("%lld/%lld\n", x[i].fz,x[i].fm); }}

[Country Training team] Small Z socks (Mo team, probability)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.