[原]895C - Square Subsets 状压DP + 离散化

楚东方 17/11/29 11:27:11

C. Square Subsets
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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
Note

In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.

In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.






因为其给的a的数据范围较小,70  ,又其是求平方数,所以考虑质数分解.

其质数只有19个,考虑状压DP

由于n为100000,复杂度太大,考虑离散化


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N=1e5+5;
const int MOD=1e9+7;
int a[N];
int cnt[75];//离散化

ll dp[2][(1<<19)+1];
ll er[N];

int s[75];
int prime[19]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67};

int main()
{
//    freopen("data.txt","r",stdin);
    ios::sync_with_stdio(false);
    for(int i=1;i<=70;i++)
    {
        int t=i;
        for(int j=0;j<19;j++)
        {
            while(t%prime[j]==0)t/=prime[j],s[i]^=(1<<j);
        }
    }

    er[0]=1;
    for(int i=1;i<N;i++)er[i]=(er[i-1]*2)%MOD;

    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        cnt[a[i]]++;
    }

    dp[0&1][0]=1;
    for(int i=1;i<=70;i++)
    {
        if(!cnt[i])
        {
            for(int j=0;j<(1<<19);j++)
            {
                dp[i&1][j]=dp[(i+1)&1][j];
            }
        }
        else
        {
            for(int j=0;j<(1<<19);j++)
            {
                dp[i&1][j]=0;
            }
            for(int j=0;j<(1<<19);j++)
            {
                dp[i&1][j^s[i]]=(dp[i&1][j^s[i]]+er[cnt[i]-1]*dp[(i+1)&1][j])%MOD;//从cnt[i]个数个选奇数个,C(n,1)+C(n,3)+...=2^(n-1)
                dp[i&1][j]=(dp[i&1][j]+er[cnt[i]-1]*dp[(i+1)&1][j])%MOD;//从cnt[i]个数个选偶数个,C(n,0)+C(n,2)*C(n,4)+...=2^(n-1)
            }
        }
    }

    cout<<(dp[70&1][0]-1)%MOD<<endl;//减去0的情况
    return 0;
}










作者:chudongfang2015 发表于 2017/11/29 11:27:11 原文链接 http://blog.csdn.net/chudongfang2015/article/details/78663296
阅读:119