NTT Board __ Board

Source: Internet
Author: User
Tags pow cmath
NTT can easily explode int

First to find the original root of the program (it seems that the original root of the NTT are 3)
Code

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

> #include <algorithm> #define LL long using namespace std;
CONST LL MAXN = 110;
ll P[MAXN],PN;
    void Div (ll x) {ll t=sqrt (x*1.0); pn=0;
            for (ll i=2;i<=t;i++) {if (x%i==0) {p[++pn]=i;
        while (x%i==0) x/=i;
} if (x>1) p[++pn]=x;
    ll Pow (ll x,ll k,ll MoD) {x%=mod;
    ll Nowk=0,ret=1;
    ll Tmp=x,tmpk=1;
            while (nowk!=k) {if (tmpk&k) {NOWK|=TMPK;
        Ret=ret*tmp%mod; } Tmp=tmp*tmp%mod;
    tmpk<<=1;
return ret;
    } void Get_g (ll N) {div (n-1);
        for (ll i=2;i<n;i++) {bool flag=true;
            for (ll j=1;j<=pn;j++) {ll t= (n-1)/p[j];
        if (Pow (i,t,n) ==1) {flag=false; break;}
            } if (flag) {ll j=n-1,temp=0; while (!) ( J& 1)) {j>>=1;temp++;}
            printf ("%i64d%i64d\n", i,temp); 
        return;
    int main () {ll n;scanf ("%i64d", &n);
    Get_g (n);
return 0; }

NTT: (prime numbers are random, generally speaking, the original number of prime numbers is 3, but for the sake of insurance recommended to call a Huangen program)

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

> #include <algorithm> #define LL long using namespace std;
CONST LL MAXN = 410000;

CONST LL Mod = 469762049;
int AN,BN,N,LN,ID[MAXN];
ll A[maxn],b[maxn],c[maxn],n;

ll w[maxn],g=3;
    ll PW (ll X,int k) {x%=mod;
    ll Ret=1,tmp=x;
    int tk=1,nowk=0;
            while (nowk!=k) {if (tk&k) {ret=ret*tmp%mod;
        NOWK|=TK; } tk<<=1;
    Tmp=tmp*tmp%mod;
return ret;
    } void NTT (ll *s,int Sig) {for (int i=0;i<n;i++) if (i<id[i)) swap (s[i],s[id[i]);
        for (int m=2;m<=n;m<<=1) {int t=m>>1,tmp=n/m;
            for (ll i=0;i<t;i++) {ll wn=sig==1?w[i*tmp]:w[n-i*tmp];
                for (int j=i;j<n;j+=m) {ll tx=s[j],ty=s[j+t]*wn%mod;
                s[j]= (tx+ty)%mod;
            s[j+t]= (tx-ty)%mod;
   }     } if (sig==-1) for (int i=0;i<n;i++) s[i]= ((s[i]*n%mod) +mod)%mod;
    int main () {scanf ("%d%d", &an,&bn); an++;bn++;
    for (int i=0;i<an;i++) scanf ("%i64d", &a[i));

    for (int i=0;i<bn;i++) scanf ("%i64d", &b[i));
    n=1;ln=0;

    while (n< (an+bn)) {n<<=1;ln++;} W[0]=1;
    W[1]=PW (g, (Mod-1)/n);
    for (int i=1;i<=n;i++) W[i]=w[i-1]*w[1]%mod;
    N=PW (n,mod-2); for (int i=0;i<n;i++) id[i]= (id[i>>1]>>1) |

    ((i&1) <<ln-1); NTT (a,1);
    NTT (b,1);
    for (int i=0;i<n;i++) C[i]=a[i]*b[i]%mod;

    NTT (c,-1);
    printf ("%i64d", c[0]);
    for (int i=1;i<an+bn-1;i++) printf ("%i64d", C[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.