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 ...
随机推荐
- VSCode设置中文语言显示
Vscode是一款开源的跨平台编辑器.默认情况下,vscode使用的语言为英文(us),如何将其显示语言修改成中文了? 1)打开vscode工具: 2)使用快捷键组合[Ctrl+Shift+p],在搜 ...
- pandas 中文快速查询手册
本文翻译自文章: Pandas Cheat Sheet - Python for Data Science ,同时添加了部分注解. 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非 ...
- vue组件父子组件传递引用类型数据
今天在写分页功能时,发现父子组件传值时,子组件监听不到父组件中数据的变化,传递的是一个引用类型的数据 其原因是引用类型共用一个内存地址,父子组件用的是同一个对象,故子组件监听不到变化,此时就需要做一个 ...
- Gitlab上如何给指定人员在指定项目里设置指定权限,给项目设置保护
一.在Gitlab页面里,点击Project,找到指定的Project里: 二.点击Members,可以添加人员,并给指定的人员设置权限 三.点击Protected Branches,可以给Proje ...
- Struts2(1)
一.struts概述 1.struts2框架应用在javaee三层结构中web层框架 2.struts2框架在struts1和webwork基础之上发展全新的框架 二.struts2环境搭建 1.导包 ...
- CodeChef - SQRGOOD:Simplify the Square Root (求第N个含平方因子数)
Tiny Wong the chef used to be a mathematics teacher in a senior high school. At that time, he always ...
- 观后感|当幸福来敲门 The Pursuit of Happyness
更好的阅读体验请点击:当幸福来敲门 The Pursuit of Happyness 看到时光机点亮的那一刻,我想儿子克里斯托夫正在侏罗纪的世界内探险,看着山川河流,穿梭在恐龙的脚下,在山洞中安稳的度 ...
- LeetCode Design Compressed String Iterator
原题链接在这里:https://leetcode.com/problems/design-compressed-string-iterator/description/ 题目: Design and ...
- python沉淀之路~~整型的属性
python的基础知识: 基本数据类型:int str list tuple dict bool 一.整型的属性功能 1.工厂方法将字符串转换成整型 a = " b = ...
- 【ASP.NET Web API2】初识Web API
Web Api 是什么? MSDN:ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动设备)的 HTTP 服务 百度百科:Web API是网络应用程序接口. ...