Fast number theory Transformation (NTT)

Source: Internet
Author: User

Today's a question, bare ntt, but I will not, so White sent 50 points.
So he came to learn about NTT.
The surface is very simple, do not bother to post, that is not the point I want to say.
The emphasis is on NTT, also called the Fast number theory transformation.
In many problems, we may encounter the polynomial multiplication problem in the mode meaning, when the traditional fast Fourier transform may not satisfy the request, this time the fast number theory transformation is useful.
Considering the realization of fast Fourier transform, using the special properties of unit complex root to reduce the operation, the cyclic convolution characteristic of DfT transform is used. So consider something that also has cyclic convolution characteristics in a modal sense.
Consider in the sense of modulo p ( p For a given prime number, meet P=C? 2 n +1 )
We make p One of the original roots for g , so analogy to the FFT, our unit root is g p? 1 n , and then other processes are analogous to the FFT.

#include <bits/stdc++.h>using namespace STD;typedef Long Longlltypedef DoubledbConst intinf=0x3f3f3f3f;intGetint () {intf=1, g=0;CharC=getchar (); while(c<' 0 '|| C>' 9 '){if(c=='-') f=-1; C=getchar ();} while(c>=' 0 '&& c<=' 9 ') g= (g<<3) + (g<<1) +c-' 0 ', C=getchar ();returnF*g;}Const intmaxn=1<< -;Const intg=7;Const intMod=950009857;intInvintw[2][MAXN];intN,m,k;intREV[MAXN];intNintCnt;ll Power (ll X,ll y) {ll res=1lL for(; y;y>>=1, x= (x*x)%mod) {if(y&1) Res=res*x%mod; }returnRes;}voidInit () { for(n=1;n<n<<1; n<<=1, cnt++); Inv=power (n,mod-2); w[0][0]=w[0][n]=w[1][0]=w[1][n]=1;intG=power (G, (mod-1)/n); for(intI=1; i<=n-1; i++) {w[0][i]= (LL) w[0][i-1]*g%mod; } for(intI=0; i<=n;i++) {w[1][i]=w[0][n-i]; } for(intI=0; i<=n;i++) {intTemp=i;intpos=0; for(intj=1; j<=cnt;j++) {pos<<=1;p OS |=temp&1; temp>>=1;    } Rev[i]=pos; }}voidFftintA[],intNintV) {inti,j,l; for(i=0; i<n;i++) {if(I>rev[i]) swap (a[i],a[rev[i]); } for(i=2; i<=n;i<<=1)    {intMid=i>>1; for(j=0; j<n;j+=i) { for(l=0; l<mid;l++) {intt= (LL) a[j+l+mid]*w[v][(n/i) *l]%mod;                A[j+l+ (mid)]= ((LL) a[j+l]-t+mod)%mod;            a[j+l]= (LL) a[j+l]+t)%mod; }        }    }}intMain () {return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Fast number theory Transformation (NTT)

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.