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 (分类讨论)的更多相关文章

  1. HDU 5203 Rikka with wood sticks 分类讨论

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5203 bc(chinese):http://bestcoder.hdu.edu.cn/con ...

  2. HDU 6665 Calabash and Landlord (分类讨论)

    2019 杭电多校 8 1009 题目链接:HDU 6665 比赛链接:2019 Multi-University Training Contest 8 Problem Description Cal ...

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

  4. Codeforces 460D Little Victor and Set --分类讨论+构造

    题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四 ...

  5. BZOJ-1067 降雨量 线段树+分类讨论

    这道B题,刚的不行,各种碎点及其容易忽略,受不鸟了直接 1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2859 ...

  6. UVaLive 6862 Triples (数学+分类讨论)

    题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数 ...

  7. 枚举(分类讨论):BZOJ 1177: [Apio2009]Oil

    1177: [Apio2009]Oil Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 1477  Solved: 589[Submit] Descri ...

  8. Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array 分类讨论连续递推dp

    题意:给出一个 数列 和一个x 可以对数列一个连续的部分 每个数乘以x  问该序列可以达到的最大连续序列和是多少 思路: 不是所有区间题目都是线段树!!!!!! 这题其实是一个很简单的dp 使用的是分 ...

  9. 【cf789B】Masha and geometric depression(分类讨论/暴力)

    B. Masha and geometric depression 题意 在黑板上写数列,首项是b,公比是q,超过l时就停止不写.给定m个数,遇到后跳过不写.问一共写多少个数,如果无穷个输出inf. ...

随机推荐

  1. jekyll介绍安装.github静态页面工具

    jekyll build # => 当前文件夹中的内容将会生成到 ./site 文件夹中. $ jekyll build --destination <destination> # ...

  2. Centos6下实现Nginx+Tomcat实现负载均衡及监控

    在性能测试过程中,我们可能会关注很多指标,比如CPU.IO.网络.磁盘等,通过这些指标大致可以判断哪个环节遇到了性能瓶颈,但是当这些指标无法判断出性能瓶颈时,我们可能就需要对一些中间件进行监控,比如N ...

  3. upc组队赛1 小C的数学问题【单调栈】(POJ2796)

    小C的数学问题 题目描述 小C是个云南中医学院的大一新生,在某个星期二,他的高数老师扔给了他一个问题. 让他在1天的时间内给出答案. 但是小C不会这问题,现在他来请教你. 请你帮他解决这个问题. 有n ...

  4. Leetcode代码复盘_动态规划

    动态规划中包含3个重要的概念: 1.最优子结构 2.边界 3.状态转移公式 以跳台阶为例,最优子结构为f(10)=f(9) + f(8),边界是f(1)=1, f(2)=2,状态转移公式f(n)=f( ...

  5. activiti7组任务测试

    package com.zcc.activiti05; import org.activiti.engine.*;import org.activiti.engine.repository.Deplo ...

  6. jQuery选择器中空格的问题再探究

    jQuery选择器的空格问题,看似很小,但是差之毫厘谬以千里,让人很是恼火,<锋利的jQuery>书中有个经典的例子,我这里也拷贝下来,再加点自己的想法 <html> < ...

  7. css3新增(圆角边框(border-radius),盒子阴影(box-shadow),文字阴影(text-shadow),背景缩放(background-size))

    1.圆角边框  border-radius border-radius 属性用于设置元素的外边框圆角 语法:border-radius:length; 参数值可以是数值 或者 百分比 的形式 正方形, ...

  8. 换了SSD发现plank也好了

    我的Dock用的是plank,很简单很好用.为什么不用Docky还有其他什么玩意儿呢?plank很简单很好用,资源占用很少,可以智能隐藏,you nearly can't feel it but yo ...

  9. python_django_类视图的第一次忐忑碰触!!!

    我们Django学到这里,基础知识阶段是已经完成了!! 在我们前面接触到的视图都是基于函数的视图函数我们可以称它为FBV,而今天我们新接触的就是视图函数的另一类CBV:基于类的视图函数,我们这里拓展这 ...

  10. CSIC_716_20191225【HTML基础入门】

    HTTP协议 超文本传输协议HyperText Transfer Protocol 四大特性: 1.一次请求一次响应 2.基于TCP/IP协议,作用于应用层 3.无状态 4.无连接 数据格式: 1.请 ...