Polynomial modulus template

Source: Internet
Author: User
Tags modulus printf
Preface:

Recently wanted to write a template for polynomial modulo, but could not find the template problem. So the internet to find a high-precision division problem, after writing to find that the high-precision division and polynomial modulus is not the same qaq. The former requires no negative numbers, for which some positions can be temporarily 0, the remainder is pulled to the back, while polynomial modulus requires that the highest item must remain 0.

So I changed the program casually, first throw it here, after the memo. I'm writing a real number (FFT) version (I don't know what the real version of the polynomial modulo actually does).

In addition I do not know whether to write wrong, after a few groups of hand-made small data on the ...

CODE:

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

> #include <cstdlib> #include <stdio.h> #include <algorithm> using namespace std;
const int maxn=1000000;

Const double Pi=acos (-1.0);
    struct Complex {double x, y;

Complex (Double a=0.0,double b=0.0): X (a), Y (b) {}};
Complex operator+ (Complex A,complex b) {return Complex (A.X+B.X,A.Y+B.Y);}
Complex operator-(Complex A,complex b) {return Complex (A.X-B.X,A.Y-B.Y);}

Complex operator* (Complex A,complex b) {return Complex (a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
Complex A[MAXN];

Complex B[MAXN];
int REV[MAXN];

int N,LG;
Double F[MAXN];

int G[MAXN];
Double ANS[MAXN];

Double MOD[MAXN];
Char S[MAXN];
int H[MAXN];

int ln,lm;

    void DFT (Complex *a,double f) {for (int i=0; i<n; i++) if (I<rev[i]) swap (a[i],a[rev[i]);
        for (int len=2; len<=n; len<<=1) {int mid= (len>>1);
  Double ang=2.0*pi/(double) Len;      Complex e (cos (ANG), F*sin (ANG));
            for (Complex *p=a; p!=a+n; p+=len) {Complex wn (1.0,0.0);
                for (int i=0; i<mid; i++) {Complex temp=wn*p[mid+i];
                P[mid+i]=p[i]-temp;
                P[i]=p[i]+temp;
            Wn=wn*e;
        }}}} void FFT (bool rev) {for (int i=0; i<n; i++) {rev[i]=0;
    for (int j=0; j<lg; J + +) if (i& (1<<j)) rev[i]|= (1<< (lg-j-1));
    } DFT (a,1.0);
    DFT (b,1.0);
    if (rev.) for (int i=0; i<n; i++) a[i]=a[i]*a[i];
    for (int i=0; i<n; i++) a[i]=a[i]*b[i];
    DFT (a,-1.0); for (int i=0; i<n; i++) a[i].
X/= (double) N;
    } void Poly_rev (int m) {if (m==1) f[0]=1.0/(double) g[0];

        else {Poly_rev (m>>1);
        n=2*m,lg=0;
        while ((1&LT;&LT;LG) <n) lg++;
        for (int i=0; i<m; i++) A[i]=complex (f[i],0.0), B[i]=complex (g[i],0.0); FoR (int i=m; i<n; i++) A[i]=b[i]=complex (0.0,0.0);
        FFT (TRUE); for (int i=0; i<m; i++) f[i]=2.0*f[i]-a[i].
    X
    }} int main () {freopen ("c.in", "R", stdin);

    Freopen ("C.out", "w", stdout);
    scanf ("%d", &AMP;LN);
    for (int i=ln-1; i>=0; i--) scanf ("%d", &h[i]);
    scanf ("%d", &AMP;LM);

    for (int i=lm-1; i>=0; i--) scanf ("%d", &g[i]);
    N=1;
    while (n<ln-lm+3) n<<=1;

    Poly_rev (N);
    ln=ln-lm+1;
    n=1,lg=0;
    while (n<2*ln+2) n<<=1,lg++;
    for (int i=0; i<ln; i++) A[i]=complex (f[i],0.0), B[i]=complex (h[i],0.0);
    for (int i=ln; i<n; i++) A[i]=b[i]=complex (0.0,0.0);
    FFT (FALSE); for (int i=0; i<ln; i++) ans[i]=a[i].
    X

    for (int i=0; i<ln-i-1; i++) swap (ans[i],ans[ln-i-1]);
    for (int i=0; i<ln; i++) printf ("%.3lf", Ans[i]);

    printf ("\ n");
    n=1,lg=0;
    while (n<ln+lm+2) n<<=1,lg++;
    for (int i=0; i<n; i++) A[i]=b[i]=complex (0.0,0.0); for (int i=0; i<ln
    i++) A[i]=complex (ans[i],0.0);
    for (int i=0; i<lm; i++) B[i]=complex (g[lm-i-1],0.0);
    FFT (FALSE); for (int i=0; i<lm-1; i++) mod[i]=h[ln+lm-i-2]-a[i].

    X
    for (int i=0; i<lm-1; i++) printf ("%.3lf", Mod[i]);

    printf ("\ n");
return 0; }

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.