Uvalive_6886_golf Bot (FFT fast Fourier transform)

Source: Internet
Author: User
Tags new set

Transmission door: Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=77958#problem/C


Types of questions: number theory


The following:

In set a, 1 or 2 numbers are taken, and they become a new set of S. Q-time query, each query input a number, Q number of how many numbers belong to the set S.

Set a element number 2e5,q<=2e5,a element size <=2e5, query number 2e5


Analysis:

Violence Enumeration O (C (n,2) +n), O (n^2) is too complex.

The number of FFT statistics appear, and then directly calculate the result is OK, complexity O (NLOGN).

Only 1 digits need O (n) to mark, S1 = {1,3,5}.

The following discussion takes 2 numbers:

For example, a = {1,3,5}

To set the polynomial


Then S2 = {2,4,6,8,10}

Comprehensive: s={1,2,3,4,5,6,8,10}


Code;

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <

Algorithm> #define MT (a,b) memset (A,b,sizeof (a)) using namespace Std;

const int M = 201000;
    struct Complex {double x,y;
        Complex (double _x=0,double _y=0) {x=_x;
    y=_y;
    Friend Complex operator-(const Complex &a,const Complex &b) {return Complex (A.X-B.X,A.Y-B.Y);
    Friend Complex operator + (const Complex &a,const Complex &b) {return Complex (A.X+B.X,A.Y+B.Y); Friend Complex operator * (const Complex &a,const Complex &b) {return Complex (a.x*b.x-a.y*b.y,a.x*
    b.y+a.y*b.x);
}
};
    Class FFT {Complex u,t; void Change (Complex y[],int len) {for (int i=1,j=len>>1,k; i<len-1; i++) {if (i<j) swap (y[
            I],Y[J]);
            k=len>>1;
                while (j>=k) {j-=k;
            k>>=1; } if (j<k) j+=k;
        } public:void FFT (Complex y[],int len,int on) {change (Y,len);
        Double Tmp=-on*2*acos (-1.0);
            for (int h=2; h<=len; h<<=1) {Complex wn (cos (tmp/h), sin (tmp/h));
                for (int j=0; j<len; j+=h) {Complex W (1,0);
                int h2=h>>1;
                    for (int k=j; k<j+h2; k++) {u=y[k];
                    T=W*Y[K+H2];
                    Y[k]=u+t;
                    Y[k+h2]=u-t;
                W=w*wn;
            }} if (On==-1) {for (int i=0; i<len; i++) {Y[i].x/=len;

}}} G;
int flag[m*4];
Complex X[m*4];

int a[m*2];
    int main () {int n,q;
        while (~SCANF ("%d", &n)) {MT (flag,0);
        int MAXN = 0;
            for (int i=0; i<n; i++) {scanf ("%d", &a[i]);
            Flag[a[i]] = 1;
        MAXN = max (Maxn,a[i]); }

        int len = 1;
        maxn<<=1;

        while (LEN&LT;MAXN) len<<=1;
        for (int i=0; i<len; i++) {X[i] = Complex (flag[i],0);

        } g.fft (x,len,1);
        for (int i=0;i<len;i++) {X[i] = X[i]*x[i];

        } g.fft (x,len,-1);
        MT (flag,0);
        for (int i=0;i<n;i++) {flag[a[i]]++;
        for (int i=0;i<len;i++) {Flag[i] + + (int) (x[i].x+0.5); }//for (int i=0;i<len;i++) {//printf ("%d--->%d\n", i,flag[i]);//} scanf ("%d", &
        AMP;Q);
        int b;
        int ans = 0;
            while (q--) {scanf ("%d", &b);
        if (flag[b]) ans++;

    printf ("%d\n", ans);
return 0;
 /** 3 1 3 5 6 2 4 5 7 8 9 * *


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.