关闭

HDU-5833-Zhu and 772002【2016CCPC网络赛】【高斯消元】

标签: HDU5833高斯消元
499人阅读 评论(0) 收藏 举报
分类:

1002-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(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

题目链接:HDU-5833

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

题目思路:这道的原题是sgu-200,另外白皮书上也有代码。

求完全平方数,质因子的范围也知道,所以我们可以对每个质因子列出一个模意义下的方程(mod 2),然后求变元的个数就行了。

ans=2^num-1(去掉全部为0)

如何求变元的个数,就是在高斯消元的时候对于一个未知数xi,如果后面的方程xi的系数都为0的话,那么这个就是一个变元。

参考博客:here

以下是代码:

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
#define maxn 2100
ll vis[maxn];
ll prime[maxn]={0};
ll a[maxn];
ll fc[maxn][maxn]={{0}};
int n;
int pnum;
int get_primes(int m) {
    memset(vis, 0, sizeof(vis));
    int cnt = 1; //注意
    for (int i = 2; i < m; i++) {
        if (!vis[i]) {
            prime[cnt++] = i;
            for (int j = i * i; j < m; j += i)
                vis[j] = 1;
        }
    }
    return cnt;
}
void change(ll k,ll g)
{
    for(int i = 1; i <= pnum; )
    {
        if(k % prime[i] == 0)
        {
            fc[i][g] ^= 1;
            k /= prime[i];
        }
        else i++;
    }
    return;
}
ll gauss()
{
    ll g = 0;
    int i,j;
    for(j = 1; j <= n && g < pnum; j++)
    {
        for(i = g + 1; i <= pnum; i++)
            if(fc[i][j] == 1)
                break;
        if(i <= pnum)
        {
            g++;
            for(int p = j; p <= n; p++)
                swap(fc[g][p],fc[i][p]);
            for(int p = g + 1; p <= pnum; p++)
            {
                if(fc[p][j] == 1)
                {
                    for(int q = j; q <= n; q++)
                        fc[p][q] ^= fc[g][q];
                }
            }
        }
    }
    return g;
}
ll qpow(ll a,ll b,ll m)
{
    ll ans = 1;
    while(b){
        if(b & 1){
            ans = (ans * a) % m;
            b--;
        }
        b /= 2;
        a = a * a % m;
    }
    return ans;
}
int main(){
    int cas = 1;
    int t;
    cin >> t;
    get_primes(2100);
    pnum = 303;
    while(t--)
    {
        memset(fc,0,sizeof(fc));
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i],change(a[i],i);

        int r = gauss();  //求变元个数

        ll ans = qpow(2, (ll)(n - r), mod) - 1;
        printf("Case #%d:\n%lld\n",cas++,ans);
    }
}
0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:128018次
    • 积分:4491
    • 等级:
    • 排名:第6562名
    • 原创:328篇
    • 转载:7篇
    • 译文:0篇
    • 评论:23条
    关注博客
    博客专栏
    最新评论