Codeforces Round #448 (Div. 2): C. Square Subsets(线性基)
来源:互联网 发布:魔兽世界 mac版 cruse 编辑:程序博客网 时间:2024/06/06 13:05
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.
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.
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.
41 1 1 1
15
42 2 2 2
7
51 2 4 5 8
7
会线性基的话,这题就是5分钟题
不然要想很久+码很久的状压DP,或者卡到比赛结束
题意:
给你n个数,每个数<=70,问有多少个集合,满足集合中所有数相乘是个完全平方数(空集除外)
思路:
很容易想到的是:一个完全平方数分解质因数后所有的因子一定都出现偶数次,而这题的ai<=70
小于70的质数有且只有19个,这样就可以将每个数转成一个长度为19的二进制
第i位为1就说明分解质因数后第i个质数出现了奇数次,为0即出现偶数次,之后再把它转回十进制的另一个数
这样问题就变成了n个数,有多少个集合满足它们异或为0!
求出这n个数的线性基,答案就是2^(n-线性基内数的个数)-1
可以用高斯消元证明
#include<stdio.h>#define mod 1000000007#define LL long longint a[100005], p[21];int z[21] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67};LL Pow(LL x, int y){LL ans = 1;while(y){if(y%2)ans = ans*x%mod;x = x*x%mod;y /= 2;}return ans;}int main(void){int n, i, x, j, now, sum;scanf("%d", &n);for(i=1;i<=n;i++){scanf("%d", &x);for(j=0;j<=18;j++){now = 0;while(x%z[j]==0)x /= z[j], now ^= 1;a[i] |= now*(1<<j);}}for(i=1;i<=n;i++){for(j=18;j>=0;j--){if(a[i]&(1<<j)){if(p[j]==0){p[j] = a[i];break;}elsea[i] ^= p[j];}}}sum = n;for(j=0;j<=18;j++){if(p[j])sum--;}printf("%lld\n", Pow(2ll, sum)-1);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 895C Square Subsets 线性基 或 状压DP
- Codeforces Round #365 (Div. 2) C题(线性约束)
- 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 #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 #448 (Div. 2) B,C,
- Codeforces Round #287 (Div. 2) C
- java面试题
- CodeForces 141B Hopscotch(分类讨论)
- WPF中使用cs代码设置附加属性的方式
- 贝叶斯网的推理模式
- STL工具库使用解析系列之一 STL::map
- Codeforces Round #448 (Div. 2): C. Square Subsets(线性基)
- 图像处理入门必看
- tinymce编辑器的高度随内容自动变化
- IM多点登录与消息漫游架构随想
- 计算机组成与设计(一)——计算机概要与技术
- 内核模块已打开,但开机未加载
- Android Scrollview上滑停靠—悬浮框停靠在标题栏下方(防微博详情页)
- React中的props和state
- 前序——中序建树