HDU 6627 equation (分类讨论)
2019 杭电多校 5 1004
题目链接:HDU 6627
比赛链接:2019 Multi-University Training Contest 5
Problem Description
You are given two integers \(N,C\) and two integer sequences \(a\) and \(b\) of length \(N\). The sequences are indexed from \(1\) to \(N\).
Please solve the following equation for \(x\):
\(∑_{i=1}^N|a_i\cdot x + b_i| = C\), where \(|v|\) means the absolute value of \(v\).
Input
The first line contains an integer \(T\) indicating there are \(T\) tests. Each test consists of \(N+1\) lines. The first line contains two integers \(N,C\). The \(i\)-th line of following \(N\) lines consists of two integers \(a_i,b_i\).
\(*\ 1\le T\le 50\)
\(*\ 1\le N\le 10^5\)
\(*\ 1\le a_i\le 1000\)
\(*\ −1000\le b_i\le 1000\)
\(*\ 1\le C\le 10^9\)
\(*\) only \(5\) tests with \(N\) larger than \(1000\)
Output
For each test, output one line.
If there are an infinite number of solutions, this line consists only one integer \(−1\).
Otherwise, this line first comes with an integer \(m\) indicating the number of solutions, then you must print \(m\) fractions from the smallest to the largest indicating all possible answers. (It can be proved that all solutions can be written as fractions). The fraction should be in the form of "a/b" where a must be an integer, b must be a positive integer, and \(gcd(abs(a),b)=1\). If the answer is \(0\), you should output "\(0/1\)".
Sample Input
4
2 3
1 2
1 -1
3 3
2 1
2 2
2 3
2 1
3 5
4 -1
3 2
1 -1
1 -2
1 -3
Sample Output
-1
2 -3/2 -1/2
0
1 2/1
Solution
题意:
给定一个整数 \(C\) 和 \(N\) 组 \(a_i,b_i\),求 \(∑_{i=1}^N|a_i\cdot x + b_i| = C\) 的所有解,如果有无穷多个解就输出 -1.
思路
分类讨论
分类讨论去绝对值。根据 \(b_i / a_i\) 排序,可得各段区间:\([-b_0/a_0, ∞),\ [-b_1/a_1, -b_0/a_0),\ [-b_2/a_2, -b_1/a_1),\ ...\ ,[-b_n/a_n, -b_{n-1}/a_{n-1}),\ [∞, -b_n/a_n)\) 设 \(suma = \sum_{i=1}^Na_i, sumb = \sum_{i=1}^Nb_i\),依次让 \(a_ix+b_i\) 变成 \(-a_ix-b_i\),也就是 \(suma - 2a_i, sumb-2b_i\),求出 \(x_i = \frac{c - sumb}{suma}\) 并判断是否在区间内。无穷解的情况:\(suma = 0, sumb = c\)。
感谢杭电没有卡 \(double\)。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;
struct Coefficient {
ll a, b;
} co[maxn];
int cmp(Coefficient c1, Coefficient c2) {
return c1.b * 1.0 / c1.a < c2.b * 1.0 / c2.a;
}
vector<Coefficient> ans;
ll gcd(ll a, ll b) {
return b == 0? a: gcd(b, a % b);
}
int main() {
int T;
cin >> T;
while(T--) {
ans.clear();
int n;
ll c;
scanf("%d%lld", &n, &c);
ll suma = 0, sumb = 0;
for(int i = 1; i <= n; ++i) {
scanf("%lld%lld", &co[i].a, &co[i].b);
suma += co[i].a;
sumb += co[i].b;
}
sort(co + 1, co + n + 1, cmp);
Coefficient t;
t.a = suma, t.b = sumb;
if((c - sumb) * 1.0 / suma >= -co[1].b * 1.0 / co[1].a) {
t.b = c - t.b;
ans.push_back(t);
}
int flag = 1;
for(int i = 1; i <= n; ++i) {
suma -= co[i].a * 2;
sumb -= co[i].b * 2;
t.a = suma;
t.b = sumb;
if(!suma) {
if(sumb == c) {
flag = 0;
break;
}
}
if(i < n) {
if((c - sumb) * 1.0 / suma >= -co[i + 1].b * 1.0 / co[i + 1].a && (c - sumb) * 1.0 / suma < -co[i].b * 1.0 / co[i].a) {
t.b = c - t.b;
ans.push_back(t);
}
} else {
if((c - sumb) * 1.0 / suma < -co[i].b * 1.0 / co[i].a) {
t.b = c - t.b;
ans.push_back(t);
}
}
}
if(!flag) printf("-1\n");
else {
sort(ans.begin(), ans.end(), cmp);
printf("%d", ans.size());
for(int i = 0; i < ans.size(); ++i) {
if(ans[i].b * 1.0 / ans[i].a < 0) printf(" -");
else printf(" ");
ll g = gcd(abs(ans[i].b), abs(ans[i].a));
if(ans[i].b == 0) printf("0/1");
else printf("%lld/%lld", abs(ans[i].b) / g, abs(ans[i].a) / g);
}
printf("\n");
}
}
return 0;
}
HDU 6627 equation (分类讨论)的更多相关文章
- HDU 5203 Rikka with wood sticks 分类讨论
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5203 bc(chinese):http://bestcoder.hdu.edu.cn/con ...
- HDU 6665 Calabash and Landlord (分类讨论)
2019 杭电多校 8 1009 题目链接:HDU 6665 比赛链接:2019 Multi-University Training Contest 8 Problem Description Cal ...
- HDU5957 Query on a graph(拓扑找环,BFS序,线段树更新,分类讨论)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5957 题意:D(u,v)是节点u和节点v之间的距离,S(u,v)是一系列满足D(u,x)<=k的点 ...
- Codeforces 460D Little Victor and Set --分类讨论+构造
题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四 ...
- BZOJ-1067 降雨量 线段树+分类讨论
这道B题,刚的不行,各种碎点及其容易忽略,受不鸟了直接 1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2859 ...
- UVaLive 6862 Triples (数学+分类讨论)
题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数 ...
- 枚举(分类讨论):BZOJ 1177: [Apio2009]Oil
1177: [Apio2009]Oil Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 1477 Solved: 589[Submit] Descri ...
- Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array 分类讨论连续递推dp
题意:给出一个 数列 和一个x 可以对数列一个连续的部分 每个数乘以x 问该序列可以达到的最大连续序列和是多少 思路: 不是所有区间题目都是线段树!!!!!! 这题其实是一个很简单的dp 使用的是分 ...
- 【cf789B】Masha and geometric depression(分类讨论/暴力)
B. Masha and geometric depression 题意 在黑板上写数列,首项是b,公比是q,超过l时就停止不写.给定m个数,遇到后跳过不写.问一共写多少个数,如果无穷个输出inf. ...
随机推荐
- DPL, CPL及RPL之间的关系
DPL: Descriptor Privilege Level 1) GDT/LDT表中的描述符 GDT/LDT表中的描述符,描述的是一段内存. 其中的DPL代表着GDT/LDT中的描述符描述的内存段 ...
- Kattis - barcode
Kattis - barcode 题目原文: To prepare for ACM-ICPC 2017 in Saigon, the host univeristy – Ho Chi Minh cit ...
- Visual Studio 2013创建并运行Cocos2d-x工程
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.准备: 我们得先把Visual Studio 2013安装好:可以去MSDN官网下载,在安装好 2.安装好vs之后,在cmd(终端)创 ...
- (4.20)sql server分离附加操作
关键词:sql server分离.sql server附加.分离附加.sql server附加分离 [0].数据库分离.附加的说明 SQL Server提供了“分离/附加”数据库.“备份/还原”数据库 ...
- Neo4j高级应用技术专题系列 - APOC存储过程库-【1】概述
Neo4j高级应用技术专题系列 - APOC存储过程库-[1]概述 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://bl ...
- 完全卸载win10上的Ubuntu子系统 - Windows Subsystem for Linux(WSL)
Ctrl + R 键入: lxrun /uninstall /full 具体请看 microsoft的说明:Frequently Asked Questions
- ionic3 emoj表情包插件 emoji-picker
1.效果演示: 2.安装扩展包依赖 npm i @ionic-tools/emoji-picker --save 3.app.module.ts中导入插件 import { EmojiPickerMo ...
- SharpZipLib 文件/文件夹 过滤
这里就不说压缩/解压了.网上教程太多. 主要说一下,解压时,如何过滤某些文件/文件夹 参考地址:https://github.com/icsharpcode/SharpZipLib/wiki/Fast ...
- Codeforces 1149D 最短路 状压DP
题意及思路:https://blog.csdn.net/yzyyylx/article/details/90145400 这题主要巧妙在分析了最短路算法的性质,得出大小小于等于3的连通块一定不会被再次 ...
- pair queue____多源图广搜
.简介 class pair ,中文译为对组,可以将两个值视为一个单元.对于map和multimap,就是用pairs来管理value/key的成对元素.任何函数需要回传两个值,也需要pair. 该函 ...