Codeforces Round #448 (Div. 2) C. Square Subsets
来源:互联网 发布:mac电磁阀官网 编辑:程序博客网 时间:2024/06/05 16:11
C. Square Subsets
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
转自:http://blog.csdn.net/zstu_zy/article/details/78643674
题意:给出1e6个数,每次可以从这n个数里选任意个数,问有多少种选法使得选出的数 是某个数的平方,
做法:引文每个数之后70,1到70只有19个质数,所以可以状压dp,枚举每个质数的奇偶情况,然后对1到70枚举每次选择奇数个或者偶数个。
#include<bits/stdc++.h>using namespace std;bool vis[100];const int mod = 1e9+7;bool check(int x){ if(x < 2) return false; for(int i= 2;i*i <= x;i ++){ if(x %i == 0) return false; } return true;}int cnt[100];int dp[100][1<<19];//表示前i的数中, 几何状态为j的时候的方案个数。int qp[100050];vector<int> v;int ss[100];void init(){ for(int i= 2;i <= 70;i ++){ for(int j = 0;j < v.size();j ++){ int tmp = i; while(tmp % v[j] == 0){ tmp /= v[j]; ss[i] ^= (1<<j); } } } qp[0] = 1; for(int i = 1;i < 100050;i ++) qp[i] = qp[i-1]*2%mod;}int main(){ for(int i = 1;i <= 70;i ++) if(check(i)) {vis[i] = true;v.push_back(i);} init(); int n; cin>> n; for(int i= 1;i<= n;i ++) { int now; scanf("%d",&now); cnt[now]++; } 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]; } } else { for(int j = 0;j <(1<<19);j ++){ dp[i][j^ss[i]] = (dp[i][j^ss[i]] + 1ll*qp[cnt[i]-1] * dp[i-1][j])%mod;// 奇数个元素的子集个数。 dp[i][j] = (dp[i][j]+1ll*qp[cnt[i]-1] * dp[i-1][j])%mod;// 偶数个元素的子集个数。 } } } cout << dp[70][0]-1 << endl;//非空子集个数。 return 0;}
- Codeforces Round #448 (Div. 2) C. Square Subsets
- Codeforces Round #448 (Div. 2) C. Square Subsets
- Codeforces Round #448 (Div. 2) C. Square Subsets
- Codeforces Round #448 (Div. 2) C. Square Subsets(状压)
- Codeforces Round #448 (Div. 2): C. Square Subsets(线性基)
- Codeforces Round #372 (Div. 2) C. Plus and Square Root
- Codeforces Round #122 (Div. 2) B. Square
- Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))
- 【Codeforces Round #372 (Div. 2)】Codeforces 716C Plus and Square Root
- Codeforces Round #247 (Div. 2)a.Black Square,b.Shower Line,c.k-Tree前三题
- Codeforces Round #372 (Div. 2) C. Plus and Square Root 数论、方程、化简、推公式
- Codeforces Round #372 (Div. 2) -- C. Plus and Square Root(找规律枚举)
- Codeforces Round #372 (Div. 2) C. Plus and Square Root【二分】
- Codeforces Round #372 (Div. 2) C. Plus and Square Root —— 构造(数学题)
- Codeforces Round #448 (Div. 2) B,C,
- Codeforces Round #247 (Div. 2) - A. Black Square
- Codeforces Round #247(Div. 2) A. Black Square 水题
- Codeforces Round #356 (Div. 2) Bear and Square Grid
- 什么是闭包?
- List存取
- springMVC注解方式的校验
- MyBatis学习笔记--第一天
- 最新阿里云服务器优惠活动汇总
- Codeforces Round #448 (Div. 2) C. Square Subsets
- 轮子哥博客
- 【转载】Ubuntu 安装 OpenVas
- APK重新签名的方法
- docker安装redis
- html的head中异步按顺序加载script脚本的兼容写法
- 互联网+时代的企业应用集成平台
- python/scipy求解非线性方程(fsolve/root)
- Redis 数据存储位置 导出数据