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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值