书上分析的太清楚,我都懒得写题解了。=_=||

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int maxp = ;
const int maxn = ;
bool vis[maxn + ];
int prime[maxp], pcnt = ; void Init()
{
int m = sqrt(maxn + 0.5);
for(int i = ; i <= m; i++) if(!vis[i])
for(int j = i*i; j <= maxn; j += i) vis[j] = true;
for(int i = ; i <= maxn; i++) if(!vis[i]) prime[pcnt++] = i;
} typedef int Matrix[maxn][maxn]; Matrix A; int rank(Matrix A, int m, int n)
{//求系数矩阵A的秩,m个方程,n个未知数
int i = , j = ;
while(i < m && j < n)
{
int r = i, k;
for(k = r; k < m; k++) if(A[k][j]) { r = k; break; }
if(k < m)
{
if(r != i) for(int k = ; k < n; k++) swap(A[r][k], A[i][k]);
for(int k = i+; k < m; k++) if(A[k][j])
for(int l = j; l < n; l++) A[k][l] ^= A[i][l];
i++;
}
j++;
}
return i;
} int main()
{
//freopen("in.txt", "r", stdin); Init();
int T;
scanf("%d", &T);
while(T--)
{
memset(A, , sizeof(A));
int n, M = ;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
long long x;
scanf("%lld", &x);
for(int j = ; j < pcnt; j++) while(x % prime[j] == )
{
M = max(M, j);
x /= prime[j];
A[j][i] ^= ;
}
}
int r = rank(A, M+, n);//共用到前M+1个素数
printf("%lld\n", (1LL << (n-r)) - );
} return ;
}

代码君

最后lrj老师提到了还可以用状压加速消元,因为500以内的素数不超过100个,所以我用了两个64位的long long来表示一个方程。第一份代码16ms,状压以后12ms,快了四分之一。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int maxp = ;
const int maxn = ;
bool vis[maxn + ];
int prime[maxp], pcnt = ; void Init()
{
int m = sqrt(maxn + 0.5);
for(int i = ; i <= m; i++) if(!vis[i])
for(int j = i*i; j <= maxn; j += i) vis[j] = true;
for(int i = ; i <= maxn; i++) if(!vis[i]) prime[pcnt++] = i;
} typedef long long Matrix[maxn][]; Matrix A; int rank(Matrix A, int m, int n)
{//求系数矩阵A的秩,m个方程,n个未知数
int i = , j = , len = n / ;
while(i < m && j < n)
{
int r = i, k;
for(k = r; k < m; k++) if(A[k][j/] & (1LL<<(j%))) { r = k; break; }
if(k < m)
{
if(r != i) for(int k = ; k <= len; k++) swap(A[r][k], A[i][k]);
for(int k = i+; k < m; k++) if(A[k][j/] & (1LL<<(j%)))
for(int l = ; l <= len; l++) A[k][l] ^= A[i][l];
i++;
}
j++;
}
return i;
} int main()
{
//freopen("in.txt", "r", stdin); Init();
int T;
scanf("%d", &T);
while(T--)
{
memset(A, , sizeof(A));
int n, M = ;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
long long x;
scanf("%lld", &x);
for(int j = ; j < pcnt; j++) while(x % prime[j] == )
{
M = max(M, j);
x /= prime[j];
A[j][i/] ^= (1LL << (i%) );
}
}
int r = rank(A, M+, n);//共用到前M+1个素数
printf("%lld\n", (1LL << (n-r)) - );
} return ;
}

代码君

UVa 11542 (高斯消元 异或方程组) Square的更多相关文章

  1. BZOJ.1923.[SDOI2010]外星千足虫(高斯消元 异或方程组 bitset)

    题目链接 m个方程,n个未知量,求解异或方程组. 复杂度比较高,需要借助bitset压位. 感觉自己以前写的(异或)高斯消元是假的..而且黄学长的写法都不需要回代. //1100kb 324ms #i ...

  2. UVA11542 Square(高斯消元 异或方程组)

    建立方程组消元,结果为2 ^(自由变元的个数) - 1 采用高斯消元求矩阵的秩 方法一: #include<cstdio> #include<iostream> #includ ...

  3. Tsinsen-A1488 : 魔法波【高斯消元+异或方程组】

    高斯消元. 自己只能想出来把每一个点看成一个变量,用Xi表示其状态,这样必定TLE,n^2 个变量,再加上3次方的高斯消元(当然,可以用bitset压位). 正解如下: 我们把地图划分成一个个的横条和 ...

  4. UVA 11542 高斯消元

    从数组中选择几个数,要求他们的乘积可以开平方,问有多少种方案. 先将单个数拆分成质因子,对于这个数而言,那些指数为奇数的质因子会使这个数无法被开平方. 所以我们需要选择一个对应质因子指数为奇数的元素, ...

  5. UVA 11542 Square 高斯消元 异或方程组求解

    题目链接:点击打开链接 白书的例题练练手. . . P161 #include <cstdio> #include <iostream> #include <algori ...

  6. POJ.1830.开关问题(高斯消元 异或方程组)

    题目链接 显然我们需要使每个i满足\[( ∑_{j} X[j]*A[i][j] ) mod\ 2 = B[i]\] 求这个方程自由元Xi的个数ans,那么方案数便是\(2^{ans}\) %2可以用^ ...

  7. 【高斯消元解xor方程组】BZOJ2466-[中山市选2009]树

    [题目大意] 给出一棵树,初始状态均为0,每反转一个节点的状态,相邻的节点(父亲或儿子)也会反转,问要使状态均为1,至少操作几次? [思路] 一场大暴雨即将来临,白昼恍如黑夜!happy! 和POJ1 ...

  8. poj1830(高斯消元解mod2方程组)

    题目链接:http://poj.org/problem?id=1830 题意:中文题诶- 思路:高斯消元解 mod2 方程组 有 n 个变元,根据给出的条件列 n 个方程组,初始状态和终止状态不同的位 ...

  9. POJ 1222 EXTENDED LIGHTS OUT(高斯消元解XOR方程组)

    http://poj.org/problem?id=1222 题意:现在有5*6的开关,1表示亮,0表示灭,按下一个开关后,它上下左右的灯泡会改变亮灭状态,要怎么按使得灯泡全部处于灭状态,输出方案,1 ...

随机推荐

  1. java集合类(二)List学习

    接上篇  java集合类(一) List接口继承了Collection接口和Iterable接口,即同样含有Collection和 Iterable的特性,还有方法,其基本方法有: 1)有关添加: b ...

  2. 1293: [SCOI2009]生日礼物 - BZOJ

    Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个彩 ...

  3. mysql数据恢复

    [1] 当数据库被删除后的恢复方法   首先建立一个测试用的数据库.  mysql -u root -p123123   ← 用root登录到MySQL服务器  Enter password:  ←  ...

  4. 把工程部署在tomcat的root路径下

    myeclipse可以右键工程:(eclipse也可以)选择properties->myeclipse->web:把web context-root改成:/然后在用myeclipse部署项 ...

  5. git/github在windows上使用

    问题描述:     git在Windows上的使用 问题解决:     (1)下载安装git http://msysgit.github.io/ 到该网址中下载msgit软件 注:     安装msg ...

  6. VS2010 创建WindowsService服务

    1.新建一个Windows 服务 2.添加Installer 这一步很重要,在处理完你的业务逻辑后需要添加一个Installer才能是你的Windows服务被安装. 在VS中添加Installer 右 ...

  7. centos在yum install报错:Another app is currently holding the yum lock解决方法

    centos在yum install报错:Another app is currently holding the yum lock,这个问题可能是很多的新手经常遇到问题,之前也有人问我,包括本人在刚 ...

  8. 地图索引 R-tree

    http://blog.csdn.net/v_JULY_v/article/details/6530142 984年,加州大学伯克利分校的Guttman发表了一篇题为“R-trees: a dynam ...

  9. REST_FRAMEWORK加深记忆-加了用户登陆认证,自定义权限的API接口

    哈哈,终于快结束了.. urls.py from django.conf.urls import include, url from django.contrib import admin urlpa ...

  10. hdu 1271 整数对

    看了别人的解题报告a了, 大致思路就是 A=a+b*10^k+c*10^(k+1) B=a+c*10^k (在A中取出一位数后) N=A+B=2*a+b*10^k+11*c*10^k 这样就好做了,再 ...