UVa 11542 Square (高斯消元)
题意:给定 n 个数,从中选出一个,或者是多个,使得选出的整数的乘积是完全平方数,求一共有多少种选法,整数的素因子不大于 500。
析:从题目素因子不超过 500,就知道要把每个数进行分解。因为结果要是完全平方数,也就是说每个素因子都得出现偶数次,对于每个数我们用一个 01 向量来表示,对于这个数相应的素因子,如果出现奇数就是 1,否则就是 0,这样就可以得到一些方程,比如举个例子。 4 个整数, 4 6 10 15 ,素因子只有 2 3 5,4 = 2 ^ 2 * 3^0 * 5^0,对于每个素数可以列出一个方程 x2 + x3 = 0 mod 2 x2 + x4 = 0 mod 2 x3 + x4 = 0 mod2,很明显答案就是这些方程的解的个数,而且这个些解都是 0 或者是 1 的组合,换句话说,答案就是这些方程 2^自 由元变量 - 1,减 1 意思是去掉全不选的情况。自由变元怎么求呢,使用高斯消元,不过这次是使用异或,比高斯消元还好写。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 500 + 5;
const int maxm = 1e6 + 2;
const LL mod = 1000000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} bool vis[maxn];
int prime[maxn], cnt; void init(){
for(int i = 2; i < maxn; ++i) if(!vis[i]){
prime[cnt++] = i;
for(int j = i*i; j < maxn; j += i) vis[j] = 1;
}
} int A[maxn][maxn]; int main(){
init();
int T; cin >> T;
while(T--){
scanf("%d", &n);
ms(A, 0); int mmax = 0;
for(int i = 0; i < n; ++i){
LL x; scanf("%lld", &x);
for(int j = 0; j < cnt; ++j) while(x % prime[j] == 0){
A[j][i] ^= 1; x /= prime[j]; mmax = max(mmax, j);
}
}
int i = 0, j = 0;
while(i <= mmax && j < n){
int r = i;
for(int k = i; k <= mmax; ++k)
if(A[k][j]){ r = k; break; }
if(A[r][j]){
if(r != i) for(int k = i; k <= n; ++k) swap(A[r][k], A[i][k]);
for(int k = i+1; k <= mmax; ++k) if(A[k][j])
for(int l = i; l <= n; ++l) A[k][l] ^= A[i][l];
++i;
}
++j;
}
printf("%lld\n", (1LL<<n-i) - 1);
}
return 0;
}
UVa 11542 Square (高斯消元)的更多相关文章
- UVA 11542 - Square(高斯消元)
UVA 11542 - Square 题目链接 题意:给定一些数字.保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为全然平方数,问有几种选法 思路:对每一个数字分解成质因子后.发现假设要 ...
- UVA 11542 Square 高斯消元 异或方程组求解
题目链接:点击打开链接 白书的例题练练手. . . P161 #include <cstdio> #include <iostream> #include <algori ...
- UVA11542 Square(高斯消元 异或方程组)
建立方程组消元,结果为2 ^(自由变元的个数) - 1 采用高斯消元求矩阵的秩 方法一: #include<cstdio> #include<iostream> #includ ...
- UVA 1563 - SETI (高斯消元+逆元)
UVA 1563 - SETI option=com_onlinejudge&Itemid=8&page=show_problem&category=520&probl ...
- UVA 11542 高斯消元
从数组中选择几个数,要求他们的乘积可以开平方,问有多少种方案. 先将单个数拆分成质因子,对于这个数而言,那些指数为奇数的质因子会使这个数无法被开平方. 所以我们需要选择一个对应质因子指数为奇数的元素, ...
- UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)
UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的运行期望 思路: ...
- uva 1560 - Extended Lights Out(枚举 | 高斯消元)
题目链接:uva 1560 - Extended Lights Out 题目大意:给定一个5∗6的矩阵,每一个位置上有一个灯和开关,初始矩阵表示灯的亮暗情况,假设按了这个位置的开关,将会导致周围包含自 ...
- uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)
题目链接:uva 10808 - Rational Resistors 题目大意:给出一个博阿含n个节点,m条导线的电阻网络,求节点a和b之间的等效电阻. 解题思路:基尔霍夫定律,不论什么一点的电流向 ...
- UVA 1397 - The Teacher's Side of Math(高斯消元)
UVA 1397 - The Teacher's Side of Math 题目链接 题意:给定一个x=a1/m+b1/n.求原方程组 思路:因为m*n最多20,全部最高项仅仅有20.然后能够把每一个 ...
随机推荐
- Django的restframework的序列化组件之对单条数据的处理
之前我们学习的都是处理书籍或者出版社的所有的数据的方法,下面我们来看下处理单个书籍,或者单个出版社的方法 这个时候我们就需要重新写一个类,这个类的方法,就需要有3个参数,参数1是self,参数2是re ...
- Ajax中什么时候用同步,什么时候用异步?
AJAX中根据async的值不同分为同步(async = false)和异步(async = true)两种执行方式:在W3C的教程中推荐使用异步执行: $.ajax({ type: "po ...
- 30-算法训练 最短路 spfa
http://lx.lanqiao.cn/problem.page?gpid=T15 算法训练 最短路 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个n个顶点, ...
- C#中多线程的并行处理
System.Threading.Tasks,在该命名空间下Task是主类,表示一个类的异步的并发的操作,创建并行代码的时候不一定要直接使用Task类,在某些情况下可以直接使用Parallel静态类( ...
- os模块。笔记
os 模块提供了很多允许你的程序与操作系统直接交互的功能 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目录名:os.listdir() ...
- threejs指定对象旋转中心
指定对象旋转中心 默认情况下,对象的旋转中心都是自身的中心.对于组对象而言,也是如此.因此,可以利用这个特点,实现对象绕任何点旋转,也就是指定旋转中心.比如我们想要下图的对象绕A点旋转 我们可以添加 ...
- vs2015未能计算子级
数据源 属性里边值 设置出现问题
- 【Android优化篇】提升Activity加载速度的方法
文章转自:http://www.jianshu.com/p/2007ca0290d3 作者: CoderFan 前言 这个也是我面试遇到的问题,当时只回答了一种情况,异步加载数据,没想到别的方式,回来 ...
- Canvas绘图 (html5新增特性)
Canvas 使用<canvas>对象,需要设置属性:width,height.指定绘图的区域大小.在canvas标签前后出现的信息将在不支持<canvas>元素的浏览器中显示 ...
- BZOJ1047或洛谷2216 [HAOI2007]理想的正方形
BZOJ原题链接 洛谷原题链接 显然可以用数据结构或\(ST\)表或单调队列来维护最值. 这里采用单调队列来维护. 先用单调队列维护每一行的最大值和最小值,区间长为正方形长度. 再用单调队列维护之前维 ...