题目链接:http://lightoj.com/volume_showproblem.php?problem=1306

You have to find the number of solutions of the following equation:

Ax + By + C = 

Where A, B, C, x, y are integers and x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2.

Input
Input starts with an integer T (≤ ), 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 [-, ]. Output
For each case, print the case number and the total number of solutions.
Sample Input - -
- - - -
-
- - - -
-
Output for Sample Input
Case :
Case :
Case :
Case :
Case :

题意:给出AX+BY+C==0中的A,B,C。问在X1到X2与Y1到Y2的范围内有几组解

分析:利用扩展欧几里得算法

首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0.

继而可以得到ax+by=c的一个组解x1=c*x0/g , y1=c*y0/g。

这样可以得到ax+by=c的通解为:

                  x=x1+b*t;

                  y=y1-a*t;

再就是要注意符号问题!!!

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include <map>
#include <string>
#include <vector>
#include<iostream>
using namespace std;
#define N 10006
#define INF 0x3f3f3f3f
#define LL long long
#define mod 1000000007
LL ex_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==)
{
x = ;
y = ;
return a;
}
LL g = ex_gcd(b,a%b,x,y);
LL t = x;
x = y;
y = t- a/b * y;
return g;
}
int sign(LL n)
{
if(n==)
return ;
return n>?:-;
}
LL ceil(LL a,LL b)
{
int s = sign(a) * sign(b);
return b/a + (b%a!= && s>);
}
LL floor(LL a,LL b)
{
int s = sign(a) * sign(b);
return b/a - (b%a!= && s<);
}
int main()
{
int T,con=;
scanf("%d",&T);
LL a,b,c,x1,x2,y1,y2,x,y;
while(T--)
{
scanf("%lld %lld %lld %lld %lld %lld %lld",&a,&b,&c,&x1,&x2,&y1,&y2);
printf("Case %d: ",con++);
if(a== && b==)
{
if(c==)
{
printf("%lld\n",(x2-x1+)*(y2-y1+));
}
else
printf("0\n");
continue;
}
if(a==)
{
if(c%b!=)
{
printf("0\n");
continue;
}
LL s = -c/b;
if(s>=y1 && s<=y2)
printf("%lld\n",x2-x1+);
else
printf("0\n");
continue;
}
if(b==)
{
if(c%a!=)
{
printf("0\n");
continue;
}
LL s = -c/a;
if(s>=x1 && s<=x2)
printf("%lld\n",y2-y1+);
else
printf("0\n");
continue;
} LL g = ex_gcd(a,b,x,y);
if(c%g!=)
{
printf("0\n");
continue;
}
if(sign(g) * sign(b) <) swap(x1,x2);
LL l1 = ceil(b, g*x1 + c*x);
LL l2 = floor(b, g*x2 + c*x);
if(sign(-a) * sign(g) <) swap(y1,y2);
LL r1 = ceil(-a,g * y1 + c*y);
LL r2 = floor(-a,g*y2 + c*y);
l1 = max(l1,r1);
r1 = min(l2,r2);
if(l1>r1) printf("0\n");
else
printf("%lld\n",r1-l1 +);
}
return ;
}

(light oj 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. 1306 - Solutions to an Equation

    1306 - Solutions to an Equation    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Lim ...

  3. 扩展欧几里得算法(extgcd)

    相信大家对欧几里得算法,即辗转相除法不陌生吧. 代码如下: int gcd(int a, int b){ return !b ? gcd(b, a % b) : a; } 而扩展欧几里得算法,顾名思义 ...

  4. noip知识点总结之--欧几里得算法和扩展欧几里得算法

    一.欧几里得算法 名字非常高大上的不一定难,比如欧几里得算法...其实就是求两个正整数a, b的最大公约数(即gcd),亦称辗转相除法 需要先知道一个定理: gcd(a, b) = gcd(b, a  ...

  5. 欧几里得算法与扩展欧几里得算法_C++

    先感谢参考文献:http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html 注:以下讨论的数均为整数 一.欧几里得算法(重点是证 ...

  6. vijos1009:扩展欧几里得算法

    1009:数论 扩展欧几里得算法 其实自己对扩展欧几里得算法一直很不熟悉...应该是因为之前不太理解的缘故吧这次再次思考,回看了某位大神的推导以及某位大神的模板应该算是有所领悟了 首先根据题意:L1= ...

  7. ****ural 1141. RSA Attack(RSA加密,扩展欧几里得算法)

    1141. RSA Attack Time limit: 1.0 secondMemory limit: 64 MB The RSA problem is the following: given a ...

  8. 浅谈扩展欧几里得算法(exgcd)

    在讲解扩展欧几里得之前我们先回顾下辗转相除法: \(gcd(a,b)=gcd(b,a\%b)\)当a%b==0的时候b即为所求最大公约数 好了切入正题: 简单地来说exgcd函数求解的是\(ax+by ...

  9. 『扩展欧几里得算法 Extended Euclid』

    Euclid算法(gcd) 在学习扩展欧几里得算法之前,当然要复习一下欧几里得算法啦. 众所周知,欧几里得算法又称gcd算法,辗转相除法,可以在\(O(log_2b)\)时间内求解\((a,b)\)( ...

随机推荐

  1. golang高并发的理解

    前言 GO语言在WEB开发领域中的使用越来越广泛,Hired 发布的<2019 软件工程师状态>报告中指出,具有 Go 经验的候选人是迄今为止最具吸引力的.平均每位求职者会收到9 份面试邀 ...

  2. Python学习曲线

    经历长达近一个月的资源筛选过程终于结束,总共1.5T百度网盘的资源经过:去重.筛选.整理.归档之后一份粗略的Python学习曲线资源已经成型,虽然中间经历了很多坎坷,不过最终还是完成,猪哥也是第一时间 ...

  3. 设计一个可拔插的 IOC 容器

    前言 磨了许久,借助最近的一次通宵上线 cicada 终于更新了 v2.0.0 版本. 之所以大的版本号变为 2,确实是向下不兼容了:主要表现为: 修复了几个反馈的 bug. 灵活的路由方式. 可拔插 ...

  4. Ubuntu16.04 部署安装Docker容器 & 注意事项

    一.部署安装Docker容器 1.1 Ubuntu下安装 crul sudo apt install curl curl是利用URL语法在命令行方式下工作的开源文件传输工具.它被广泛应用在Unix.多 ...

  5. [开源]Dapper Repository 一种实现方式

    接着上篇[开源]Entity Framework 6 Repository 一种实现方式 由于Dapper 本身就是轻量级Orm特性,这里参考Creating a Data Repository us ...

  6. 盘点 Python 中的那些冷知识(二)

    上一篇文章分享了 Python中的那些冷知识,地址在这里 盘点 Python 中的那些冷知识(一) 今天将接着分享!! 06. 默认参数最好不为可变对象 函数的参数分三种 可变参数 默认参数 关键字参 ...

  7. IL中间语言指令大全

    一些 IL 语言指令解释: Public field Static     Beq     如果两个值相等,则将控制转移到目标指令.Public field Static     Beq_S     ...

  8. 2017年IT行业测试调查报告

    在刚刚过去的2017年, 我们来一起看一下2017年IT行业测试调查报告 还是1到5名测试工程师最多 Test Architects 在北上广一线城市已经出现 https://www.lagou.co ...

  9. 回顾曾经的自己,献给java初学者的建议

    要不惜代价投资自己,任何对自己的投资都是值得的 要多学习数据结构, 习惯看源码! 一份知识经过n个人的传递早已经不成样子了 遇到问题不要直接百度,百度上那些花里胡哨的东西有用的很少,对症下药才是王道, ...

  10. JAVA IO流编程 实现文件的写入、写出以及拷贝

    一.流的概念 流:数据在数据源(文件)和程序(内存)之间经历的路径. 输入流:数据从数据源(文件)到程序(内存)的路径. 输出流:数据从程序(内存)到数据源(文件)的路径. 以内存为参照,如果数据向内 ...