UVA 11542 Square (math: Gaussian elimination)

Source: Internet
Author: User

Given the number of N, ask how many methods you have taken to make the product of the number taken is a complete square number

The thinking span of this question is still very big

Because the number of elements in the topic is not greater than 500

Consider starting with the only decomposition

Write this n number as the only form of decomposition

Find the exponent of the corresponding element factor

For example 4 6 10 15

That is:

4 = 2^2

6 = 2^1x3^1

Ten = 2^1x5^1

* = 3^1x5^1

So four numbers correspond to a prime number factor of up to 5

Get 4 vectors for:

X1 = (2, 0, 0)

x2 = (1, 1, 0)

x3 = (1, 0, 1)

x4 = (0, 1, 1)

The product of these four numbers can be written as

2^ (x1+x2+x3) x3^ (x2+x4) x5^ (x3+x4)

To make the result a complete square number, the exponent of each prime and even

So there are

(x2+x3)% 2 = 0 (x1%2 = = 0)

(x2+x4)% 2 = 0

(x3+x4)% 2 = 0

can be converted to: (here ^ denotes xor, above ^ denotes exponentiation)

x2 ^ x3 = 0

x2 ^ x4 = 0

x3 ^ x4 = 0

The corresponding augmented matrix is:


Next, the matrix can be solved by Gauss elimination method.

There's an x free variable.

The corresponding result is 1<<x-1 (0 not considered)

The code is as follows:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAXN 510 #d

Efine LL Long long using namespace std;
typedef int MATRIX[MAXN][MAXN];

int PRIME[MAXN];
    int gen_primes (int n) {int cnt = 2;
    Prime[0] = 2;
    PRIME[1] = 3;
        for (int i=4; i<=500; ++i) {bool OK = true;
                for (int j=0; j<cnt; ++j) {if (i%prime[j] = = 0) {ok = false;
            Break
        }} if (OK) {prime[cnt++] = i;
}} return CNT;
    } int Rank (Matrix A, int m, int n) {int I, j, K, R, U;
    i = j = 0;
        while (i<m && j<n) {r = i;
                for (k=i; k<m; ++k) {//Find the maximum row if (A[k][j]) {r = k) for column J 1;
            Break
                    }} if (A[r][j]) {if (r! = i) {//exchange the current line with line I for (k=0; k<n; ++k)
            Swap (A[r][k], a[i][k]);} for (u=i+1; u<m; ++u) {//From line i+1 to start Gaussian elimination if (A[u][j]) {for (k=i; k<=n;
                    ++K) {a[u][k] ^= a[i][k];
        }}} ++i;
    } ++j;
} return i;
    } int main (void) {LL x;
    int T, n, Maxp;
    int m = gen_primes (500);
    Matrix A;
    scanf ("%d", &t);
        while (t--) {maxp = 0;
        scanf ("%d", &n);
        memset (A, 0, sizeof (a));
            for (int i=0; i<n; ++i) {cin >> x;
                    for (int j=0; j<m; ++j) {while (x%prime[j] = = 0) {MAXP = max (Maxp, J);
                    x/= prime[j];
                A[j][i] ^= 1;
        }}}//printf ("Maxp =%d\n", MAXP);
        int r = Rank (A, maxp+1, N);
    cout << (1ll<< (n-r)) -1ll << Endl;
} return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.