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.
Input
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
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)
Sample INPUT6 4
1 2 3 3 3 2
2 6
1 3
3 5
1 6Sample OUTPUT2/5
0/1
1/1
4/15
"Sample Interpretation"
Inquiry 1: Total C (5,2) = 10 possible, of which two 2 are extracted 1 possible, extract two 3 has 3 possible, the probability is (1+3)/10=4/10=2/5.
Question 2: Total C (3,2) = 3 possible, can not draw the same color socks, the probability is 0/3=0/1.
Inquiry 3: Total C (3,2) = 3 possible, are extracted two 3, the probability is 3/3=1/1.
Note: The above C (A, b) represents the number of combinations, the combination of C (A, B) is equivalent to the selection of B in a different item number of selection scheme.
"Data size and conventions"
30% of the data are n,m≤5000;
60% of the data are n,m≤25000;
100% of the data in N,m≤50000,1≤l < R≤n,ci≤n. Http://blog.csdn.net/bossup/article/details/39236275code:
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 #defineMAXN 500057 using namespacestd;8typedefLong LongInt64;9 Charch;Ten intN,m,a[maxn],cnt[maxn],pos[maxn],siz; One Int64 tmp; AInt64 gcd (Int64 A,int64 b) {return(B?GCD (b,a%b): a);} - structfenshu{ - Int64 FZ,FM; the voidHuajian () { -Int64 d=gcd (FZ,FM); -Fz/=d,fm/=D; - } + }ANS[MAXN]; - structquery{ + intL,r,id; A }LIST[MAXN]; at BOOLcmp (query A,query b) { - if(POS[A.L]!=POS[B.L])returnpos[a.l]<POS[B.L]; - returnpos[a.r]<POS[B.R]; - } - BOOLOK; - voidReadint&x) { in for(ok=0, Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') ok=1; - for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); to if(OK) x=-x; + } - voidModifyintColintop) { thetmp-= (1ll*cnt[col]*Cnt[col]); *cnt[col]+=op; $tmp+= (1ll*cnt[col]*Cnt[col]);Panax Notoginseng } - voidWork () { the intL=1, r=0; + for(intI=1; i<=m;i++){ A for(; r<list[i].r;r++) Modify (a[r+1],1); the for(; r>list[i].r;r--) Modify (a[r],-1); + for(; l<list[i].l;l++) Modify (a[l],-1); - for(; l>list[i].l;l--) Modify (a[l-1],1); $Ans[list[i].id]= (Fenshu) {tmp-(r-l+1), 1ll* (r-l+1) * (rl)}; $ Ans[list[i].id].huajian (); - } - } the intMain () { -Read (n), read (m), siz= (int) sqrt (n);Wuyi for(intI=1; i<=n;i++) read (A[i]); the for(intI=1; i<=n;i++) pos[i]=i/siz; - for(intI=1; i<=m;i++) Read (LIST[I].L), read (LIST[I].R), list[i].id=i; WuSort (list+1, list+m+1, CMP); - Work (); About for(intI=1; i<=m;i++) printf ("%lld/%lld\n", ans[i].fz,ans[i].fm); $ return 0; -}
bzoj2038: [2009 countries Training team] small Z socks (hose)