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 ...
随机推荐
- 一点简单的关于ASP.NET下载
一点简单的关于ASP.NET下载 个人简单的认为是有两种方法的,第一种就是直接用一个超链接链接到我们要下载的资源就可以了.只是说这个方法会有一点小问题就是,比如像图片或者文本文件这些浏览器是可以自动将 ...
- android studio 突然出现Gradle project sync failed 错误
出现: 之前还是好好的,突然就出现Gradle project sync failed 错误,网上原因可能是工具的问题. 解决办法: 重新打开android studio就好了.不知道大家还有其他的 ...
- 怎么让LinearLayout充满ScrollView
ScrollView里只能放一个元素. 当ScrollView里的元素想填满ScrollView时,使用"fill_parent"是不管用的,必需为ScrollView设置: ...
- IP地址格式控制
/// <summary> /// 验证IP格式是否输入正确 /// </summary> /// <param name="ip"></ ...
- 无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent
无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent 在Flex Application里,是不能直接用addChild添加Sprite ...
- c++ 枚举 在函数中的应用
#include <iostream> using namespace std; enum RespErrNo { SUCCESS = , INVALID_URL = , INVALID_ ...
- [转载]点评阿里云、盛大云等国内IaaS产业
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:刘黎明 原文地址:http://www.chinacloud.org ...
- ORA-01031:insufficient privileges
描述:oracle11g用scott用户在plsql上以sysdba身份登录显示以上错误,可是在cmd面板中却正常,网上各种找答案不没有对症,最后这位网友的回答解决了我的问题. 原帖网址:http:/ ...
- CentOS 6.6安装LAMP和Subversion服务器
目标:在CentOS 6.6上安装LAMP,并安装最新版1.8.*的Subversion服务器,和Subversion权限管理前端iF.svnadmin. 安装步骤: 安装新一些版本LAMP步骤 1. ...
- PHP杂记
SOAP: 感觉是类似于Java中的HttpClient的东西,和curl也有点像. PHPStorm中查看所有的函数结构(Structure):Alt+7 查找方法或类(Symbol Name 函数 ...