Codeforces #250E the child and Binary fast Fourier transform __codeforces

Source: Internet
Author: User

The topic: Given a set S, for I=1...M i=1...m How many binary trees satisfy each node's weights in the set S S and the weights and the I-I
constructs the answer polynomial f (x) f (x) and set S S, then the generating function C (x) c (x), then root node is a binary tree, and the right subtree is a two-pronged tree, the weight of itself must be in set S, in addition to the case of an empty tree
There are f (x) =f2 (x) c (x) +1 f (x) =f^2 (x) c (x) +1
Solution f (x) =1±1−4c (x) √2c (x) =21±1−4c (x) √f (x) =\frac{1\pm\sqrt{1-4c (x)}}{2c (x)}=\frac2{1\pm\sqrt{1-4c (x)}}
If the denominator is not reversible if a minus sign is taken below the equation, go to
Get F (x) =21+1−4C (x) √f (x) =\frac2{1+\sqrt{1-4c (x)}}
about polynomial negation and polynomial open root see picks blog
CF on each point 7s time can cross BZ I'm really not in the mood. The card constant
[cover face Bear] I'm all fast Fourier transform the

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 525000 #d

Efine P 998244353 #define G 3 #define INV2 499122177 using namespace std;

int n,m,l;

int a[m],b[m];
    Long Long quick_power (long long X,long long y) {long long re=1;
        while (y) {if (y&1) (re*=x)%=p; (x*=x)%=p;
    y>>=1;
return to re;
    } void FFT (int a[],int n,int type) {static int temp[m];
    int i;
    if (n==1) return;
    for (i=0;i<n;i+=2) temp[i>>1]=a[i],temp[i+n>>1]=a[i+1];
    memcpy (A,temp,sizeof (a[0]) *n);
    int *l=a,*r=a+ (n>>1);
    FFT (L,n>>1,type);
    FFT (R,n>>1,type);
    Long Long W=quick_power (G, Long Long) (P-1)/n*type% (P-1), wn=1;
    For (i=0;i<n>>1;i++, (wn*=w)%=p) temp[i]= (l[i]+wn*r[i))%p,temp[i+ (n>>1)]= (l[i]-wn*r[i]%p+p)%P;
memcpy (A,temp,sizeof (a[0]) *n);
    } void Get_inv (int a[],int b[],int n) {static int temp[m];
    int i; IF (n==1) {b[0]=quick_power (a[0],p-2);
    return;
    } GET_INV (a,b,n>>1);
    memcpy (Temp,a,sizeof (a[0]) *n);
    memset (Temp+n,0,sizeof (a[0]) *n);
    FFT (temp,n<<1,1);
    FFT (b,n<<1,1);
    for (i=0;i<n<<1;i++) temp[i]= (Long Long) b[i]* (long Long) temp[i]*b[i]%p+p)%P;
    FFT (temp,n<<1,p-2);
    Long Long Inv=quick_power (n<<1,p-2);
    for (i=0;i<n;i++) b[i]=temp[i]*inv%p;
memset (B+n,0,sizeof (a[0]) *n);
    } void Get_root (int a[m],int b[m],int N)//0-ori 1-inv {static int temp[m],b_inv[m];
    int i;
        if (n==1) {b[0]=1;
    return;
    } get_root (a,b,n>>1);
    memset (B_inv,0,sizeof (a[0]) *n);
    GET_INV (B,b_inv,n);
    memcpy (Temp,a,sizeof (a[0]) *n);
    memset (Temp+n,0,sizeof (a[0]) *n);
    FFT (temp,n<<1,1);
    FFT (b,n<<1,1);
    FFT (b_inv,n<<1,1);
  for (i=0;i<n<<1;i++) temp[i]= (Long Long) inv2* (b[i]+ (Long Long) temp[i]*b_inv[i]%p)%P;  FFT (temp,n<<1,p-2);
    Long Long Inv=quick_power (n<<1,p-2);
    for (i=0;i<n;i++) b[i]=temp[i]*inv%p;
memset (B+n,0,sizeof (a[0]) *n);
    int main () {int i,x;
    cin>>n>>m;
    for (l=1;l<=m;l<<=1); for (i=1;i<=n;i++) {s

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.