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

 #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. linux 错误总结

    帝国cms登录后台提示“登录成功”,接着又提示“您还未登录” 把帝国cms文件夹下的/e/data/adminlogin 目录权限不可写导致,请将此目录权限设置为777权限即可解决.就可以正常登录后台 ...

  2. 虚拟机备份转移后,网络启动异常,提示“SIOCSIFADDR: No such device”的解决方案

    虚拟机管理软件:Oracle VirturalBox Manager 4.0.8 虚拟机:Ubuntu Server 10.10 i386 The problem lies in the fact t ...

  3. Atmel Studio 6.0新建项目

        使用Atmel Studio 6.0新建一个项目 连接Atmel 开发板与调试板,开发板使用的芯片是ATXMGEA128A1 注意: 以上对于开发板与调试板的连接,需要非常注意连接时线的方向, ...

  4. VIM Taglist安装配置和使用

    问题描述:            VIM  Taglist安装于配置 问题解决:             (1)安装Taglist包      (2)解压taglist压缩包         (3)将 ...

  5. 【DP/二分】BZOJ 1863:[Zjoi2006]trouble 皇帝的烦恼

    863: [Zjoi2006]trouble 皇帝的烦恼 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 465  Solved: 240[Submit][ ...

  6. Android 问题流水总结

    先来一篇环境搭建的博客 这些都是大同小异. http://blog.csdn.net/yzhj2005/article/details/6980676 http://blog.csdn.net/wan ...

  7. 解决ORA-00020错误

    解决ORA-00020错误 分类: Oracle2009-05-13 17:26 3398人阅读 评论(0) 收藏 举报 数据库sessionoraclesql服务器object 项目上使用的Orac ...

  8. java基础知识回顾之---java String final类构造方法

    /** * String 构造方法学习 *     String(byte[ ] bytes):通过byte数组构造字符串对象. *     String(byte[] bytes, int offs ...

  9. POJ 1054 The Troublesome Frog(枚举+剪枝)

    题目链接 题意 :给你r*c的一块稻田,每个点都种有水稻,青蛙们晚上会从水稻地里穿过并踩倒,确保青蛙的每次跳跃的长度相同,且路线是直线,给出n个青蛙的脚印点问存在大于等于3的最大青蛙走的连续的脚印个数 ...

  10. *[topcoder]BracketExpressions

    http://community.topcoder.com/stat?c=problem_statement&pm=13243 就是能否通过把字符串中的'X'替换成"()" ...