HDU 5833 Zhu and 772002 高斯消元

Zhu and 772002

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5833

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(1≤n≤300),next line there are n numbers a1,a2,…,an,(1≤ai≤1018).

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

Hint

题意

给你n个数,然后让你选择一些数,乘起来成为完全平方数,问你有多少种方案。

题解:

完全平方数,就是这个数的质因数的次数都是偶数次。

那么我们对于每一个质因数,我们就可以列一个mod2方程组,A[i][j]就表示第j个数的这个i素数是奇数还是偶数。

然后就是求这个方程解的个数。

其实就是2^自由变远的个数,再减去全部不选就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2005;
const int mod = 1e9+7;
int p[2005],cnt,vis[2005],cas;
void init(){
    for(int i=2;i<maxn;i++){
        if(vis[i])continue;
        p[cnt++]=i;
        for(int j=i;j<maxn;j+=i)
            vis[j]=1;
    }
}
bitset<330> A[305];
void solve(){
    for(int i=0;i<305;i++)
        A[i].reset();
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        long long x;
        scanf("%lld",&x);
        for(int j=0;j<cnt;j++)
        {
            if(x%p[j]==0)
            {
                int flag = 0;
                while(x%p[j]==0)
                {
                    x/=p[j];
                    flag^=1;
                }
                A[j][i]=flag;
            }
        }
    }
    int i=0,j=0;
    for(i=0;i<n;i++)
    {
        int id=-1;
        for(int k=j;k<cnt;k++)
        {
            if(A[k][i])
            {
                id=k;
                break;
            }
        }
        if(id==-1)continue;
        swap(A[j],A[id]);
        for(int k=j+1;k<cnt;k++)
        {
            if(A[k][i])
                A[k]^=A[j];
        }
        j++;
    }
    int ans = 1;
    for(int i=0;i<n-j;i++)
        ans = ans * 2 % mod;
    printf("Case #%d:\n%d\n",++cas,ans-1);
}
int main(){
    init();
    int t;
    scanf("%d",&t);
    while(t--){
        solve();
    }
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5771155.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞