2019HDU暑期多校训练-1004equation-方程求解
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:
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 ai,bi.
1≤T≤50
1≤N≤10^5
1≤ai≤1000
−1000≤bi≤1000
1≤C≤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
核心思想:
每个式子|ai⋅x+bi|,都存在一个零点,n个式子最多有n个零点,将n个零点升序排列在x轴上,这样就有n+1个区间。对于任何一个区间,ai⋅x+bi的正负是确定的,也就是可以把绝对值符号去掉。
枚举每个区间,去掉绝对值符号后合并同类项,得到方程的一个解,若此解在被枚举的区间内,则保留此解,否则舍掉此解。如果某个区间在合并同类项后x的系数为0,则分情况讨论:1、等式恒成立,则此区间任意一个实数都是方程的解,输出-1;2、等式不成立,此区间无解。
代码如下:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+20;
//h.v表示零点,chu.v表示解
struct node{
int a,b;
double v;
}h[N],chu[N];
int sa[N],sb[N];
bool cmp(node p,node q)
{
return p.v<q.v;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int n,c;
//输入
scanf("%d%d",&n,&c);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&h[i].a,&h[i].b);
//h.v表示零点
h[i].v=-1.0*h[i].b/h[i].a;
}
//按零点升序排列
sort(h+1,h+n+1,cmp);
//求个前缀和,方便合并同类项
for(int i=1;i<=n;i++)
{
sa[i]=sa[i-1]+h[i].a;
sb[i]=sb[i-1]+h[i].b;
}
//枚举区间
int cnt=0;
int flag=0;
for(int i=0;i<=n;i++)
{
//前i个式子为正
//i+1到n个式子为负,要取负得绝对值
//ssa=sa[i]-(sa[n]-sa[i])
int ssa=2*sa[i]-sa[n];
int ssb=2*sb[i]-sb[n];
//x的系数在合并同类项后为0
if(ssa==0)
{
if(c-ssb==0)
{
//解个数无穷,输出-1
flag=1;
break;
}
}
//x的系数不是0
else
{
double te=1.0*(c-ssb)/ssa;
//判断解是否在区间内
if((i==0||te>=h[i].v)&&(i+1>n||te<h[i+1].v))
{
//约分
int t=__gcd(c-ssb,ssa);
chu[cnt].a=(c-ssb)/t;
chu[cnt].b=ssa/t;
//按照题目要求,分子的值要为正
if(chu[cnt].b<0)
{
chu[cnt].b=-chu[cnt].b;
chu[cnt].a=-chu[cnt].a;
}
chu[cnt].v=te;
cnt++;
}
}
}
//输出
if(flag)
{
printf("-1\n");
continue;
}
sort(chu,chu+cnt,cmp);
if(cnt==0)
printf("0\n");
else
{
printf("%d ",cnt);
for(int i=0;i<cnt-1;i++)
printf("%d/%d ",chu[i].a,chu[i].b);
printf("%d/%d\n",chu[cnt-1].a,chu[cnt-1].b);
}
}
return 0;
}
2019HDU暑期多校训练-1004equation-方程求解的更多相关文章
- HDU6578 2019HDU多校训练赛第一场 1001 (dp)
HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...
- HDU6579 2019HDU多校训练赛第一场1002 (线性基)
HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...
- MATLAB 符号变量表达式 + 方程求解
源代码见文末 部分源代码: % 符号变量 两种表达方式 a=sym('a'); class(a); syms b; b; % 符号常量 c=sym('); c; % 符号表达式 三种表达方式 f1=' ...
- 洛谷——P1689 方程求解
P1689 方程求解 题目描述 给一个方程,形如X+Y=Z或X-Y=Z.给出了其中两个未知数,请求出第三个数.未知数用‘?’表示,等式中也许会出现一些多余的空格. 输入输出格式 输入格式: 一行,方程 ...
- 洛谷 P1689 方程求解
P1689 方程求解 题目描述 给一个方程,形如X+Y=Z或X-Y=Z.给出了其中两个未知数,请求出第三个数.未知数用‘?’表示,等式中也许会出现一些多余的空格. 输入输出格式 输入格式: 一行,方程 ...
- 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)
layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...
- HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)
HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...
- FESTUNG模型介绍—1.对流方程求解
FESTUNG模型介绍-1.对流方程求解 1. 控制方程 对流问题中,控制方程表达式为 \[\partial_t C + \partial_x (u^1 C) + \partial_y (u^2 C) ...
- 2021牛客暑期多校训练营3 J 思维
传送门 J-Counting Triangles_2021牛客暑期多校训练营3 (nowcoder.com) 题目 Goodeat finds an undirected complete graph ...
随机推荐
- SpringMVC 指定404、500错误页面
1.在web.xml中追加 <error-page> <error-code>404</error-code> <location>/404</l ...
- Spring Cloud Gateway(二):Spring Cloud Gateway整合Eureka应用
Spring Cloud Gateway 应用概述 下面的示例启动两个服务:gataway-server 和 user-service 都注册到注册中心 Eureka上,客户端请求后端服务[user- ...
- mysql 使用service mysqld start 提示未识别服务 进入/etc/rc.d/init.d 下面未发现有mysqld解决方法
1.执行whereis mysql会有如下打印: mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql ...
- 重读APUE(12)-SIGCHLD与僵尸进程
SIGCHLD信号是当子进程终止时向父进程发送的信号:它的语义如下: 如果进程明确的将该信号设置为SIG_IGN,则调用进程不会产生僵尸进程:这种情况下,wait是等不到给子进程收尸的,所以wait阻 ...
- Linux rpm 安装MySQL
1 检查是否存在旧版本mysql (1) mysql 执行命令:rpm -qa|grep mysql,若存在旧mysql,删除查询到的旧mysql,执行: rpm -e --nodeps XXXX ...
- yum搭建LAMP环境
LAMP=Linux+Apache(httpd)+Mysql(mariadb)+PHP Apache HTTP 服务器 2.4 文档:http://httpd.apache.org/docs/2.4/ ...
- 升级chrome浏览器导致网站登录功能不能用
笔者开发一个java web项目,低版本的chrome(74以下)可以正常登录,升级到chrome74不能正常登录,登录成功后url会携带一个jsessionid=xxxxxx. 登录成功那个页面有s ...
- 将ByteBuffer保存成文件
String dest = "d:/download/" + name; Path path = Paths.get(dest).getParent().toAbsolutePat ...
- 二分类Logistic回归模型
Logistic回归属于概率型的非线性回归,分为二分类和多分类的回归模型.这里只讲二分类. 对于二分类的Logistic回归,因变量y只有“是.否”两个取值,记为1和0.这种值为0/1的二值品质型变量 ...
- Python3 Selenium自动化web测试 ==>FAQ:日期格式和日期字符串格式相互转换
学习目的: 掌握python的基础应用 场景: 生成的测试日报需要加上时间戳作为唯一标志,免得文件覆盖,过往的文件丢失 因为os.rename方法要求文件名必须拼接的都是字符串 代码释义: # 日期转 ...