Codeforces Round #448 (Div. 2) C. Square Subsets
来源:互联网 发布:数据库2008安装教程 编辑:程序博客网 时间:2024/05/29 15:03
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
题意:给出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];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
- 练习1-13 编写一个程序,打印输入中单词长度的垂直方向的直方图(垂直图)
- 编码解码原理
- 四、界面编程(四) View的滑动及滑动冲突详解
- Spring AOP 测试需要POM添加的jar包
- iOS11和iPhone X适配(这里有一大坑)
- Codeforces Round #448 (Div. 2) C. Square Subsets
- 8080端口被占用,Address already in use: JVM_Bind <null>:8080
- Okhttp 请求添加公共参数、公共Headers 方法
- 位运算
- 基础排序:冒泡排序
- vue项目中从服务器获取图片
- linux top 命令
- java根据URL获取json对象
- ideEA开发。javaweb