当前位置:网站首页>tourist的NTT模板
tourist的NTT模板
2022-07-07 22:41:00 【愚末语】
template <typename T>
class Modular {
public:
using Type = typename decay<decltype(T::value)>::type;
constexpr Modular() : value() {}
template <typename U>
Modular(const U& x) {
value = normalize(x);
}
template <typename U>
static Type normalize(const U& x) {
Type v;
if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
else v = static_cast<Type>(x % mod());
if (v < 0) v += mod();
return v;
}
const Type& operator()() const { return value; }
template <typename U>
explicit operator U() const { return static_cast<U>(value); }
constexpr static Type mod() { return T::value; }
Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(int) { Modular result(*this); *this += 1; return result; }
Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
Modular operator-() const { return Modular(-value); }
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
#ifdef _WIN32
uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
asm(
"divl %4; \n\t"
: "=a" (d), "=d" (m)
: "d" (xh), "a" (xl), "r" (mod())
);
value = m;
#else
value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
return *this;
}
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, long long>::value, Modular>::type& operator*=(const Modular& rhs) {
long long q = static_cast<long long>(static_cast<long double>(value) * rhs.value / mod());
value = normalize(value * rhs.value - q * mod());
return *this;
}
template <typename U = T>
typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
value = normalize(value * rhs.value);
return *this;
}
Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }
friend const Type& abs(const Modular& x) { return x.value; }
template <typename U>
friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename V, typename U>
friend V& operator>>(V& stream, Modular<U>& number);
private:
Type value;
};
template <typename T>
class NTT {
public:
using Type = typename decay<decltype(T::value)>::type;
static Type md;
static Modular<T> root;
static int base;
static int max_base;
static vector<Modular<T>> roots;
static vector<int> rev;
static void clear() {
root = 0;
base = 0;
max_base = 0;
roots.clear();
rev.clear();
}
static void init() {
md = T::value;
assert(md >= 3 && md % 2 == 1);
auto tmp = md - 1;
max_base = 0;
while (tmp % 2 == 0) {
tmp /= 2;
max_base++;
}
root = 2;
while (power(root, (md - 1) >> 1) == 1) {
root++;
}
assert(power(root, md - 1) == 1);
root = power(root, (md - 1) >> max_base);
base = 1;
rev = {0, 1};
roots = {0, 1};
}
static void ensure_base(int nbase) {
if (md != T::value) {
clear();
}
if (roots.empty()) {
init();
}
if (nbase <= base) {
return;
}
assert(nbase <= max_base);
rev.resize(1 << nbase);
for (int i = 0; i < (1 << nbase); i++) {
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
}
roots.resize(1 << nbase);
while (base < nbase) {
Modular<T> z = power(root, 1 << (max_base - 1 - base));
for (int i = 1 << (base - 1); i < (1 << base); i++) {
roots[i << 1] = roots[i];
roots[(i << 1) + 1] = roots[i] * z;
}
base++;
}
}
static void fft(vector<Modular<T>> &a) {
int n = (int) a.size();
assert((n & (n - 1)) == 0);
int zeros = __builtin_ctz(n);
ensure_base(zeros);
int shift = base - zeros;
for (int i = 0; i < n; i++) {
if (i < (rev[i] >> shift)) {
swap(a[i], a[rev[i] >> shift]);
}
}
for (int k = 1; k < n; k <<= 1) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
Modular<T> x = a[i + j];
Modular<T> y = a[i + j + k] * roots[j + k];
a[i + j] = x + y;
a[i + j + k] = x - y;
}
}
}
}
static vector<Modular<T>> multiply(vector<Modular<T>> a, vector<Modular<T>> b) {
if (a.empty() || b.empty()) {
return {};
}
int eq = (a == b);
int need = (int) a.size() + (int) b.size() - 1;
int nbase = 0;
while ((1 << nbase) < need) nbase++;
ensure_base(nbase);
int sz = 1 << nbase;
a.resize(sz);
b.resize(sz);
fft(a);
if (eq) b = a; else fft(b);
Modular<T> inv_sz = 1 / static_cast<Modular<T>>(sz);
for (int i = 0; i < sz; i++) {
a[i] *= b[i] * inv_sz;
}
reverse(a.begin() + 1, a.end());
fft(a);
a.resize(need);
return a;
}
};
边栏推荐
- Vscode software
- 玩轉Sonar
- [C language] objective questions - knowledge points
- Scrapy framework
- Service Mesh介绍,Istio概述
- [programming questions] [scratch Level 2] March 2019 garbage classification
- The difference between get and post
- paddle入门-使用LeNet在MNIST实现图像分类方法二
- How to learn a new technology (programming language)
- If an exception is thrown in the constructor, the best way is to prevent memory leakage?
猜你喜欢
Stm32f1 and stm32cubeide programming example - rotary encoder drive
【编程题】【Scratch二级】2019.09 绘制雪花图案
STM32F1與STM32CubeIDE編程實例-旋轉編碼器驅動
3 years of experience, can't you get 20K for the interview and test post? Such a hole?
Two small problems in creating user registration interface
new和delete的底层原理以及模板
After going to ByteDance, I learned that there are so many test engineers with an annual salary of 40W?
80% of the people answered incorrectly. Does the leaf on the apple logo face left or right?
5G NR 系统消息
Coindesk comments on the decentralization process of the wave field: let people see the future of the Internet
随机推荐
接口测试要测试什么?
Usage of limit and offset (Reprint)
Jouer sonar
Use filters to count URL request time
备库一直有延迟,查看mrp为wait_for_log,重启mrp后为apply_log但过一会又wait_for_log
Operating system principle --- summary of interview knowledge points
商品的设计等整个生命周期,都可以将其纳入到产业互联网的范畴内
Handwriting a simulated reentrantlock
1293_FreeRTOS中xTaskResumeAll()接口的实现分析
[C language] objective questions - knowledge points
Coindesk comments on the decentralization process of the wave field: let people see the future of the Internet
52岁的周鸿祎,还年轻吗?
Which securities company has a low, safe and reliable account opening commission
玩转Sonar
QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
3 years of experience, can't you get 20K for the interview and test post? Such a hole?
[basis of recommendation system] sampling and construction of positive and negative samples
从服务器到云托管,到底经历了什么?
Anaconda+pycharm+pyqt5 configuration problem: pyuic5 cannot be found exe