30-盐水(分段dfs)
链接:https://www.nowcoder.com/acm/contest/94/K
来源:牛客网
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
输入描述:
输出描述:
每组数据输出一行,包含一个整数表示非空子集的个数。
输入例子:
1
5 1 2
1 2
1 2
1 2
1 2
1 4
输出例子:
15
-->
输入
1
5 1 2
1 2
1 2
1 2
1 2
1 4
输出
15 思路:由于n可以为35不能直接深搜,会爆的,所以可以将其分为两部分深搜去求和,又(a1 + a2)/ (b1 + b2) == x / y; 变形为: b1 * x - a1 * y == a2 * y - b2 * x; 可见分为了互不影响的两部分,a1,b1为第一个dfs求和,b2,a2为第二个
dfs求和,最后只有互为相反数即可为一组x/y的组合,同时可以转为map标记。
注意:数据范围。
#include <bits/stdc++.h>
using namespace std;
map <long long, long long> mp; //要用long long,因为sa = 1e4 * 35, x = 1e4, sa * x < 3e9超int了
long long ans;
int a[40], b[40];
int x, y, n; void dfs_before(int k, int sa, int sb){
if(k > n / 2){
mp[x * sb - sa * y]++;
return ;
}
dfs_before(k + 1, sa + a[k], sb + b[k]);
dfs_before(k + 1, sa, sb);
} void dfs_after(int k, int sa, int sb){
// cout << k << endl;
if(k >= n){
ans += mp[sa * y - sb * x];
return ;
}
dfs_after(k + 1, sa + a[k], sb + b[k]);
dfs_after(k + 1, sa, sb);
} int main(){
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
ans = 0;
mp.clear();
cin >> n >> x >> y;
for(int i = 0; i < n; i++){
cin >> a[i] >> b[i];
}
int m = n / 2;
dfs_before(0, 0, 0);
dfs_after(m + 1, 0, 0);
cout << ans - 1 << endl; //要减去一:sa = sb = 0的情况要除去,虽然符合推导的表达式,但是显然不合题意
}
return 0;
}
30-盐水(分段dfs)的更多相关文章
- PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)
1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight Wi assigne ...
- 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)
题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...
- PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...
- 1034 Head of a Gang (30分)(dfs 利用map)
One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...
- 天梯赛练习 L3-011 直捣黄龙 (30分) dijkstra + dfs
题目分析: 本题我有两种思路,一种是只依靠dijkstra算法,在dijkstra部分直接判断所有的情况,以局部最优解得到全局最优解,另一种是dijkstra + dfs,先计算出最短距离以及每个点的 ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
- 1018 Public Bike Management (30) Dijkstra算法 + DFS
题目及题解 https://blog.csdn.net/CV_Jason/article/details/81385228 迪杰斯特拉重新认识 两个核心的存储结构: int dis[n]: //记录每 ...
- PAT A1103 Integer Factorization (30 分)——dfs,递归
The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...
- PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]
题目 Input our current position and a destination, an online map can recommend several paths. Now your ...
随机推荐
- LNMP安装及配置
LNMP官方网站:http://lnmp.org http://oneinstack.com/install/ 安装详细介绍:http://lnmp.org/install.html 1,安装LNMP ...
- KVM- 日常管理与配置
KVM虚拟机的管理主要是通过virsh命令对虚拟机进行管理. 1. 查看KVM虚拟机配置文件及运行状态 (1) KVM虚拟机默认配置文件位置: /etc/libvirt/qemu/ autostar ...
- Redis 高可用及分片集群,说了你也不懂
Redis 简介 Memcached: 优点:高性能读写.单一数据类型.支持客户端式分布式集群.一致性hash 多核结构.多线程读写性能高. 缺点:无持久化.节点故障可能出现缓存穿透.分布式需要客户端 ...
- MySql 创建函数 Error Code : 1418
查看日志信息:show variables like 'log_%';显示'log_bin'.'log_bin_trust_function_creators'等状态 解决方法: 关闭binary l ...
- GO语言heap剖析及利用heap实现优先级队列
GO语言heap剖析 本节内容 heap使用 heap提供的方法 heap源码剖析 利用heap实现优先级队列 1. heap使用 在go语言的标准库container中,实现了三中数据类型:heap ...
- swing之borderlayout
import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; //1.继承 JFrame类 ...
- 使用 MLCC 替代电解电容需要注意几点 (2018-07-23)
使用 MLCC 替代电解电容需要注意几点 容量,MLCC 在高压时容量会降到标称的 30~50% 以下 1. MLCC 的 ESR 很低,比较适合高频 DCDC 输出. MLCC 会有压电效应,可能会 ...
- [Project Euler] 来做欧拉项目练习题吧: 题目013
问题描述: Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 371072 ...
- [转]实现微信浏览器内打开App Store链接
微信浏览器是不支持打开App Store 页面的,不知道微信为什么这么做.比如你页面写 <a href=”http://itunes.apple.com/us/app/id399608199″& ...
- lnmp下安装curl openssl扩展
openssl http://www.mamicode.com/info-detail-1957696.html curl https://blog.csdn.net/qq_34372929/arti ...