HDU5833-Zhu and 772002(高斯消元)

63 篇文章 0 订阅
1 篇文章 0 订阅

Zhu and 772002

                                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                                  Total Submission(s): 1862    Accepted Submission(s): 656


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
 

Author
UESTC
 

Source
 

Recommend
wange2014
 


题意:给出n个数字,问有多少种方式取(1~n)个数字使得成为一个完美平方数

解题思路:一个数字分解后,可以用01表示分解后每种素数的个数,然后再用n个01变量xi来表示是否选择这个数,其中xi表示选第i个数,xi表示不选它,则可以对每个素数的幂列出一个模2的方程,有因为任意变量的系数非0即1,所以可以把方程组转化为xor方程组,然后对xor方程组进行消元,假设最后自由变量f个,则线性方程组的解共有2的f次方个。因本题不允许一个整数都不选,则最终答案减一。

白书原题P160


#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <cmath>  
#include <map>  
#include <cmath>  
#include <set>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <bitset>  
#include <functional>  

using namespace std;

#define LL long long  
const int INF = 0x3f3f3f3f;
const LL mod = 1000000007;

int a[500],visit[5005];
int v[500][500];
int n,cnt,sum,r;

void init()
{
    memset(visit, 1, sizeof visit);
    visit[0] = visit[1] = 0;
    for (int i = 2; i <= 2000; i++)
    {
        if (visit[i])
        {
            for (int j = 2 * i; j <= 2000; j += i)
                visit[j] = 0;
        }
    }
    cnt = 0;
    for (int i = 0; i < 2000; i++)
        if (visit[i]) a[cnt++] = i;
}

void Swap(int x, int y)
{
    if (x == y) return;
    for (int i = 0; i < n; i++)
        swap(v[x][i], v[y][i]);
}

void check(int x, int y)
{
    for (int i = x; i < cnt; i++)
    {
        if (v[i][y] != 0)
        {
            Swap(x, i);
            break;
        }
    }
}

void fxor(int x, int y)
{
    for (int i = 0; i < n; i++)
        v[y][i] = v[x][i] ^ v[y][i];
}

void f(int x, int y)
{
    for (int i = x + 1; i < cnt; i++)
        if (v[i][y] != 0) fxor(x, i);
}

LL getsum(LL x,LL y)
{
    LL ans = 1;
    while (y)
    {
        if (y % 2 == 1) ans = (ans * x) % mod;
        y >>= 1;
        x = (x * x) % mod;
    }
    return ans;
}

int main()
{
    init();
    int t, cas = 0;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        memset(v, 0, sizeof(v));
        for (int i = 0; i < n; i++)
        {
            LL x;
            scanf("%lld", &x);
            int res = 0;
            while (x != 1)
            {
                if (x%a[res] == 0)
                {
                    v[res][i]++;
                    x /= a[res];
                }
                else res++;
            }
        }
        for (int i = 0; i < cnt; i++)
            for (int j = 0; j < n; j++)
                v[i][j] %= 2;
        r = 0;
        for (int i = 0; i < n; i++)
        {
            check(r, i);
            if (v[r][i] != 0)
            {
                f(r, i);
                r++;
            }
        }
        r = n - r;
        LL ans=getsum(2, r) - 1;
        printf("Case #%d:\n%lld\n", ++cas, ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值