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 ...
随机推荐
- 网易云信 集成UI库登录dologin没有回调
感谢github上的两位大佬指出问题的解决方法. 解决方法: 在进行ui初始化要在主进程中进行,初始化前进行主进程判断. 若还收不到回调,可尝试将uikit中的base包去掉而在build.gradl ...
- abundant
In ecology [生态学], local abundance is the relative representation of a species in a particular ecosys ...
- k8s-hpa自动横向扩容
目录 hpa自动扩容 官方文档 HPA是什么 Horizontal Pod Autoscaler 演练 参数 案例:监控cpu,内存,每秒数据包自动扩容 度量指标 pod清单案例-pod定义cup内存 ...
- Oracle中的instr函数
最近修改某个条件,由原来输入一个数据修改为可以输入多个,如图所示: 在实现时用到了regexp_substr函数进行分割连接起来的数据,查询时还用到了instr函数进行判断,但出现了问题,当子库存输入 ...
- Oracle decode和case的区别
case在SQL中有两种写法,先建立一个表create table salgrade(grade int, sal int);insert into salgrade values(1,1000);i ...
- Xcode中匹配的配置包的存放目录
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
- 1945-祖安 say hello-String
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include<bits/stdc++.h> 3 char str[100][40]; 4 char s[10 ...
- python中的虚拟环境(在jupyter和pycharm中的使用)
1.通过anaconda新建虚拟环境 创建虚拟环境:conda create -n your_env_name python=3.6 激活虚拟环境:activate your_env_name(虚拟环 ...
- 制作一个有趣的涂鸦物联网小项目(涂鸦模组SDK开发 CBU BK7231N WiFi+蓝牙模组 HSV彩色控制)
实现的功能: l APP控制月球灯 l 本地月球灯控制 l APP控制"大白"颜色,实现各种颜色变身 l 门状态传感器状态APP显示 l 网络状态指示灯,连接服务器长亮, ...
- 算法 A-Star(A星)寻路
一.简介 在游戏中,有一个很常见地需求,就是要让一个角色从A点走向B点,我们期望是让角色走最少的路.嗯,大家可能会说,直线就是最短的.没错,但大多数时候,A到B中间都会出现一些角色无法穿越的东西,比如 ...