Codeforces 895C Square Subsets(状压DP 或 异或线性基)
题目链接 Square Subsets
这是白书原题啊
先考虑状压DP的做法
$2$到$70$总共$19$个质数,所以考虑状态压缩。
因为数据范围是$70$,那么我们统计出$2$到$70$的每个数的个数然后从$2$考虑到$70$。
设$dp[x][mask]$为考虑到$x$这个数的时候,$x$这个数和之前的所有数中,选出某些数,他们的乘积分解质因数,所有的指数对$2$取模之后,
状态为$mask$的方案数。
然后就可以转移了……这个状压DP花了我好几个小时……真是弱啊
哦对最后还要特判$1$的情况。
每个$1$选或不选都可以,然后考虑只选$1$的情况,累加即可。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const LL mod = 1e9 + 7;
const int N = 3e6 + 10;
const int M = 1e5 + 10; int c[101], p[30], m[101];
int n, x, cnt, now, all;
bool flag;
LL two[M], f[2][N]; void up(LL &a, LL b) { a = (a + b) % mod;} void init(){
two[0] = 1;
rep(i, 1, 100000) two[i] = two[i - 1] * 2 % mod; scanf("%d", &n);
rep(i, 1, n) scanf("%d", &x), ++c[x]; rep(i, 2, 70){
flag = true;
rep(j, 2, i - 1) if (i % j == 0){
flag = false;
break;
}
if (flag) p[cnt++] = i;
} rep(i, 1, 70){
int y = i;
rep(j, 0, cnt - 1){
int tt = 0;
while (y % p[j] == 0) y /= p[j], ++tt;
if (tt & 1) m[i] |= (1 << j);
}
}
} int main(){ init();
all = (1 << cnt) - 1;
rep(i, 2, 70){
if (c[i] == 0) continue;
memset(f[now ^ 1], 0, sizeof f[now ^ 1]);
LL a1 = two[c[i] - 1], a2 = (a1 - 1 + mod) % mod; up(f[now ^ 1][m[i]], a1);
up(f[now ^ 1][0], a2); rep(mask, 0, all) up(f[now ^ 1][mask ^ m[i]], f[now][mask] * a1 % mod);
rep(mask, 0, all) up(f[now ^ 1][mask], f[now][mask] * a2 % mod);
rep(mask, 0, all) up(f[now ^ 1][mask], f[now][mask]);
now ^= 1;
} LL ans = f[now][0];
ans = (ans * two[c[1]]) % mod;
up(ans, (two[c[1]] - 1 + mod) % mod);
printf("%lld\n", ans);
return 0;
}
还有一种就是考虑异或线性基的做法。
如果一个数可以被当前线性基中的数表示出来,那么这个数就相当于一个完全平方数。
选与不选两种状态。
令最后线性基中的数的个数为$x$
最后答案就是$2^{n - x} - 1$
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int mod = 1e9 + 7; int c[101], p[30], m[101], b[100010];
int n, x, cnt, now, all;
int ans = 0;
int ret;
bool flag; struct lb{
int d[70];
int cnt;
void clear(){
memset(d, 0, sizeof d);
cnt = 0;
}
bool ins(int val){
dec(i, 30, 0) if (val & (1 << i)){
if (!d[i]){ d[i] = val; break; }
val ^= d[i];
}
return val > 0;
}
int qmax(){
int ret = 0;
dec(i, 30, 0) if ((ret ^ d[i]) > ret) ret ^= d[i];
return ret;
}
int qmin(){
rep(i, 0, 30) if (d[i]) return d[i];
return 0;
}
} LB; void init(){ scanf("%d", &n);
rep(i, 1, n) scanf("%d", b + i), ++c[x]; rep(i, 2, 70){
flag = true;
rep(j, 2, i - 1) if (i % j == 0){
flag = false;
break;
}
if (flag) p[cnt++] = i;
} rep(i, 1, 70){
int y = i;
rep(j, 0, cnt - 1){
int tt = 0;
while (y % p[j] == 0) y /= p[j], ++tt;
if (tt & 1) m[i] |= (1 << j);
}
}
} int main(){ init();
rep(i, 1, n) LB.ins(m[b[i]]);
rep(i, 0, 30) if (LB.d[i]) ++ans;
ret = 1;
rep(i, 1, n - ans) ret = ret * 2 % mod;
ret += mod - 1;
ret %= mod;
printf("%d\n", ret);
return 0;
}
Codeforces 895C Square Subsets(状压DP 或 异或线性基)的更多相关文章
- Codeforces 895C - Square Subsets 状压DP
题意: 给了n个数,要求有几个子集使子集中元素的和为一个数的平方. 题解: 因为每个数都可以分解为质数的乘积,所有的数都小于70,所以在小于70的数中一共只有19个质数.可以使用状压DP,每一位上0表 ...
- Codeforces 895C - Square Subsets
895C - Square Subsets 思路:状压dp. 每个数最大到70,1到70有19个质数,给这19个质数标号,与状态中的每一位对应. 状压:一个数含有这个质因子奇数个,那么他状态的这一位是 ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- Codeforces 917C - Pollywog(状压 dp+矩阵优化)
UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...
- Codeforces 79D - Password(状压 dp+差分转化)
Codeforces 题目传送门 & 洛谷题目传送门 一个远古场的 *2800,在现在看来大概 *2600 左右罢( 不过我写这篇题解的原因大概是因为这题教会了我一个套路罢( 首先注意到每次翻 ...
- Codeforces 544E Remembering Strings 状压dp
题目链接 题意: 给定n个长度均为m的字符串 以下n行给出字符串 以下n*m的矩阵表示把相应的字母改动成其它字母的花费. 问: 对于一个字符串,若它是easy to remembering 当 它存在 ...
- codeforces 21D. Traveling Graph 状压dp
题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...
- CodeForces 327E Axis Walking(状压DP+卡常技巧)
Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub ...
- Codeforces ----- Kefa and Dishes [状压dp]
题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...
随机推荐
- 自动化运维工具——pssh
PSSH介绍 pssh是一个python编写可以在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具中很出色的.它的用法可以媲美ansible的一些简单用法,执行起来速度比ansible快它支持 ...
- 七周成为数据分析师06_MySQL
关于 MySQL 的知识,主要也是一些实操和练习. 因为个人之前已经专门练习过 MySQL 操作,这里就不做笔记,之后另写一篇博文记录 MySQL 知识. 同时附上本课程对应的文字教程: 如何七周成为 ...
- JAVA基础篇—继承
父类Vehicle package com.car; public class Vehicle { final String brand;// String color;// double speed ...
- LeetCode(228) Summary Ranges
题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...
- Cinder配置多Ceph后端步骤
1. 检查cinder当前backend配置 使用cinder service-list,查看cinder-volume服务的Host字段格式. 旧版格式: 新版格式: 旧版中Host字段是cinde ...
- ubuntu12.04 ppa安装git
PPA地址:https://launchpad.net/~git-core/+archive/ppa 支持所有的Ubuntu版本. 运行命令: sudo apt-add-repository ppa: ...
- 【转】git bash here 右键菜单失效后的修复方法
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\git_shell] @="Git Ba&sh Here" [ ...
- 枚举进程——暴力搜索内存(Ring0)
上面说过了隐藏进程,这篇博客我们就简单描述一下暴力搜索进程. 一个进程要运行,必然会加载到内存中,断链隐藏进程只是把EPROCESS从链表上摘除了,但它还是驻留在内存中的.这样我们就有了找到它的方法. ...
- electron 安装
1.从网上下载的是nodejs的v0.10.42版本的安装文件,安装node-v0.10.42-x86.msi都使用默认安装,安装完成后会安装在C:\Program Files\nodejs\目录下, ...
- PHP函数参数传递(相对于C++的值传递和引用传递)
学语言学得比较多了,今天突然想PHP函数传递,对于简单类型(基本变量类型)和复杂类型(类)在函数参数传递时,有没有区别呢,今天测试了下: 代码如下: <?php function test($a ...