hdu 5833 Zhu and 772002 高斯消元

Zhu and 772002



Problem Description
Zhu and 772002 are both good at math. One day, Zhu wants to test the ability of 772002, so he asks 772002 to solve a math problem. 

But 772002 has a appointment with his girl friend. So 772002 gives this problem to you.

There are n numbers a1,a2,...,an. The value of the prime factors of each number does not exceed 2000, you can choose at least one number and multiply them, then you can get a number b.

How many different ways of choices can make b is a perfect square number. The answer maybe too large, so you should output the answer modulo by 1000000007.
 

 

Input
First line is a positive integer T , represents there are T test cases.

For each test case:

First line includes a number n(1n300),next line there are n numbers a1,a2,...,an,(1ai1018).
 

 

Output
For the i-th test case , first output Case #i: in a single line.

Then output the answer of i-th test case modulo by 1000000007.
 

 

Sample Input
2 3 3 3 4 3 2 2 2
 

 

Sample Output
Case #1: 3 Case #2: 3
 

题意:

  给你n个数,每个数的素数因子最大不超过2000,从n个数取出任意个至少一个,问有多少种方案使得数的乘积为完全平方数。

题解:

  将所有素数处理出来

  答案就是 每种素数个数为偶数个

  将每个素数使用个数设为x 那么最终mod 2 = 0

  那么有303个这样的方程

  高斯消元求解异或方程组

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 2e3+10, M = 1e6, mod = 1000000007, inf = 2e9;

int p[N],H[N],cnt,A[500][500],mx,ini[N];
LL a[N];
void init() {
        for(int i = 2; i <= 2000; ++i) {
            if(!H[i]) {
                p[cnt++] = i;
                for(int j = i+i; j <= 2000; j += i) {
                        H[j] = 1;
                }
            }
        }
        ini[0] = 1;
        for(int i = 1; i<= 2000; ++i) ini[i] = ini[i-1]*2%mod;
}
LL Guass(int n,int m) {
        int i, j;
        for(i = 0, j = 0; i < n && j < m; ) {
            int x = -1;
            for(int k = i; k < n; ++k) {
                if(A[k][j]) {
                    x = k;
                    break;
                }
            }
            if(x == -1) {
                ++j;
                continue;
            }
            for(int k = 0; k <= m; ++k) swap(A[i][k],A[x][k]);
            for(int u = i+1; u < n; ++u) {
                if(A[u][j]) {
                    for(int k = 0; k <= m; ++k) {
                        A[u][k] ^= A[i][k];
                    }
                }
            }
            ++i;
            ++j;
        }
        return m-i;
}
int main() {
        int T,cas = 1,n;
        init();
        scanf("%d",&T);
        while(T--) {
            scanf("%d",&n);
            mx = 0;
            for(int i = 0; i < n; ++i) scanf("%I64d",&a[i]);
            memset(A,0,sizeof(A));
            for(int i = 0; i < n; ++i) {
                for(int j = 0; j < cnt; ++j) {
                    while(a[i] % p[j] == 0) {
                        A[j][i] ^= 1;
                        a[i] /= p[j];
                        mx = max(mx,j+1);
                    }
                }
            }
            printf("Case #%d:\n",cas++);
            cout<<ini[Guass(mx,n)] - 1<<endl;
        }
        return 0;
}

 

posted @ 2016-10-03 19:26  meekyan  阅读(267)  评论(0编辑  收藏  举报