链接:https://www.nowcoder.com/acm/contest/147/A

来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 262144K,其他语言524288K

64bit IO Format: %lld

题目描述

Niuniu has recently learned how to use Gaussian elimination to solve systems of linear equations.

Given n and a[i], where n is a power of 2, let's consider an n x n matrix A.

The index of A[i][j] and a[i] are numbered from 0.

The element A[i][j] satisfies A[i][j] = a[i xor j],

https://en.wikipedia.org/wiki/Bitwise_operation#XOR

Let p = 1000000007.

Consider the equation 

A x = b (mod p)

where A is an n x n matrix, and x and b are both n x 1 row vector.

Given n, a[i], b[i], you need to solve the x.

For example, when n = 4, the equations look like

A[0][0]*x[0] + A[0][1]*x[1] + A[0][2]*x[2] + A[0][3]*x[3] = b[0] (mod p)

A[1][0]*x[0] + A[1][1]*x[1] + A[1][2]*x[2] + A[1][3]*x[3] = b[1] (mod p)

A[2][0]*x[0] + A[2][1]*x[1] + A[2][2]*x[2] + A[2][3]*x[3] = b[2] (mod p)

A[3][0]*x[0] + A[3][1]*x[1] + A[3][2]*x[2] + A[3][3]*x[3] = b[3] (mod p)

and the matrix A can be decided by the array a.

It is guaranteed that there is a unique solution x for these equations.

输入描述:

The first line contains an integer, which is n.
The second line contains n integers, which are the array a.
The third line contains n integers, which are the array b. 1 <= n <= 262144
p = 1000000007
0 <= a[i] < p
0 <= b[i] < p

输出描述:

The output should contains n lines.
The i-th(index from 0) line should contain x[i].
x[i] is an integer, and should satisfy 0 <= x[i] < p.

示例1

输入

复制

4
1 10 100 1000
1234 2143 3412 4321

输出

复制

4
3
2
1

看了一整天也没有真的搞明白FWT和FFT到底是干吗玩意儿 最后绝望的我决定退而求其次

大概知道一下怎么用的就行了

简而言之 FFT就是用来算多项式乘法(卷积),FWT就是用来算位运算卷积

FFT算法,是用于优化卷积,而FWT是用于优化逻辑运算卷积。形如下图:

它同样可以写作

因为 i ^ j ^ j = i 把这道题的等式列出来 就会发现

FWT(B) = FWT(A) * FWT(X)

所以对b 和 a 求一次FWT

然后对b / a做FWT的逆变换IFWT


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#define inf 1e18
using namespace std;
int n;
const long long p = 1e9 + 7;
const int maxn = 263000;
long long a[maxn], b[maxn]; long long qpow(long long a, long long b)
{
long long ans = 1;
while (b) {
if (b & 1) {
ans = ans * a % p;
}
a = a * a % p;
b = b >> 1;
}
return ans;
} void fwt(long long a[])
{
for (int d = 1; d < n; d <<= 1) {
for (int m = d << 1, i = 0; i < n; i += m) {
for (int j = 0; j < d; j++) {
long long x = a[i + j], y = a[i + j + d];
a[i + j] = (x + y) % p;
a[i + j + d] = (x - y + p) % p;
}
}
}
} void ifwt(long long a[])
{
long long inv = qpow(2, p - 2);//并不知道为什么是p - 2
for (int d = 1; d < n; d <<= 1) {
for (int m = d << 1, i = 0; i < n; i += m) {
for (int j = 0; j < d; j++) {
long long x = a[i + j], y = a[i + j + d];
a[i + j] = (x + y) % p;
a[i + j + d] = (x - y + p) % p;
a[i + j] = a[i + j] * inv % p;
a[i + j + d] = a[i + j + d] * inv % p;
}
}
}
} int main()
{
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &b[i]);
} fwt(a);
fwt(b);
for (int i = 0; i < n; i++) {
a[i] = (b[i] * qpow(a[i], p - 2)) % p;
}
ifwt(a);
for (int i = 0; i < n; i++) {
cout << a[i] << endl;
}
}
}

牛客网多校赛第九场A-circulant matrix【数论】的更多相关文章

  1. 牛客网多校训练第九场H Cutting Bamboos

    题目链接:https://ac.nowcoder.com/acm/contest/889/H 题意:给出n颗竹子的高度,q次询问,每次询问给出l,r,x,y,每次选取[l,r]中的竹子,砍y次砍掉所有 ...

  2. 牛客网多校训练第一场 B - Symmetric Matrix(dp)

    链接: https://www.nowcoder.com/acm/contest/139/B 题意: 求满足以下条件的n*n矩阵A的数量模m:A(i,j) ∈ {0,1,2}, 1≤i,j≤n.A(i ...

  3. 牛客网多校训练第一场 A - Monotonic Matrix(Lindström–Gessel–Viennot lemma)

    链接: https://www.nowcoder.com/acm/contest/139/A 题意: 求满足以下条件的n*m矩阵A的数量模(1e9+7):A(i,j) ∈ {0,1,2}, 1≤i≤n ...

  4. 牛客网多校赛第9场 E-Music Game【概率期望】【逆元】

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  5. 牛客网多校赛第七场J--Sudoku Subrectangle

    链接:https://www.nowcoder.com/acm/contest/145/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...

  6. 牛客网多校赛第七场--C Bit Compression【位运算】【暴力】

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524 ...

  7. 牛客网多校赛第七场A--Minimum Cost Perfect Matching【位运算】【规律】

    链接:https://www.nowcoder.com/acm/contest/145/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  8. 牛客网多校第3场C-shuffle card 平衡树或stl(rope)

    链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there ...

  9. 牛客网多校第3场Esort string (kmp)

    链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...

随机推荐

  1. c++Valgrind内存检测工具---19

    原创博文,转载请标明出处--周学伟  http://www.cnblogs.com/zxouxuewei/ 一.Valgrind 概述 Valgrind是一套Linux下,开放源代码(GPL V2)的 ...

  2. Simply Syntax(思维)

    Simply Syntax Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5551   Accepted: 2481 Des ...

  3. MySQL------报错Access denied for user 'root'@'localhost' (using password:NO)解决方法

    报错:Access denied for user 'root'@'localhost' (using password:NO) 原因:没有给用户“root'@'localhost”赋予数据库权限 解 ...

  4. PHP开发中,让var_dump调试函数输出更美观 ^_^#

    前提:php必须安装Xdebug模块. 用var_dump打印输出时,输出的内容没有被格式化.如下图: 通常使用var_dump打印的内容是被格式化后输出的,如下图: 造成没有格式化输出的原因是因为p ...

  5. Git Step by Step – (4) 探索.git目录

    前面一篇文章介绍了Git对象模型,接下来我们就进入".git"目录看看到底有什么东西,目录中哪些东西又跟Git对象模型相关.结合这个目录,我们将进一步了解Git的工作原理. .gi ...

  6. PHP从数组中找到指定元素的位置

    群里有人问,有个数组五个元素 分为1到5  现在要求 循环找出3元素的索引,怎么做性能才是最高. 我不知道哪个性能最高,但是我想提出可以用多种方式进行查找,然后进行比较选择. 我想,最简单最基础的 应 ...

  7. MessageDigest类提供MD5或SHA等加密算法

    MessageDigest可使用的加密方法有MD2\MD5\SHA-1\SHA-256\SHA-384\SHA-512,使用时候只替换相应参数值即可 MessageDigest md5 = Messa ...

  8. iOS开发-编译出错 duplicate symbols for architecture x86_64

    今天对原来项目文件进行重新整理,根据文件内容进行分类,结果复制粘贴时没注意把一个文件复制了两遍 编译的时候就出现Duplicate Symbol Error 在网上搜素了一圈发现也有人遇到过这个问题, ...

  9. [Shell] Shell 中的算术

    Shell 脚本变量默认是作为字符串处理,而不是数字,这使得在 Shell 脚本做数学运算显得较为复杂.在保持脚本编程规范和更好的算术支持方便,Perl 和 Python 会是更好的选择.但是你仍然可 ...

  10. 如何让移植的嵌入式ARM显示中文汉字

    如果你急于在ARM开发板上看到Qt显示中文,而不介意稍次的效果,可以在运行Qt程序时,增加设置字体的参数,比如运行名为hello的Qt程序:./hello -fn unifont 1.首先,需要文泉驿 ...