1306 - Solutions to an Equation
| Time Limit: 2 second(s) | Memory Limit: 32 MB |
You have to find the number of solutions of the following equation:
Ax + By + C = 0
Where A, B, C, x, y are integers and x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing seven integers A, B, C, x1, x2, y1, y2 (x1 ≤ x2, y1 ≤ y2). The value of each integer will lie in the range [-108, 108].
Output
For each case, print the case number and the total number of solutions.
Sample Input |
Output for Sample Input |
|
5 1 1 -5 -5 10 2 4 -10 -8 80 -100 100 -90 90 2 3 -4 1 7 0 8 -2 -3 6 -2 5 -10 5 1 8 -32 0 0 1 10 |
Case 1: 3 Case 2: 37 Case 3: 1 Case 4: 2 Case 5: 1 |
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
#include<stack>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
LL gcd(LL n,LL m)
{
if(m==0)
{
return n;
}
else if(n%m==0)
{
return m;
}
else return gcd(m,n%m);
}
pair<LL,LL>P(LL n,LL m)
{
if(m==0)
{
pair<LL,LL>ask=make_pair(1,0);
return ask;
}
else
{
pair<LL,LL>an=P(m,n%m);
LL x=an.second;
LL y=an.first;
y-=(n/m)*x;
an.first=x;
an.second=y;
return an;
}
}
int main(void)
{
LL i,j,k;
scanf("%lld",&k);
LL s;
LL A,B,C,x1,x2,y1,y2;
for(s=1; s<=k; s++)
{
LL sum=0;
scanf("%lld %lld %lld %lld %lld %lld %lld",&A,&B,&C,&x1,&x2,&y1,&y2);
C=-C;
if(A==0&&B==0&&C!=0)
sum=0;
else if(A==0&&B==0&&C==0)
{
sum=(LL)(x2-x1+1)*(LL)(y2-y1+1);
}
else if(A==0)
{
if(C%B)
{
sum=0;
}
else
{
LL t=(C/B);
if(t>=y1&&t<=y2)
sum=(x2-x1+1);
else sum=0;
}
}
else if(B==0)
{
if(C%A)
{
sum=0;
}
else
{
LL t=(C/A);
if(t>=x1&&t<=x2)
sum=(y2-y1+1);
else sum=0;
}
}
else
{
if(A<0)
{
C=-C;
A=-A;
B=-B;
}
LL gc=gcd(abs(A),abs(B));
if(C%gc)
{
sum=0;
}
else if((LL)A*(LL)B>0)
{
A/=gc;
B/=gc;
C/=gc;
pair<LL,LL>ask=P((A),(B));
LL x=(LL)ask.first;
LL y=(LL)ask.second;
x*=C;
y*=C;
LL l=-1e9;
LL r=1e9;
LL id=1e9;
while(l<=r)
{
LL mid=(l+r)/2;
if(x+mid*B>=x1)
{
id=mid;
r=mid-1;
}
else l=mid+1;
}
l=-1e9;
r=1e9;
LL ic=1e9;
while(l<=r)
{
LL mid=(l+r)/2;
if(x+mid*B<=x2)
{
ic=mid;
l=mid+1;
}
else r=mid-1;
}
if(id>ic)
{
sum=0;
}
else if(id==ic)
{
LL xx=x+id*B;
if(xx>=x1&&xx<=x2)
{
LL yy=y-id*A;
if(yy>=y1&&yy<=y2)
{
sum=1;
}
}
else sum=0;
}
else
{
l=-1e9;
r=1e9;
LL ip=1e9,iq=1e9;
while(l<=r)
{
LL mid=(l+r)/2;
if(y-mid*A>=y1)
{
ip=mid;
l=mid+1;
}
else r=mid-1;
}
l=-1e9;
r=1e9;
while(l<=r)
{
LL mid=(l+r)/2;
if(y-mid*A<=y2)
{
iq=mid;
r=mid-1;
}
else l=mid+1;
}
if(ip<iq)
{
sum=0;
}
else
{ if(ic<iq||id>ip)
{
sum=0;
}
else
{
if(id<=iq&&ic>=ip)
{
sum=ip-iq+1;
}
else if(iq<=id&&ip>=ic)
{
sum=ic-id+1;
}
else if(iq>=id&&iq<=ic)
{
sum=ic-iq+1;
}
else if(id>=iq&&id<=ip)
{
sum=ip-id+1; }
}
}
}
}
else
{
A/=gc;
B/=gc;
C/=gc;
pair<LL,LL>ask=P(abs(A),abs(B));
LL x=(LL)ask.first;
LL y=(LL)ask.second;
y=-y;
x*=C;
y*=C;
LL l=-1e9;
LL r=1e9;
LL id=1e9;
while(l<=r)
{
LL mid=(l+r)/2;
if(x+mid*abs(B)>=x1)
{
id=mid;
r=mid-1;
}
else l=mid+1;
}
l=-1e9;
r=1e9;
LL ic=1e9;
while(l<=r)
{
LL mid=(l+r)/2;
if(x+mid*abs(B)<=x2)
{
ic=mid;
l=mid+1;
}
else r=mid-1;
}
if(id>ic)
{
sum=0;
}
else if(id==ic)
{
LL xx=x+id*abs(B);
if(xx>=x1&&xx<=x2)
{
LL yy=y+id*A;
if(yy>=y1&&yy<=y2)
{
sum=1;
}
}
else sum=0;
}
else
{
l=-1e9;
r=1e9;
LL ip=1e9,iq=1e9;
while(l<=r)
{
LL mid=(l+r)/2;
if(y+mid*A>=y1)
{
iq=mid;
r=mid-1;
}
else l=mid+1;
}
l=-1e9;
r=1e9;
while(l<=r)
{
LL mid=(l+r)/2;
if(y+mid*A<=y2)
{
ip=mid;
l=mid+1;
}
else r=mid-1;
}
if(ip<iq)
{
sum=0;
}
else
{ if(ic<iq||id>ip)
{
sum=0;
}
else
{
if(id<=iq&&ic>=ip)
{
sum=ip-iq+1;
}
else if(iq<=id&&ip>=ic)
{
sum=ic-id+1;
}
else if(iq>=id&&iq<=ic)
{
sum=ic-iq+1;
}
else if(id>=iq&&id<=ip)
{
sum=ip-id+1; }
}
}//printf("%lld %lld %lld %lld\n",ip,iq,id,ic);
}
}
}
printf("Case %lld: %lld\n",s,sum); }
return 0;
}
1306 - Solutions to an Equation的更多相关文章
- lightoj 1306 - Solutions to an Equation 扩展的欧几里得
思路:看题就知道用扩展的欧几里得算法做!!! 首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0. 继而可以得到ax+by=c的一个 ...
- LightOJ 1306 - Solutions to an Equation 裸EXGCD
本题是极其裸的EXGCD AX+BY+C=0 给你a b c 和x与y的区间范围,问你整数解有几组 作为EXGCD入门,题目比较简单 主要需要考虑区间范围的向上.向下取整,及正负符号的问题 问题是这正 ...
- Solutions to an Equation LightOJ - 1306
Solutions to an Equation LightOJ - 1306 一个基础的扩展欧几里得算法的应用. 解方程ax+by=c时,基本就是先记录下a和b的符号fla和flb(a为正则fla为 ...
- Jordan Lecture Note-6: The Solutions of Nonlinear Equation.
The Solutions of Nonlinear Equation 本文主要介绍几种用于解非线性方程$f(x)=0$的一些方法. (1) Bisection Method. 算法: step 1: ...
- [lightoj P1306] Solutions to an Equation
[lightoj P1306] Solutions to an Equation You have to find the number of solutions of the following e ...
- (light oj 1306) Solutions to an Equation 扩展欧几里得算法
题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...
- [ACM_数学] Counting Solutions to an Integral Equation (x+2y+2z=n 组合种类)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27938#problem/E 题目大意:Given, n, count the numbe ...
- [LeetCode] Solve the Equation 解方程
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...
- [Swift]LeetCode640. 求解方程 | Solve the Equation
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...
随机推荐
- gcc 引用math 库 编译的问题 解决方法
1.gcc app.c -lm 其中lm表示的是连接 m forlibm.so / libm.a表示你想要的库 abc for libabc.so / libabc.a 其中.a表示的是静态链接库 . ...
- 框架学习-MyBatis(01)
1.MyBatis是持久层框架 什么是持久化: 狭义:把数据永久性的保存到数据当中 广义:针对于数据库的所有操作都称为持久化操作,CreateReadUpdateDelete操作 2.有哪些持久层框架 ...
- 内网穿透—使用 frp 实现内外网互通
前言 什么是内网穿透? 内网穿透,又叫 NET 穿透,是计算机用语.用通俗的说法就是你家里的个人电脑,可以直接被外网的人访问.例如你在公司,不通过远程工具,直接也可以访问到家里的电脑(本文章特指 we ...
- HDFS【hadoop3.1.3 windows开发环境搭建】
目录 一.配置hadoop3.1.3 windows环境依赖 配置环境变量 添加到path路径 在cmd中测试 二.idea中的配置 创建工程/模块 添加pom.xml依赖 日志添加--配置log4j ...
- 分类模型性能的评判方法-ROC分析
一.混淆矩阵 二.引入ROC曲线 如上第一幅图,蓝色高斯表示真实值为阴性,红色高斯表示真实值为阳性.A,B,C代表不同的阈值,阈值线左边表示预测值为阴性,阈值线右边表示预测值为阳性.阈值从A到C,由此 ...
- 网口程序udp
# -*- coding: utf-8 -*- """ Created on Thu Nov 12 15:02:53 2020 @author: Administrato ...
- Dubbo声明式缓存
为了进一步提高消费者对用户的响应速度,减轻提供者的压力,Dubbo提供了基于结果的声明式缓存.该缓存是基于消费者端的,所以使用很简单,只需修改消费者配置文件,与提供者无关 一.创建消费者07-cons ...
- apply 和 call 的区别
相同点: 都能够改变方法的执行上下文(执行环境),将一个对象的方法交给另一个对象来执行,并且是立即执行 不同点: call方法从第二个参数开始可以接收任意个参数,每个参数会映射到相应位置的func的参 ...
- Map集合的认识和理解
java.util.Map(k,v)集合* Map的特点:* 1.Map集合是一个双列集合,一个元素包含两个值(一个是key,一个是Value)* 2.Map集合中的元素,key和value的类型可以 ...
- 3.Vue.js-目录结构
Vue.js 目录结构 上一章节中我们使用了 npm 安装项目,我们在 IDE(Eclipse.Atom等) 中打开该目录,结构如下所示: 目录解析 目录/文件 说明 build 项目构建(webpa ...