codeforces895c(状压dp)

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 modulo109 + 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.



题意:求n个元素的重集的元素积为某数平方的子集个数。元素ai<=70。

思路:由于ai很小,可以使用状态压缩,将70以内的所有质数求出来,打上标签,然后将70以内每个数的质因子情况用二进制压缩表示。对于集合中的元素,求出70以内每个数的个数,列状态转移方程。如果集合中不包含这个元素则跳过,否则,从i-1的每个状态加上这个元素后有两种情况,分别为加奇数个(状态改变),加偶数个(状态不变),两种情况可能性相同。


#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
ll pri[19] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67 };  //70以内素数表 
ll s[75];  //70以内每个数用二进制存储含有素数表中的每个素数的个数奇偶 
ll a[100005];  //a[i]=2^i 
int num[75];    //集合中每个数的个数 
int dp[72][1<<19];   //dp[i][j]表示从小于等于i的数中选择,其积的质因子状态为j时的选择方式数目 
int main()
{
	//预处理 
	for(int i=2;i<=70;i++)
	{
		s[i]=0;
		int tmp=i;
		for(int j=0;j<19;j++)
		{
			while(tmp%pri[j]==0)
			{
				tmp/=pri[j];
				s[i]^=(1<<j);
			}
		}
	}
	a[0]=1;
	for(int i=1;i<100005;i++)a[i]=(a[i-1]<<1)%mod;
	int n;
	while(~scanf("%d",&n))
	{
		memset(num,0,sizeof(num));
		memset(dp,0,sizeof(dp));
		int x;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&x);
			num[x]++;
		}
		dp[0][0]=1;
		for(int i=1;i<=70;i++)
		{
			if(!num[i])  //集合中没有i,则跳过 
			{
				for(int j=0;j<(1<<19);j++)dp[i][j]=dp[i-1][j];
			}
			else   //根据数字i的数目,一半情况为奇,一半情况为偶数 
			{
				for(int j=0;j<(1<<19);j++)
				{
					dp[i][j^s[i]]=(dp[i][j^s[i]]+dp[i-1][j]*a[num[i]-1])%mod;
					dp[i][j]=(dp[i][j]+dp[i-1][j]*a[num[i]-1])%mod;
				}
			}
		}
		printf("%d\n", (dp[70][0] - 1 + mod) % mod); //减去空集情况 
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值