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 ...
随机推荐
- HANA 与BW
本来备件折让有个挺麻烦的.我当时一度以为不能用BW做,其实自己也想用hana做,发现坤哥说的 的很有道理.BW都没有学懂,都是迷迷糊糊的.再去做hana 弄的四不像.先沉下心来,在bw里把这个项目做好 ...
- Log4j_学习_03_自己动手封装log工具
二.参考资料 1.log4j 是否可以通过条件判断 在程序运行中动态选择日志存储目录 2.log4j删除N天前日志实现
- MySQL+Node.js连接和操作
在本节中,您将学习如何使用mysql模块从node.js应用程序与MySQL进行交互. 我们将向您展示如何使用Node.js连接到MySQL,执行常用操作,如使用mysql模块API执行插入,选择,更 ...
- C# 如何将对象写入文件
http://wenku.baidu.com/link?url=QwDRlO1TeoubnmtUOitXXTRa-eZ6QFKvEuyXyzLXD9c0qCRUV5TL9Fq7_HqvxrMcwsAL ...
- [Luogu4177][CEOI2008]order
luogu sol 这题有点像网络流24题里面的太空飞行计划啊. 最大收益=总收益-最小损失. 先令\(ans=\sum\)任务收益. 源点向每个任务连容量为收益的边. 每个机器向汇点连容量为购买费用 ...
- 【LeetCode】002 Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- C# 使用API检查域用户名和密码是否正确
添加引用: using System.Runtime.InteropServices; public class VerifyUserByDomain { ; ; ); [DllImport(&quo ...
- MyEclipse安装jbpm插件
介绍如何在MyEclipse8.6里安装jbpm插件. 工具/原料 MyEclipse8.6 jbpm-4.4.rar 方法/步骤 1 下载jbpm包并解压 下载最新的jbpm包,本文以jbpm4.4 ...
- QrCode C#生成二维码 及JavaScript生成二维码
一 C#的二维码 示例: class Program { static void Main(string[] args) { QrEncoder qrEncoder = new QrEncode ...
- 【转载】Linux 进程调度时间测量
测试Context Switch time(进程上下文切换时间) -------------------------------------------------- 创建两个进程(实时进程) ...