牛客多校第九场 A Circulant Matrix(FWT)

28 篇文章 0 订阅

链接: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.

示例1

输入

复制

4
1 10 100 1000
1234 2143 3412 4321

输出

复制

4
3
2
1

题目大意:题目给出数组a和b的值,要求求出线性方程组\sum_{j=1}^{n}A[i][j]*x[j]=b[i],(i=1,2,...,n)的解,其中A[i][j]=a[i \oplus j]\oplus代表异或运算。

题目思路:这个题只需要将式子转化一下,就是一个FWT的板子题了。

题目中给出\sum_{j=1}^{n}A[i][j]*x[j]=b[i],(i=1,2,...,n),可以转化为\sum_{j=1}^{n}a[i\oplus j]*x[j]=b[i],即\sum_{i\oplus j=k}^{ } a[i]*x[j]=b[k]

这个就是一个裸的FWT的式子了。接下来对于数组a和数组b分别做一次FWT,再令b=a/b,再对b数组做一次FWT的逆变换,就可以求出结果了。

具体实现看代码:

#include <bits/stdc++.h>
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) x&-x
#define pb push_back
#define MP make_pair
#define clr(a) memset(a,0,sizeof(a))
#define _INF(a) memset(a,0x3f,sizeof(a))
#define FIN freopen("in.txt","r",stdin)
#define fuck(x) cout<<"["<<x<<"]"<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
//head
const int MX=3e5+7;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;

ll qpow(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1) res=(res*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return res;
}
void FWT_xor(int *a,int N,int opt){
	int inv2=qpow(2,mod-2);
	for(int i=1;i<N;i<<=1){
		for(int p=i<<1,j=0;j<N;j+=p){
			for(int k=0;k<i;++k){
				int X=a[j+k],Y=a[i+j+k];
				a[j+k]=(X+Y)%mod;a[i+j+k]=(X+mod-Y)%mod;
				if(opt==-1)a[j+k]=1ll*a[j+k]*inv2%mod,a[i+j+k]=1ll*a[i+j+k]*inv2%mod;
			}
		}
	}
}

int n;
int a[MX],b[MX];

int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	for(int i=0;i<n;i++) scanf("%d",&b[i]);
	FWT_xor(a,n,1);FWT_xor(b,n,1);
	for(int i=0;i<n;i++) a[i]=b[i]*qpow(a[i],mod-2)%mod;
	FWT_xor(a,n,-1);
	for(int i=0;i<n;i++) printf("%d\n",a[i]%mod);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值