NTT 分类: templates 2015-08-11 23:34 3人阅读 评论(0) 收藏
我觉得我还需要理解一下 FFT 和 NTT …
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
const int maxn = 4e4+5, Mod = 950009857;
const int G = 7;
int n, m, k, N = 1;
long long g[maxn], invN;
long long f[maxn], c[maxn];
long long p[maxn];
long long power(long long x,int t)
{
long long ret = 1;
while(t)
{
if(t&1) ret *= x, ret %= Mod;
x *= x, x %= Mod, t >>= 1;
}
return ret;
}
void PreWork()
{
while(N <= (n<<1)) N <<= 1;
invN = power(N, Mod - 2);
g[0] = g[N] = 1, g[1] = power(G, (Mod-1)/N);
for(int i = 2; i < N; i++)
g[i] = g[i-1] * g[1] % Mod;
}
void NTT(long long a[],int len,int flag)
{
for(int i = 0; i < len; i++)
{
int s = i, p = 0;
for(int j = len>>1; j > 0; j >>= 1)
p |= (s&1)*j, s >>= 1;
if(p > i) std::swap(a[p], a[i]);
}
for(int L = 2 ; L <= len; L <<= 1)
for(int i = 0; i < len ; i += L)
for(int j = 0; j < (L>>1); j++)
{
long long w = (flag == 1)?g[N/L*j]:g[N - N/L*j];
long long u = a[i + j], v = a[i + j +(L>>1)]*w % Mod;
u += v, v = u - (v<<1), a[i + j] = u % Mod;
a[i + j +(L>>1)] = (v + Mod) % Mod;
}
if(flag == -1)
for(int i = 0; i < len; i++)
a[i] = a[i] * invN % Mod;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。