GivenNIntegers you cangenerate2n-1Non-empty Subsets from them. determine for howsets 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, 16 ,.... .
Input
Input contains multiple testcases. First line of the input containsT (1 ≤ T ≤ 30)The number of test cases. Each test case consists of 2 lines. First line containsN (1 ≤ n ≤ 100)And second linecontainsNSpace separated integers. All these integers are between 1 and 10 ^ 15. None of these integers is divisible by hour me 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
Question: n positive integers are given. One or more integers are selected from them so that the product of the selected integers is the total number of integers.
Idea: Use the 01 vector to represent a number, and use N 01 vectors to represent our choice, because the number of times of the prime factor must be an even number, so we can calculate the odd number as 1, and the even number as 0, which is a group of equations that can be transformed into oxr. The final result is a free variable F. The answer is 2 ^ F-1, f is used to calculate the rank of the N-equations.
#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;}
Ultraviolet-11542 square (equations of different or)