[HRBUSTOJ1476] Pairs (FFT)

Source: Internet
Author: User

Title Link: http://acm-software.hrbust.edu.cn/problem.php?id=1476

Test instructions: Give n number, M inquiry, ask a K each time. Ask the number of the n number of two and strictly less than k pairs.

According to the input sample, nothing is required:

f = CNT (1 2 3 4 5) T * (x) (where (x) represents a vector of 1,x,x^2 ..., CNT indicates the number of occurrences of these numbers, T means transpose) squared once corresponds to the coefficient before the X power, taking the first k-1 coefficient and is the answer. Complexity is too high, O (n^2) coefficient pointers is not possible, so need to make a point value expression DFT and then idft back.

1#include <bits/stdc++.h>2 using namespacestd;3 4 Const DoublePI = ACOs (-1.0);5typedefstructComplex {6     Doubler,i;7Complex (Double_r =0.0,Double_i =0.0) {8R = _r; i =_i;9     }TenComplexoperator+(ConstComplex &b) { One         returnComplex (r+b.r,i+B.I); A     } -Complexoperator-(ConstComplex &b) { -         returnComplex (r-b.r,i-B.I); the     } -Complexoperator*(ConstComplex &b) { -         returnComplex (r*b.r-i*b.i,r*b.i+i*B.R); -     } + }complex; -  + voidChange (Complex y[],intLen) { A     inti,j,k; at      for(i =1, j = len/2; I < len-1; i++) { -         if(I <j) Swap (Y[i],y[j]); -K = len/2; -          while(J >=k) { -J-=K; -K/=2; in         } -         if(J < k) J + =K; to     } + } -  the voidFFT (Complex y[],intLenintOn ) { * Change (Y,len); $      for(inth =2; H <= Len; H <<=1) {Panax NotoginsengComplex wn (cos (-on*2*pi/h), sin (-on*2*pi/h)); -          for(intj =0; J < len;j+=h) { theComplex W (1,0); +              for(intK = J;k < j+h/2; k++) { AComplex U =Y[k]; theComplex T = w*y[k+h/2]; +Y[k] = u+T; -y[k+h/2] = uT; $W = w*WN; $             } -         } -     } the     if(On =-1) { -          for(inti =0; I < len;i++) {WuyiY[I].R/=Len; the         } -     } Wu } -  About Const intMAXN =400040; $typedefLong LongLL; - intA[MAXN]; - Complex C[MAXN]; - LL F[MAXN]; A intN, M; +  the intMain () { -     //freopen ("in", "R", stdin); $     intT; thescanf"%d", &T); the      while(t--) { theMemset (F,0,sizeof(f)); thescanf"%d%d",&n,&m); -         intMaxx =-1; in          for(inti =0; I < n; i++) { thescanf"%d", &a[i]); theMaxx =Max (A[i], Maxx); Aboutf[a[i]]++; the         } the         intLen1 = Maxx +1; the         intLen =1; +          while(Len <2* len1) Len <<=1; -          for(inti =0; i < Len; i++) C[i] = Complex (0,0); the          for(inti =0; i < len1; i++) C[i] = Complex (F[i],0);BayiFFT (c, Len,1); the          for(inti =0; i < Len; i++) C[i] = c[i] *C[i]; theFFT (c, Len,-1); -          for(inti =0; i < Len; i++) F[i] = (LL) (C[I].R +0.5); -Len =2*Maxx; the          for(inti =0; I < n; i++) f[a[i]*2]--; the          for(inti =1; I <= Len; i++) { theF[i]/=2; theF[i] + = f[i-1]; -         } the          while(m--) { the             intK; thescanf"%d", &k);94printf"%lld\n", f[k-1]); the         } the     } the     return 0;98}

[HRBUSTOJ1476] Pairs (FFT)

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.