Codeforces895C. Square Subsets

来源:互联网 发布:美工待遇 编辑:程序博客网 时间:2024/05/16 19:54
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
41 1 1 1
output
15
input
42 2 2 2
output
7
input
51 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,为70以内素数只有19个,所以很容易想到状压dp,先存下每个数出现多少次,然后dp转移,dp转移时分别计算某个数出现奇数次还是偶数次的方案数
ps :C(n,0)+C(n,2)+……=C(n,1)+C(n,3)+……=2^(n-1);
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;#define MAXN 60100#define MAXM 1000100const LL mod = 1e9 + 7;int s[100],cnt[100];int pri[19] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67 };int dp[71][1 << 19];int a[100005];int main(){int n, x;for (int i = 2; i <= 70; i++){int x = i;for (int j = 0; j < 19; j++){while (x%pri[j] == 0) s[i] ^= (1 << j), x /= pri[j];}}a[0] = 1;for (int i = 1; i <= 100004; i++)a[i] = (2 * a[i - 1]) % mod;while (~scanf("%d", &n)){memset(cnt, 0, sizeof cnt);memset(dp, 0, sizeof dp);for (int i = 0; i < n; i++){scanf("%d", &x);cnt[x]++;}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];continue;}for (int j = 0; j < (1 << 19); j++){dp[i][j^s[i]] = 1LL * (1LL*dp[i][j^s[i]] + 1LL*dp[i - 1][j] * a[cnt[i] - 1]) % mod;dp[i][j] = 1LL * (1LL*dp[i][j] + 1LL*dp[i - 1][j] * a[cnt[i] - 1]) % mod;}}printf("%d\n", (dp[70][0] - 1 + mod) % mod);}return 0;}


原创粉丝点击