Codeforces Round #448 (Div. 2) C. Square Subsets

来源:互联网 发布:数据库2008安装教程 编辑:程序博客网 时间:2024/05/29 15:03

C. Square Subsets
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.

Two ways are considered different if sets of indexes of elements chosen by these ways are different.

Since the answer can be very large, you should find the answer modulo 109 + 7.

Input
First line contains one integer n (1 ≤ n ≤ 105) — the number of elements in the array.

Second line contains n integers ai (1 ≤ ai ≤ 70) — the elements of the array.

Output
Print one integer — the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.

Examples
input
4
1 1 1 1
output
15
input
4
2 2 2 2
output
7
input
5
1 2 4 5 8
output
7

题意:给出1e6个数,每次可以从这n个数里选任意个数,问有多少种选法使得选出的数 是某个数的平方,
做法:引文每个数之后70,1到70只有19个质数,所以可以状压dp,枚举每个质数的奇偶情况,然后对1到70枚举每次选择奇数个或者偶数个。

#include<bits/stdc++.h>using namespace std;bool vis[100];const int mod = 1e9+7;bool check(int x){    if(x < 2) return false;    for(int i= 2;i*i <= x;i ++){        if(x %i == 0) return false;    }    return true;}int cnt[100];int dp[100][1<<19];int qp[100050];vector<int> v;int ss[100];void init(){    for(int i= 2;i <= 70;i ++){        for(int j = 0;j < v.size();j ++){            int tmp = i;            while(tmp % v[j] == 0){                tmp /= v[j];                ss[i] ^= (1<<j);            }        }    }    qp[0] = 1;    for(int i = 1;i < 100050;i ++) qp[i] = qp[i-1]*2%mod;}int main(){    for(int i = 1;i <= 70;i ++) if(check(i)) {vis[i] = true;v.push_back(i);}    init();    int n;    cin>> n;    for(int i= 1;i<= n;i ++) {        int now;        scanf("%d",&now);        cnt[now]++;    }    dp[0][0] = 1;    for(int i= 1;i <= 70;i ++){        if(cnt[i] == 0) {            for(int j = 0;j < (1<<19);j ++){                dp[i][j] = dp[i-1][j];            }        }        else {            for(int j = 0;j <(1<<19);j ++){                dp[i][j^ss[i]] = (dp[i][j^ss[i]] + 1ll*qp[cnt[i]-1] * dp[i-1][j])%mod;                dp[i][j] = (dp[i][j]+1ll*qp[cnt[i]-1] * dp[i-1][j])%mod;            }        }    }    cout << dp[70][0]-1 << endl;    return 0;}
阅读全文
0 0
原创粉丝点击