UVA - 11542 Square (异或方程组)

Given n integers you cangenerate 2n-1 non-empty subsets from them. Determine for howmany of these subsets the product of all the integers in that is a perfectsquare. For example for the set {4,6,10,15} there are3
such subsets. {4}, {6,10,15} and {4,6,10,15}. Aperfect square is an integer whose square root is an integer. For example 1, 4,9, 16,…. .

Input

Input contains multiple testcases. First line of the input contains T(1≤T≤30)the number of test cases. Each test case consists of 2 lines. First line containsn(1≤n≤100) and second linecontains
n space separated integers. All these integers are between 1 and 10^15.  None of these integers is divisible by aprime greater than 500.

Output

For each test caseoutput is a single line containing one integer denoting the number of non-emptysubsets whose integer product is a perfect square. The input will be such thatthe result will always fit into signed 64 bit integer.

SampleInput                              Output for Sample Input


4

3

2 3 5

3

6 10 15

4

4 6 10 15

3

2 2 2


0

1

3

3

Problemsetter: Abdullah al Mahmud

SpecialThanks to: Manzurur RahmanKhan

题意:给出n个正整数,从中选出1个或者多个,使得选出来的整数乘积是完全平方数,一共有多少种选法。

思路:用01向量表示一个数,再用n个01向量来表示我们的选择,因为完全平方数要求素因子的次数一定要是偶数的,所以我们可以统计的将奇数当作1,偶数当作0,那么就是一组可以变换成oxr的方程组,最后的结果有自由变量f个,答案是2^f-1,f求解就是求n-方程组的秩

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
typedef long long ll;
using namespace std;
const int maxn = 510;

typedef int Matrix[maxn][maxn];
int prime[maxn], vis[maxn];
Matrix A;

int gen_primes(int m) {
	memset(vis, 0, sizeof(vis));
	int cnt = 0;
	for (int i = 2; i < m; i++) {
		if (!vis[i]) {
			prime[cnt++] = i;
			for (int j = i * i; j < m; j += i)
				vis[j] = 1;
		}
	}
	return cnt;
}

int rank(Matrix A, int m, int n) {
	int i = 0, j = 0, k , r, u;
	while (i < m && j < n) {
		r = i;
		for (k = i; k < m; k++)
			if (A[k][j]) {
				r = k;
				break;
			}
		if (A[r][j]) {
			if (r != i)
				for (k = 0; k <= n; k++)
					swap(A[r][k], A[i][k]);
			for (u = i+1; u < m; u++)
				if (A[u][j])
					for (k = i; k <= n; k++)
						A[u][k] ^= A[i][k];
			i++;
		}
		j++;
	}
	return i;
}

int main() {
	int m = gen_primes(505);

	int t;
	scanf("%d", &t);
	while (t--) {
		int n, maxp = 0;;
		ll x;
		scanf("%d", &n);

		memset(A, 0, sizeof(A));
		for (int i = 0; i < n; i++) {
			scanf("%lld", &x);
			for (int j = 0; j < m; j++)
				while (x % prime[j] == 0) {
					maxp = max(maxp, j);
					x /= prime[j];
					A[j][i] ^= 1;
				}
		}

		int r = rank(A, maxp+1, n);
		printf("%lld\n", (1ll << (n-r)) - 1);
	}
	return 0;
}
时间: 2024-08-02 13:50:43

UVA - 11542 Square (异或方程组)的相关文章

UVA 11542 - Square(高斯消元)

UVA 11542 - Square 题目链接 题意:给定一些数字,保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为完全平方数,问有几种选法 思路:对每个数字分解成质因子后,发现如果要是完全平方数,选出来的数字的每个质因子个数都必然要是偶数,这样每个质因子可以列出一个异或的方程,如果数字包含质因子,就是有这个未知数,然后进行高斯消元,求出自由变量的个数,每个自由变量可以选或不选,这样的情况就是(2^个数),然后在扣掉什么都不选的1种就是答案了 代码: #include <cstdi

Uva 11542 Square

题目中说数组中的数的最大质因子不超过500,我们筛出≤500的质数,然后考虑对每个质数列一个方程组.. 然后这几乎就是高斯消元求解异或方程组的模板题了.... 注意答案是 2^(自由元数量)-1,因为空集不是答案的一部分.. #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<cstring>

UVA 11542 Square 高斯消元 异或方程组求解

题目链接:点击打开链接 白书的例题练练手. . . P161 #include <cstdio> #include <iostream> #include <algorithm> #include <math.h> #include <string.h> #include <algorithm> using namespace std; #define ll int #define LL long long const int mod

高斯消元 UVA 11542 Square

题目传送门 题意:给n个数,选择一些数字乘积为平方数的选择方案数.训练指南题目. 分析:每一个数字分解质因数.比如4, 6, 10, 15,, , , , 令,表示选择第i个数字,那么,如果p是平方数,那么每个质因数上的指数为偶数,x1系数为2已经是偶数不考虑.可以转换为异或为0判断偶数,即奇数置为1,偶数置为0,然后n个数字m个质因数的增广矩阵消元看有几个自由变量(取0或1无所谓),答案是2^r - 1(全部都不取方案不算) #include <bits/stdc++.h> const in

UVA 11542 Square ——线性基

[题目分析] 每个数没有超过500的因子.很容易想到把每一个数表示成一个二进制的数. (0代表该质数的次数为偶数,1代表是奇数) 然后问题转化成了选取一些二进制数,使他们的异或和为0. 高斯消元,2^(自由元)即为答案,需要把空集的情况减去,所以减一. 然而发现并不需要知道哪些是自由元,所以只需要用线性基去维护即可. 然后代码就呼之欲出了. [代码] #include <cstdio> #include <cstring> #include <cmath> #inclu

uva 11542 高斯消元

Square Input: Standard Input Output: Standard Output Given n integers you can generate 2n-1 non-empty subsets from them. Determine for how many of these subsets the product of all the integers in that is a perfect square. For example for the set {4,6

【BZOJ2466】【中山市选2009】树 高斯消元解异或方程组

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44356273"); } 题解: 参照此题解,也是我写的,俩题一样. [POJ1681]Painter's Problem 高斯消元,求最小∑系数的异或方程组 代码: #include <cmath> #include &

高斯消元法求解异或方程组: cojs.tk 539.//BZOJ 1770 牛棚的灯

高斯消元求解异或方程组: 比较不错的一篇文章:http://blog.sina.com.cn/s/blog_51cea4040100g7hl.html cojs.tk  539. 牛棚的灯 ★★☆   输入文件:lights.in   输出文件:lights.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗中,她感到惊

【HDU 5833】Zhu and 772002(异或方程组高斯消元)

300个最大质因数小于2000的数,选若干个它们的乘积为完全平方数有多少种方案. 合法方案的每个数的质因数的个数的奇偶值异或起来为0. 比如12=2^2*3,对应的奇偶值为01(2的个数是偶数为0,3的个数是奇数为1),3的对应奇偶值为01,于是12*3是完全平方数. 然后异或方程组就是: a11x1+a12x2+...+a1nxn=0 a21x1+a22x2+...+a2nxn=0 ... an1x1+an2x2+...+annxn=0 aij:第i个质数(2000内有303个质数)在第j个数