Nowcoder Circulant Matrix ( FWT )
题意 :
给你一个a数组和b数组,构造出A[i][j]矩阵(A[i][j] = a[i xor j])
给出等式 A * x = b ( mod p )
n等于4的时候有:
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)
分析 :
对于给出的矩阵乘法式子、你随便取出第一行会发现一个规律
例如 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[0][0]*x[0] => a[0^0] * x[0] => a[0] * x[0] = b[0]
A[0][1]*x[1] => a[0^1] * x[1] => a[1] * x[1] = b[0]
......
然后你会发现当把所有的 A[i][j] 变成 a[i^j] 后
每一条恒等式都变成了异或卷积的形式
当然这个规律也可以这么看,你会发现 A 的第二维下标永远和 x 的下标一样
所以变成卷积形式的话,那么的出来的异或值永远为 i ==> A[i][j] * x[j] + A[i][j+1]*x[j+1] + .... = b[i] ==> i^j^j = i
所以可以用 FWT 思考
对于普通的 FWT 优化的是
for(int i=0; i<n; i++) for(int j=0; j<n; j++) b[i^j] += a[i] * x[j]
这里我们已知 a 和 b 要求 x、可以将下标变化一下有
for(int i=0; i<n; i++) for(int j=0; j<n; j++) b[i] += a[j] * x[j^i]
for(int i=0; i<n; i++) for(int j=0; j<n; j++) x[i^j] += b[i] / a[j]
这样就变成了熟悉的异或卷积的形式
只不过乘法运算变成了除法运算、这里还好是求模意义下的
故可以用乘法逆元来将除法变成乘法
for(int i=0; i<n; i++) for(int j=0; j<n; j++) x[i^j] = b[i] * inv_a[j]
这题就做完了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> #define LL long long #define ULL unsigned long long #define scl(i) scanf("%lld", &i) #define scll(i, j) scanf("%lld %lld", &i, &j) #define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k) #define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l) #define scs(i) scanf("%s", i) #define sci(i) scanf("%d", &i) #define scd(i) scanf("%lf", &i) #define scIl(i) scanf("%I64d", &i) #define scii(i, j) scanf("%d %d", &i, &j) #define scdd(i, j) scanf("%lf %lf", &i, &j) #define scIll(i, j) scanf("%I64d %I64d", &i, &j) #define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k) #define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k) #define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k) #define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l) #define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l) #define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l) #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define lowbit(i) (i & (-i)) #define mem(i, j) memset(i, j, sizeof(i)) #define fir first #define sec second #define VI vector<int> #define ins(i) insert(i) #define pb(i) push_back(i) #define pii pair<int, int> #define VL vector<long long> #define mk(i, j) make_pair(i, j) #define all(i) i.begin(), i.end() #define pll pair<long long, long long> #define _TIME 0 #define _INPUT 0 #define _OUTPUT 0 clock_t START, END; void __stTIME(); void __enTIME(); void __IOPUT(); using namespace std; const LL mod = 1e9 + 7; LL inv2 = (mod + 1)>>1; void FWT(LL f[], int n, int op) { int mx = 0; while((1LL<<mx) < n) mx++; for (int i = 1; i <= mx; ++i) { int m = (1 << i), len = m >> 1; for (int r = 0; r < n; r += m) { int t1 = r, t2 = r + len; for (int j = 0; j < len; ++j, ++t1, ++t2) { LL x1 = f[t1], x2 = f[t2]; if (op == 1) { //xor f[t1] = x1 + x2; f[t2] = (x1 - x2 + mod) % mod; if(f[t1] >= mod) f[t1] -= mod; if(f[t2] < 0) f[t2] += mod; } if (op == 2) { //and f[t1] = x1 + x2; f[t2] = x2; if(f[t1] >= mod) f[t1] -= mod; } if (op == 3) { //or f[t1] = x1; f[t2] = x2 + x1; if(f[t2] >= mod) f[t2] -= mod; } } } } } void IFWT(LL f[], int n, int op) { int mx = 0; while((1LL<<mx) < n) mx++; for (int i = mx; i >= 1; --i) { int m = (1 << i), len = m >> 1; for (int r = 0; r < n; r += m) { int t1 = r, t2 = r + len; for (int j = 0; j < len; ++j, ++t1, ++t2) { LL x1 = f[t1], x2 = f[t2]; if (op == 1) { //xor // f[t1] = (x1 + x2) / 2; // f[t2] = (x1 - x2) / 2; f[t1] = (x1 + x2) * inv2; f[t2] = (x1 - x2) * inv2; if(f[t1] >= mod) f[t1] %= mod; if(f[t2] >= mod) f[t2] %= mod; if(f[t2] < 0) f[t2] = f[t2] % mod + mod; } if (op == 2) { //and f[t1] = x1 - x2; f[t2] = x2; if(f[t1] < 0) f[t1] += mod; } if (op == 3) { //or f[t1] = x1; f[t2] = x2 - x1; if(f[t2] < 0) f[t2] += mod; } } } } } LL pow_mod(LL a, LL b){ a %= mod; LL ret = 1; while(b){ if(b & 1) ret = (ret * a)%mod; a = (a * a)%mod; b >>= 1; }return ret; } const int maxn = 262144 + 10; LL a[maxn], b[maxn], x[maxn]; int main(void){__stTIME();__IOPUT(); int n; sci(n); for(int i=0; i<n; i++) scl(a[i]); for(int i=0; i<n; i++) scl(b[i]); FWT(a, n, 1); FWT(b, n, 1); for(int i=0; i<n; i++) x[i] = (b[i] * pow_mod(a[i], mod-2))%mod; IFWT(x, n, 1); for(int i=0; i<n; i++) printf("%lld\n", x[i]); __enTIME();return 0;} void __stTIME() { #if _TIME START = clock(); #endif } void __enTIME() { #if _TIME END = clock(); cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl; #endif } void __IOPUT() { #if _INPUT freopen("in.txt", "r", stdin); #endif #if _OUTPUT freopen("out.txt", "w", stdout); #endif }