1. 程式人生 > >高斯消元&線性基模板

高斯消元&線性基模板

高斯消元:poj1222

#include <iostream>
#include <cstdio>
#define N 5
#define M 6
#define L 10
using namespace std;
int n,b[L*L],a[L*L][L*L],c[L*L];

inline int read(){
	int s=0; char c=getchar();
	while (c<'0' || c>'9') c=getchar();
	while (c>='0' && c<='9') s=s*10+c-'0',c=getchar();
	return s;
}

inline void mod(int x,int y){
	for (int i=1;i<=N*M;i++)
		a[x][i]^=a[y][i];
	b[x]^=b[y];
}

int main(){
	n=read();
	for (int t=1;t<=n;t++){
		for (int i=1;i<=N;i++)
			for (int j=1;j<=M;j++)
				b[M*(i-1)+j]=read();
		for (int i=1;i<=N;i++)
			for (int j=1;j<=M;j++){
				int x=M*(i-1)+j;
				a[x][x]=1;
				if (i>1) a[x][x-M]=1;
				if (i<N) a[x][x+M]=1;
				if (j>1) a[x][x-1]=1;
				if (j<M) a[x][x+1]=1;
			}
		for (int i=1;i<=N*M;i++){
			for (int j=i;j<=N*M;j++)
				if (a[j][i]){
					for (int k=1;k<=N*M;k++)
						swap(a[j][k],a[i][k]);
					swap(b[j],b[i]);
				}
			for (int k=1;k<=N*M;k++)
				if (a[k][i] && k!=i) mod(k,i);
			}
		printf("PUZZLE #%d\n",t);
		for (int i=1;i<=N;i++){
			for (int j=1;j<M;j++)
				printf("%d ",b[M*(i-1)+j]);
			printf("%d\n",b[M*i]);
		}
	}
	return 0;
}

線性基:bzoj3150
#include <bits/stdc++.h>
#define N 110
#define ll long long
using namespace std;
int n,a[N],c[N],l(-1),z[N],x;
ll ans(0);

int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a+1,a+1+n);
	for (int i=n;i;i--){
		x=a[i];
		for (int j=0;j<=32;j++)
			if ((1<<j)&a[i]){
				if (c[j]) a[i]^=c[j];
				else{
					c[j]=a[i]; break;
				}
		}
		if (!a[i]) ans+=x;
	}
	printf("%lld\n",ans);
	return 0;
}