Small Z's socks (hose)Time
limit:20000MS
Memory Limit:265216KB
64bit IO Format:%lld &%ll U SubmitStatusPracticeHYSBZ 2038Appoint Description:System Crawler (2016-07-13)
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, little z numbered the n socks from 1 to N, and then from the number L to R (l though little Z doesn't care if two socks are a complete pair, even if two socks are left and right, he cares about the color of the 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 Input
6 4
1 2 3 3 3 2
2 6
1 3
3 5
1 6
Sample Output
2/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.
Ideas:
probability = A * (A-1) + b* (b-1) .../n (n-1);//Here A indicates the number of occurrences of a and the rest is similar
Simplify = a^2 + b^2 + ...-(A + B + C + ...)/n * (n-1);
Statistics according to the formula.
/** author:sweat123 * Created TIME:2016/7/14 16:47:20 * File Name:main.cpp*/#include<Set>#include<map>#include<queue>#include<stack>#include<cmath>#include<string>#include<vector>#include<cstdio>#include<time.h>#include<cstring>#include<iostream>#include<algorithm>#defineINF 1<<30#defineMOD 1000000007#definell Long Long#defineLson l,m,rt<<1#defineKey_value Ch[ch[root][1]][0]#defineRson m+1,r,rt<<1|1#definePi ACOs (-1.0)using namespacestd;Const intMAXN =50010;structnode{intL,r,id;} Q[MAXN];intN,m,a[maxn],pos[maxn];ll SON[MAXN],MOM[MAXN],ANS,NUM[MAXN];BOOLCMP (node A,node b) {if(POS[A.L] = = POS[B.L])returnA.R <B.R; returnPOS[A.L] <POS[B.L];}voidUpdata (intXintval) {ans-= (1LL * Num[a[x]] *Num[a[x]]); NUM[A[X]]+=Val; Ans+ = (1LL * Num[a[x]] *Num[a[x]]);}intgcd (ll x,ll y) {if(Y = =0)returnx; returnGCD (y,x%y);}intMain () { while(~SCANF ("%d%d",&n,&m)) { intTP =ceil (sqrt (n)); for(inti =1; I <= N; i++) {scanf ("%d",&A[i]); Pos[i]= (I-1)/TP;//The position of i-th} memset (num,0,sizeof(num)); for(inti =1; I <= m; i++) {scanf ("%d%d",&q[i].l,&Q[I].R); Q[i].id=i; } sort (Q+1, q+m+1, CMP); intPL,PR; PL=1, PR =0; Ans=0; for(inti =1; I <= m; i++){ if(Q[I].L = =Q[I].R) {Son[q[i].id]=0; Mom[q[i].id]=1; Continue; } Else{ if(PR <Q[I].R) { for(intj = pr +1; J <= Q[I].R; J + +) {Updata (J,1); } PR=Q[I].R; } Else{ for(intj = PR; J > q[i].r; j--) {Updata (J,-1); } PR=Q[I].R; } if(PL <Q[I].L) { for(intj = PL; J < Q[i].l; J + +) {Updata (J,-1); } PL=Q[I].L; } Else{ for(intj = PL-1; J >= Q[i].l; j--) {Updata (J,1); } PL=Q[I].L; } ll TP1= ans-(Q[I].R-Q[I].L +1); ll TP2= 1LL * (Q[I].R-Q[I].L +1) * (Q[I].R-Q[I].L); LL BF=gcd (TP1,TP2); Son[q[i].id]= tp1/F; Mom[q[i].id]= tp2/BF; //cout<<q[i].id<< ' <<tp1/bf<< ' <<tp2/bf<< ' <<ans<<endl; } } for(inti =1; I <= m; i++) {printf ("%lld/%lld\n", Son[i],mom[i]); } } return 0;}
HYSBZ 2038 MO Team algorithm