题解 CF895C 【Square Subsets】
我们设 ,考虑什么时候 是完全平方数。容易发现如果所有 都是偶数,此时 为整数,即 是完全平方数;否则 不是完全平方数。
题目转换为原序列 有多少个非空子集乘积质因数分解后质数均为偶数。
发现奇偶性是满足异或性质的,奇数对应 ,偶数对应 。考虑将原序列的每一个 进行质因数分解,将分解后每个质因数的指数奇偶性二进制压位成一个整数,存到线性基中。
假设线性基中有 个元素,那么剩下的 个数可以用线性基的元素异或表示,可以任意取,共有 种取法,注意减掉全都不取的一种就是答案。
//By: Luogu@rui_er(122461)
#include <bits/stdc++.h>
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define per(x,y,z) for(int x=y;x>=z;x--)
#define debug printf("Running %s on line %d...\n",__FUNCTION__,__LINE__)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
using namespace std;
typedef long long ll;
const int N = 1e5+5, mod = 1e9+7;
const int pm[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67};
int n, a[N];
template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}
struct LinearBasis {
int p[19];
LinearBasis() {memset(p, 0, sizeof(p));}
~LinearBasis() {}
void insert(int x) {
per(i, 18, 0) {
if(!((x >> i) & 1)) continue;
if(!p[i]) {p[i] = x; return;}
x ^= p[i];
}
}
int qmax() {
int ans = 0;
per(i, 18, 0) chkmax(ans, ans^p[i]);
return ans;
}
int cnt() {
int ans = 0;
per(i, 18, 0) ans += !!p[i];
return ans;
}
}LB;
int qpow(int x, int y) {
int ans = 1;
for(;y;y>>=1,x=1LL*x*x%mod) if(y & 1) ans = 1LL * ans * x % mod;
return ans;
}
int main() {
scanf("%d", &n);
rep(i, 1, n) {
scanf("%d", &a[i]);
int now = 0;
per(j, 18, 0) {
now <<= 1;
while(!(a[i] % pm[j])) {
now ^= 1;
a[i] /= pm[j];
}
}
LB.insert(now);
}
printf("%d\n", (qpow(2, n-LB.cnt())+mod-1)%mod);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现