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.
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.
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.
4 1 1 1 1
15
4 2 2 2 2
7
5 1 2 4 5 8
7
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;
}