链接:https://www.nowcoder.com/acm/contest/94/K
来源:牛客网

时间限制:C/C++ 5秒,其他语言10秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

小马哥有杯盐水,第杯有单位的盐和单位的水。小马哥很无聊,于是他想知道有多少种这杯盐水的非空子集,倒在一起之后盐和水的比是

输入描述:

输入第一行包含一个整数,代表数据组数。
每组数据第一行包含三个整数表示的最大公约数。
接下来行,第行包含两个整数

输出描述:

每组数据输出一行,包含一个整数表示非空子集的个数。

输入例子:
1
5 1 2
1 2
1 2
1 2
1 2
1 4
输出例子:
15

-->

示例1

输入

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)的更多相关文章

  1. 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 W​i​​ assigne ...

  2. 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)

    题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...

  3. 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 ...

  4. 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 ...

  5. 天梯赛练习 L3-011 直捣黄龙 (30分) dijkstra + dfs

    题目分析: 本题我有两种思路,一种是只依靠dijkstra算法,在dijkstra部分直接判断所有的情况,以局部最优解得到全局最优解,另一种是dijkstra + dfs,先计算出最短距离以及每个点的 ...

  6. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  7. 1018 Public Bike Management (30) Dijkstra算法 + DFS

    题目及题解 https://blog.csdn.net/CV_Jason/article/details/81385228 迪杰斯特拉重新认识 两个核心的存储结构: int dis[n]: //记录每 ...

  8. 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 ...

  9. PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]

    题目 Input our current position and a destination, an online map can recommend several paths. Now your ...

随机推荐

  1. ubuntu设置开机启动命令行模式

    1.命令: sudo gedit /etc/default/grub 找到这一行 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" (ubuntu ...

  2. LeetCode OJ:Lowest Common Ancestor of a Binary Search Tree(最浅的公共祖先)

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  3. java学习笔记 --- 多线程(线程安全问题——同步代码块)

    1.导致出现安全问题的原因: A:是否是多线程环境 B:是否有共享数据 C:是否有多条语句操作共享数据 2.解决线程安全问题方法: 同步代码块: synchronized(对象){ 需要同步的代码; ...

  4. uva11292 Dragon of Loowater(排序后贪心)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  5. Unity物体上下反复漂浮效果

    using UnityEngine;using System.Collections;// 主界面的开始按钮使用该脚本,控制上下来回浮动public class Floating : MonoBeha ...

  6. 开始写博客,与ITer们互相学习

    学习计算机也6年了,一直待在学校里.这些年来很多学习资料都来自网络上的技术博客,非常感谢各位 ITer 的分享精神,鄙人从中受益匪浅.从今天起也挤出时间开始写技术博客.主要是把自己这些年的一些技术文档 ...

  7. ThreadPoolTaskExecutor异常收集

    ThreadPoolTaskExecutor ipFinderThreads = new ThreadPoolTaskExecutor(); ipFinderThreads.setCorePoolSi ...

  8. Rails的静态资源管理(二)—— 如何使用 Asset Pipeline

    官方文档:http://guides.ruby-china.org/asset_pipeline.html http://guides.rubyonrails.org/asset_pipeline.h ...

  9. 人脑和CPU

    人类的数学运算没有计算机快是因为神经信号速度没有电信号快吗,电信号是光速吧. 不过人类的cpu大脑和存储硬盘和内存超过目前计算机n条街,虽然传输速度慢,但是传输量也是大的,其实计算机就是根据人脑设计的 ...

  10. maven依赖的添加

      maven可是个管理jar依赖的好玩意,不用再关心导这个jar包那个jar包,这个jar包是谁家的,和谁有啥关系.有了maven,简简单单就搞定,下面以eclipse为例,在一个springboo ...