[牛客网暑期ACM多校训练营(第九场)] A Circulant Matrix [FWT]

链接:https://www.nowcoder.com/acm/contest/147/A
来源:牛客网

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.

分析: 仔细分析的话,可以发现实际上可以求:

bk=k=iXORjaixi b k = ∑ k = i X O R j a i ∗ x i

然后就可以用FWT来求解
代码

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <string>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, l, r)  for(int i = l; i < r; i++)
#define per(i, r, l) for(int i = r; i >= l; i--)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>pii;

const int N = (int) 2e6 + 11;
const int M = (int) 1e6 + 11;
const int MOD = (int) 1e9 + 7;
const double EPS = (double) 1e-9;
const double PI = (double)acos(-1.0);
const int INF = (int) 0x3f3f3f3f;
const ll INFF = (ll) 0x3f3f3f3f3f3f3f3f;

void read(int &x){
    char ch = getchar(); x = 0;
    while(ch < '0' || ch > '9') ch = getchar();
    while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
}
/*-----------------------------------------------------------------------------------*/ 

ll add(ll x, ll y){return (x += y) >= MOD ? x - MOD : x;}
ll sub(ll x, ll y){return (x -= y) < 0 ? x + MOD : x;}
int Pow(int a, int b, int c){
    int s = 1; a %= c;
    while(b){
        if(b & 1) s = s * 1ll * a % c;
        b >>= 1;
        a = a * 1ll * a % c;
    }
    return s;
}
int inv2 = Pow(2, MOD - 2, MOD);
void FWT_xor(int a[],int n,int on) {
    for(int i = 1; i < n; i <<= 1) {
        for(int j = 0; j < n; j += (i << 1)) {
            for(int k = 0; k < i; ++k) {
                int u = a[j + k], t = a[j + k + i];
                a[j + k]=add(u, t); a[j + k + i] = sub(u, t);
                if(on == -1) {
                    a[j + k] = (ll)a[j+k] * inv2 % MOD;
                    a[j + k + i] = (ll)a[j + k + i] * inv2 % MOD;
                }
            }
        }
    }
}
int a[N], b[N], mx = (1 << 19), x[N];
int main(){
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif 
    //cout << (1 << 19) <<"\n";
    int n; read(n);
    rep(i, 0, n) read(a[i]);
    rep(i, 0, n) read(b[i]);
    FWT_xor(b, n, 1);
    FWT_xor(a, n, 1);
    rep(i, 0, n) x[i] = b[i] * 1ll * Pow(a[i], MOD - 2, MOD) % MOD;
    FWT_xor(x, n, -1);
    rep(i, 0, n) printf("%d\n", (x[i] + MOD) % MOD);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值