HDU 5833 Zhu and 772002 (高斯消元)

Zhu and 772002

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

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   |   We have carefully selected several similar problems for you:  5841 5840 5838 5837 5836 

题意:给你n个数,选择一些数字乘积为平方数的选择方案数。

题解:将原题化成异或方程组,并用高斯消元求解矩阵的秩.
每一个数字分解质因数。比如4,6,10,15。
4 = 2^2 * 3^0 * 5^0 . (^在这里是指数)
6 = 2^1 * 3^1 * 5^0.
10 = 2^1 * 3^0 *  5^1.
15 = 2^0 * 3^1 * 5^1.
令Xi表示选择第i个数字,那么,如果p=2^(2x1+x2+x3) * 3^(x2+x4) * 5^(x3+x4) 是平方数,那么每个质因数上的指数为偶数,x1系数为2已经是偶数不考虑。可以转换为异或为0判断偶数,即奇数置为1,偶数置为0,然后n个数字m个质因数的增广矩阵消元(gauss消元)看有几个自由变量(取0或1无所谓),答案是 2^r - 1( 全部都不取方案不算)(快速幂ok)。

另外:可以看一下卿学姐写的代码,觉得用了bitset很好,代码简便。点我打开链接

代码: 
#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
#include<queue>
#include<set>
#include<stack>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mst(a) memset(a, 0, sizeof(a))
#define M_P(x,y) make_pair(x,y)  
#define rep(i,j,k) for (int i = j; i <= k; i++)  
#define per(i,j,k) for (int i = j; i >= k; i--)  
#define lson x << 1, l, mid  
#define rson x << 1 | 1, mid + 1, r  
const int lowbit(int x) { return x&-x; }  
const double eps = 1e-8;  
const int INF = 1e9+7; 
const ll inf =(1LL<<62) ;
const int MOD = 1e9 + 7;  
const int mod = 1000000007;
const int N = 2010; 
const int M=100010; 
template <class T1, class T2>inline void getmax(T1 &a, T2 b) {if (b>a)a = b;}  
template <class T1, class T2>inline void getmin(T1 &a, T2 b) {if (b<a)a = b;}
int read()
{
	int v = 0, f = 1;
	char c =getchar();
	while( c < 48 || 57 < c ){
		if(c=='-') f = -1;
		c = getchar();
	}
	while(48 <= c && c <= 57) 
		v = v*10+c-48, c = getchar();
	return v*f;
}
typedef int Matrix[N][N];
int prime[N], vis[N];
Matrix A;
int init(int m)
{
    memset(vis, 0, sizeof(vis));
    int ans = 0;
    for (int i = 2; i < m; i++){
        if (!vis[i]) {
            prime[ans++] = i;
            for (int j = i * i; j < m; j += i)
                vis[j] = 1;
        }
    }
    return ans;
}
int gauss(Matrix A, int m, int n) {
    int i = 0, j = 0 ;
	int 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;
}
ll q_mod(ll a,ll b,ll m)
{
    ll ans = 1;
    while(b)
	{
        if(b&1)   	
            ans = (ans * a) % m;
          b>>=1;
        a = a * a % m;
    }
    return ans;
}
int main()
{
	#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    int m = init(2010);
    int t;
    int cas= 1;
    t=read();
    while (t--)
	{
        int n;
		int maxx= 0;
        ll x;
        n=read();
        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)
				{
                    maxx = max(maxx, j);
                    x /= prime[j];
                    A[j][i] ^= 1;
                }
        }
        int r = gauss(A, maxx+1, n);
        int ans = q_mod(2, (ll)(n-r), mod);
        printf("Case #%d:\n%d\n", cas++ , ans-1);
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值