cf 895C Square Subsets

一 原题

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.



二 分析

题意:给定n个1到70之间的数,问有多少种组合方式,使得选出的数的乘积是完全平方数。

在[1, 70]区间上共有19个素数,很容易想到把整数i表示为一个19维的0-1向量,每一维表示有偶/奇数个对应的素因子,把这样的向量记作mask[i]。两个数相乘则对应为两个向量在每个维度上做异或,完全平方数对应全0向量。

理解了这一点之后,这道题就可以用动态规划解决了,dp[i][j](i<=70, j<2^20)表示考虑输入中小于等于i的数,这些数相乘得到j表示的向量的方案数。递推方程为:
dp[i+1][j] += dp[i][j] * f1[i+1], dp[i+1][j^mask[i+1]] += dp[i][j] * f2[i+1]
其中f1[i]表示从输入中选取偶数个i的方案数,f2[i]表示从输入中选取奇数个i的方案数。

这道动归的状态表达方法之前没见识过,RBQ RBQ


三 代码

AC代码:
#include<iostream>
#include<cstring>
using namespace std;

const int inf = 72;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;

int n, times[inf];
bool prime[inf];
int mask[inf];
long long pow[maxn];
long long f1[inf], f2[inf];
long long dp[2][1 << 20]; // total 19 prime numbers in [1, 70]

void setMask() {
	for(int i = 1; i < inf; i++) {
		int cnt = 0;
		for(int j = 1; j <= i; j++) {
			if(!prime[j]) continue;
			int x = i;
			while(x % j == 0) {
				x /= j;
				mask[i] ^= (1 << cnt);
			}
			cnt++;
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	int val;
	for(int i = 0; i < n; i++) {
		cin >> val;
		times[val]++;
	}
	
	// prepare f1 and f2
	pow[0] = 1;
	for(int i = 1; i < maxn; i++) {
		pow[i] = (pow[i - 1] + pow[i -1]) % mod;
	}
	for(int i = 0; i < inf; i++) {
		if(times[i] == 0)
			f1[i] = 1, f2[i] = 0;
		else
			f1[i] = f2[i] = pow[times[i] - 1];
	}
	
	// prepare mask
	for(int i = 2; i < inf; i++) {
		prime[i] = true;
		for(int j = 2; j * j <= i; j++) {
			if(i % j == 0) {
				prime[i] = false;
				break;
			}
		}
	}
	setMask();

	// begin dp
	dp[0][0] = 1;
	for(int i = 1; i <= 70; i++) {
		int cur = i % 2;
		memset(dp[cur], 0, sizeof(dp[cur]));
		for(int j = 0; j < (1 << 20); j++) {
			dp[cur][j] += (dp[1 - cur][j] * f1[i]) % mod;
			dp[cur][j] %= mod;
			dp[cur][j ^ mask[i]] += (dp[1 - cur][j] * f2[i]) % mod;
			dp[cur][j] %= mod;
		}
	}
	
	cout << (dp[0][0] - 1) % mod << endl; // due to 70 is even, it's dp[0][0]

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值