codeforces895c Square Subsets

17 篇文章 0 订阅
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
Copy
4
1 1 1 1
output
15
input
Copy
4
2 2 2 2
output
7
input
Copy
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.

题意:给出若干个数,问有多少个非空子集使得乘积为平方数。

因为ai很小只有70,而70以内的质数只有19个,所以我们进行状压dp。先预处理每个数的每个质因子的个数,之后分奇偶次转移即可。如果一个数字没有出现,那么直接无视。

偶数次对于质因子没有影响,奇数此有影响,要异或一下。

还有就是要乘以2^(cnti-1),这代表每个不同大小的数字怎么选。

代码:

#include <iostream>
using namespace std;
#define ll long long
int n,a[100005],cnt[75],dp[75][(1<<19)+5],s[75],prime[19]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67},pow[100005]={1};
int main()
{
	dp[0][0]=1;
    for(int i=1,t=i;i<=70;i++,t=i)
        for(int j=0;j<19;j++)
            while(t%prime[j]==0)
                t/=prime[j],s[i]^=(1<<j);
    for(int i=1;i<100005;i++) pow[i]=(pow[i-1]*2)%1000000007;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i],cnt[a[i]]++;
    for(int i=1;i<=70;i++)
    {
        if(!cnt[i]) 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^s[i]]=((ll)dp[i][j^s[i]]+(ll)pow[cnt[i]-1]*dp[i-1][j])%1000000007;
            dp[i][j]=((ll)dp[i][j]+(ll)pow[cnt[i]-1]*dp[i-1][j])%1000000007;
        }
    }
    cout<<(dp[70][0]-1)%1000000007<<endl;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值