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 ...
随机推荐
- 解析Ceph: RBDCache 背后的世界
转自:https://www.ustack.com/blog/ceph-internal-rbdcache/ RBDCache 是Ceph的块存储接口实现库 Librbd 的用来在客户端侧缓存数据的目 ...
- hihocoder 1049 后序遍历树
#include<cstdio> #include<cstdlib> #include<iostream> #include<cstring> #inc ...
- html 常用代码块
解决外边框不计入div尺寸的代码-moz-box-sizing: border-box;box-sizing: border-box;-webkit-box-sizing: border-box; 手 ...
- 运动目标跟踪中kalman滤波器的使用
目标跟踪的kalman滤波器介绍 Kalman滤波器是通过前一状态预测当前状态,并使用当前观测状态进行校正,从而保证输出状态平稳变化,可有效抵抗观测误差.因此在运动目标跟踪中也被广泛使用.在视频处理的 ...
- CodeForces - 961D:Pair Of Lines (几何,问两条直线是否可以覆盖所有点)
You are given n points on Cartesian plane. Every point is a lattice point (i. e. both of its coordin ...
- [ZOJ2587]Unique Attack
vjudge sol 最小割判定唯一性. 只要做完一个任意最小割后,判断一下是不是所有点都要么和\(S\)相连,要么和\(T\)相连. 只要两边各一次\(dfs\)就行了. code #include ...
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3.Monkey Script小案例
1.实现打开搜狗搜索APP,在搜索框输入内容,点击回车,重复2次运行 2.实现代码如下所示: type=user count=10 speed=1.0 start data >> Laun ...
- 解决docker 下来镜像出现 error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net的问题
http://f2d6cb40.m.daocloud.io [root@node2 ~]# docker --version ...
- 获得Oracke中刚插入的ID ---> GetInsertedID()
(1)首先 需要创建序列: CREATE SEQUENCE SE_TD_POWER MINVALUE 1 NOMAXVALUE START WITH 1 INCREMENT BY 1 NOCYCLE ...