NTT

匿名 (未验证) 提交于 2019-12-02 23:59:01
namespace NTT {     const int g = 3;     int power(int x, int t) {         int ret = 1;         for(; t; t >>= 1, x = 1LL * x * x % P)              if(t & 1)                 ret = 1LL * ret * x % P;         return ret;     }     void NTT(int *a, int len, int f) {              int n = 1 << len;         for(int i = 0; i < n; ++i) {             int t = 0;             for(int j = 0; j < len; ++j)                  if(i >> j & 1)                      t |= 1 << (len - j - 1);              if(i < t) swap(a[i], a[t]);         }         for(int l = 2; l <= n; l <<= 1) {             int m = l >> 1;             int w = power(g, f == 1 ? (P - 1) / l : (P - 1) - (P - 1) / l);             for(int i = 0; i < n; i += l) {                 int t = 1;                 for(int k = 0; k < m; ++k, t = 1LL * t * w % P) {                     int x = a[i + k], y = 1LL * t * a[i + m + k] % P;                     a[i + k] = (x + y) % P;                     a[i + k + m] = ((x - y) % P + P) % P;                 }             }                }         if(f == -1) {             int inv = power(n, P - 2);             for(int i = 0; i < n; ++i) a[i] = 1LL * a[i] * inv % P;         }     } }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!