ntt768系数生成代码

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
const int W = 55;
const int Q = 3457;
const int R = (1<<16)%Q;

int qmi(int a, int k) {
	int ans = 1;
	while (k) {
		if (k & 1)ans = (ans * a) % Q;
		a = a * a % Q;
		k >>= 1;
	}
	return (long long)ans% Q;
}

vector<vector<int>> genTree() {
	queue<int> q;
	vector<int> tmp;
	vector<vector<int>> ans;
	ans.push_back({ 64 });
	q.push(32);
	q.push(160);
	q.push(-1);
	while (q.size()) {
		auto t = q.front();
		q.pop();
		if (t == -1) {
			ans.push_back(tmp);
			tmp.clear();
			if(q.size()>1)
			q.push(-1);
			continue;
		}
		else {
			tmp.push_back(t);
			if ((t & 1) == 0) {
				q.push(t / 2);
				q.push((t + 192) / 2);
			}
		}
	}
	return ans;
}

vector<vector<int>> nttForward(vector<vector<int>> &tree) {
	vector<vector<int>> ans;
	for (auto& i : tree) {
		vector<int> v;
		for (auto j : i) {
			v.push_back(qmi(W, j) * R % Q);
		}
		ans.push_back(v);
	}
	return ans;
}

vector<vector<int>> nttBackward(vector<vector<int>>& tree) {
	vector<vector<int>> ans;
	for (auto& i : tree) {
		vector<int> v;
		for (auto j : i) {
			v.push_back(qmi(W, 384 - j) * R % Q);
		}
		ans.push_back(v);
		
	}
	return ans;
}
int getInv(int val) {
	for (int i = 1; i < 3457; i++) {
		if (qmi(val, i) == 1) {
			return qmi(val, i - 1);
		}
	}
	return -1;
}
int calcLastNttBackward() {
	//b[j]-b[j+128]=2*t-a[j+128]
	//             =2*(a[j+128]*(55**64))-a[j+128]
	//             =a[j+128]*((2*(55**64)-1)
	//
	int t = (2 * qmi(55, 64) - 1) % Q;
	int inv = getInv(t);
	return inv * (1 << 16) % Q;
}

int main() {
	auto tree = genTree();
	auto ans=nttForward(tree);
	auto b = nttBackward(tree);
	for (auto& t : tree) {
		for (auto i : t)cout << i << ' ';
		cout << endl;
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值