UVa 11542 (高斯消元 异或方程组) Square
书上分析的太清楚,我都懒得写题解了。=_=||
#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的更多相关文章
- BZOJ.1923.[SDOI2010]外星千足虫(高斯消元 异或方程组 bitset)
题目链接 m个方程,n个未知量,求解异或方程组. 复杂度比较高,需要借助bitset压位. 感觉自己以前写的(异或)高斯消元是假的..而且黄学长的写法都不需要回代. //1100kb 324ms #i ...
- UVA11542 Square(高斯消元 异或方程组)
建立方程组消元,结果为2 ^(自由变元的个数) - 1 采用高斯消元求矩阵的秩 方法一: #include<cstdio> #include<iostream> #includ ...
- Tsinsen-A1488 : 魔法波【高斯消元+异或方程组】
高斯消元. 自己只能想出来把每一个点看成一个变量,用Xi表示其状态,这样必定TLE,n^2 个变量,再加上3次方的高斯消元(当然,可以用bitset压位). 正解如下: 我们把地图划分成一个个的横条和 ...
- UVA 11542 高斯消元
从数组中选择几个数,要求他们的乘积可以开平方,问有多少种方案. 先将单个数拆分成质因子,对于这个数而言,那些指数为奇数的质因子会使这个数无法被开平方. 所以我们需要选择一个对应质因子指数为奇数的元素, ...
- UVA 11542 Square 高斯消元 异或方程组求解
题目链接:点击打开链接 白书的例题练练手. . . P161 #include <cstdio> #include <iostream> #include <algori ...
- POJ.1830.开关问题(高斯消元 异或方程组)
题目链接 显然我们需要使每个i满足\[( ∑_{j} X[j]*A[i][j] ) mod\ 2 = B[i]\] 求这个方程自由元Xi的个数ans,那么方案数便是\(2^{ans}\) %2可以用^ ...
- 【高斯消元解xor方程组】BZOJ2466-[中山市选2009]树
[题目大意] 给出一棵树,初始状态均为0,每反转一个节点的状态,相邻的节点(父亲或儿子)也会反转,问要使状态均为1,至少操作几次? [思路] 一场大暴雨即将来临,白昼恍如黑夜!happy! 和POJ1 ...
- poj1830(高斯消元解mod2方程组)
题目链接:http://poj.org/problem?id=1830 题意:中文题诶- 思路:高斯消元解 mod2 方程组 有 n 个变元,根据给出的条件列 n 个方程组,初始状态和终止状态不同的位 ...
- POJ 1222 EXTENDED LIGHTS OUT(高斯消元解XOR方程组)
http://poj.org/problem?id=1222 题意:现在有5*6的开关,1表示亮,0表示灭,按下一个开关后,它上下左右的灯泡会改变亮灭状态,要怎么按使得灯泡全部处于灭状态,输出方案,1 ...
随机推荐
- sql2008 计划自动创建数据库分区【转】
本文转自:http://jingyan.baidu.com/article/6b97984d9a26ec1ca3b0bf77.html sql2008 计划自动创建数据库分区 固定增量的数据,自动创建 ...
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- MyEclipse 安装JRebel进行热部署
安装环境 版本:myeclipse2015stable2 说明:下面是我已经安装了界面 安装过程 进入市场 出现下面提示,不用管它,点Continue 用关键词搜索 配置 进入JRebel配置中心,配 ...
- curPos和tgtPos
curpos tgtpos 乍一看以为是当前位置和目标位置,但在项目里面这两个位置有点坑 当客户端玩家移动或者AI里面的位置,会把获得的位置付给tgtpos 而以前的tgtpos会付给curpos 所 ...
- Unity3d 接入 移动MM支付SDK(2.3) 全攻略
原地址:http://blog.csdn.net/dingxiaowei2013/article/details/26842177 先将例程运行起来 下载例程(csdn积分不够上传不了,只能用百度网盘 ...
- 分享: 利用Readability解决网页正文提取问题
原文:http://www.cnblogs.com/iamzyf/p/3529740.html 做数据抓取和分析的各位亲们, 有没有遇到下面的难题呢? - 如何从各式各样的网页中提取正文!? 虽然可以 ...
- CPU/ABI显示No system images installed for this target的解决方案
CPU/ABI显示No system images installed for this target的解决方案 手动下载image http://www.androiddevtools.cn/ SD ...
- mySql 的基本操作
mysql -uroot -proot show databases; use ltcl_net;show tables; desc tablename; 查看表结构 create table tes ...
- HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
- ubuntu下安装spark1.4.0
构建在hadoop2.6.0之上的 1.在官网下载spark-1.4.0-bin-hadoop2.6.tgz 2.解压到你想要放的文件夹里,tar zxvf spark-1.4.0-bin-hadoo ...