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


PROBLEM SETTER: JANE ALAM JAN
思路:扩展欧几里得;
这题再次让我重新认识了扩欧;其实这题只要分情况讨论下,用扩欧求出特解,然后二分找通解中
t 的范围然后看t重合的部分就行,有几个特殊情况讨论下,还有A,B系数的符号讨论下。
#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的更多相关文章

  1. lightoj 1306 - Solutions to an Equation 扩展的欧几里得

    思路:看题就知道用扩展的欧几里得算法做!!! 首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0. 继而可以得到ax+by=c的一个 ...

  2. LightOJ 1306 - Solutions to an Equation 裸EXGCD

    本题是极其裸的EXGCD AX+BY+C=0 给你a b c 和x与y的区间范围,问你整数解有几组 作为EXGCD入门,题目比较简单 主要需要考虑区间范围的向上.向下取整,及正负符号的问题 问题是这正 ...

  3. Solutions to an Equation LightOJ - 1306

    Solutions to an Equation LightOJ - 1306 一个基础的扩展欧几里得算法的应用. 解方程ax+by=c时,基本就是先记录下a和b的符号fla和flb(a为正则fla为 ...

  4. Jordan Lecture Note-6: The Solutions of Nonlinear Equation.

    The Solutions of Nonlinear Equation 本文主要介绍几种用于解非线性方程$f(x)=0$的一些方法. (1) Bisection Method. 算法: step 1: ...

  5. [lightoj P1306] Solutions to an Equation

    [lightoj P1306] Solutions to an Equation You have to find the number of solutions of the following e ...

  6. (light oj 1306) Solutions to an Equation 扩展欧几里得算法

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...

  7. [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 ...

  8. [LeetCode] Solve the Equation 解方程

    Solve a given equation and return the value of x in the form of string "x=#value". The equ ...

  9. [Swift]LeetCode640. 求解方程 | Solve the Equation

    Solve a given equation and return the value of x in the form of string "x=#value". The equ ...

随机推荐

  1. 深度探讨 PHP 之性能

    1.缘起 关于PHP,很多人的直观感觉是PHP是一种灵活的脚本语言,库类丰富,使用简单,安全,非常适合WEB开发,但性能低下.PHP的性能是否真的就 如同大家的感觉一样的差呢?本文就是围绕这么一个话题 ...

  2. jmeter+ant输出测试报告

    jmeter自己本身可以输出html测试报告的,不过这种自带的测试报告特别简陋,如下图所示,一般我们是不看这种的. 我们可以使用ant来输出更高效.更直观的测试报告. 首先下载安装ant, 我用的是a ...

  3. Swagger2异常 java.lang.NumberFormatException: For input string: ""

    问题在访问swagger首页时报错: java.lang.NumberFormatException: For input string: "" at java.lang.Numb ...

  4. SQL count和sum

    count(1).count(*)与count(列名)的执行区别 count(1) and count(字段) 两者的主要区别是 (1) count(1) 会统计表中的所有的记录数,包含字段为null ...

  5. Initialization of data members

    In C++, class variables are initialized in the same order as they appear in the class declaration. C ...

  6. SpringMVC(4):文件上传与下载

    一,文件上传 文件上传是项目开发中最常见的功能之一 ,springMVC 可以很好的支持文件上传,但是SpringMVC上下文中默认没有装配MultipartResolver,因此默认情况下其不能处理 ...

  7. 如何使用gitHub管理自己的项目

    GitHub 与 Git Git是一种分布式版本控制系统,与svn是同样的概念 GitHub是一个网站,提供Git服务 前提:你的本机电脑已经安装了git,并且已经注册了gitHub账号 Git上传本 ...

  8. Spring Boot的异步任务、定时任务和邮件任务

    一.异步任务 1.启动类添加注解@EnableAsync,开启异步任务注解功能: 2.需要异步执行的方法上添加@Async注解. 二.定时任务 1.启动类添加注解@EnableScheduling,开 ...

  9. 爬虫之正则表达式re模块

    为什么要学正则表达式 实际上爬虫一共就四个主要步骤: 明确目标 (要知道你准备在哪个范围或者网站去搜索) 爬 (将所有的网站的内容全部爬下来) 取 (去掉对我们没用处的数据) 处理数据(按照我们想要的 ...

  10. 『学了就忘』Linux服务管理 — 76、RPM包安装的服务管理

    目录 1.独立服务的启动管理 2.独立服务的自启动管理 方式一: 方式二:(推荐) 方式三: 3.验证 1.独立服务的启动管理 (1)使用/etc/init.d/目录中的启动脚本启动服务(推荐) [r ...