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

 #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. Android 自定义Toast,不使用系统Toast

    效果图: 创建Toast类 package com.example.messageboxtest; import android.app.Activity; import android.conten ...

  2. Discuz!NT 后台任意文件上传的源代码修补方法

    相关的漏洞介绍: http://www.wooyun.org/bugs/wooyun-2013-035199 Discuz!NT 管理后台可以自定义文件类型,从而造成任意文件上传,测试过程中 aspx ...

  3. 在ubuntu16.04 下安装haproxy 1.5.11 做tcp负载均衡

    由于haproxy需要FQ下载,所以从csdn下载了较为新版的haproxy1.5.11,安装过程如下: 1. 解压haproxy-1.5.11.tar.gz : tar xzvf haproxy-1 ...

  4. HTTP - 内容编码

    HTTP 应用程序有时在发送之前需要对内容进行编码.例如,在把很大的 HTML 文档发送给通过慢速连接上来的客户端之前,服务器可能就会对它进行压缩,这样有助于减少传输实体的时间. 内容编码过程 内容编 ...

  5. JS Web打印,实现预览新样式

    问题描述:     JS实现Web打印,要求打印前一种样式,打印预览时新样式 问题解决:         (1)设置打印时的css样式,设置打印前的css样式 注:         以上为print. ...

  6. 【BZOJ】【3404】【USACO2009 Open】Cow Digit Game又见数字游戏

    博弈论 Orz ZYF 从前往后递推……反正最大才10^6,完全可以暴力预处理每个数的状态是必胜还是必败(反正才两个后继状态),然后O(1)查询……我是SB /******************** ...

  7. [转载]Windows 7 IIS (HTTP Error 500.21 - Internal Server Error)解决

    今天在测试网站的时候,在浏览器中输入http://localhost/时,发生如下错误: HTTP Error 500.21 - Internal Server Error Handler " ...

  8. Unsupervised Learning: Use Cases

    Unsupervised Learning: Use Cases Contents Visualization K-Means Clustering Transfer Learning K-Neare ...

  9. win8 任务栏不合并隐藏标题

    让win8任务栏不合并,并且隐藏标题的办法: 效果如下: 首先让win8不合并任务栏 1.任务栏上点鼠标右键 -- "属性" 2."任务栏按钮"选择" ...

  10. 转载一个不错的Scrapy学习博客笔记

    背景: 最近在学习网络爬虫Scrapy,官网是 http://scrapy.org 官方描述:Scrapy is a fast high-level screen scraping and web c ...