(light oj 1306) Solutions to an Equation 扩展欧几里得算法
题目链接: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 扩展欧几里得算法的更多相关文章
- lightoj 1306 - Solutions to an Equation 扩展的欧几里得
思路:看题就知道用扩展的欧几里得算法做!!! 首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0. 继而可以得到ax+by=c的一个 ...
- 1306 - Solutions to an Equation
1306 - Solutions to an Equation PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Lim ...
- 扩展欧几里得算法(extgcd)
相信大家对欧几里得算法,即辗转相除法不陌生吧. 代码如下: int gcd(int a, int b){ return !b ? gcd(b, a % b) : a; } 而扩展欧几里得算法,顾名思义 ...
- noip知识点总结之--欧几里得算法和扩展欧几里得算法
一.欧几里得算法 名字非常高大上的不一定难,比如欧几里得算法...其实就是求两个正整数a, b的最大公约数(即gcd),亦称辗转相除法 需要先知道一个定理: gcd(a, b) = gcd(b, a ...
- 欧几里得算法与扩展欧几里得算法_C++
先感谢参考文献:http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html 注:以下讨论的数均为整数 一.欧几里得算法(重点是证 ...
- vijos1009:扩展欧几里得算法
1009:数论 扩展欧几里得算法 其实自己对扩展欧几里得算法一直很不熟悉...应该是因为之前不太理解的缘故吧这次再次思考,回看了某位大神的推导以及某位大神的模板应该算是有所领悟了 首先根据题意:L1= ...
- ****ural 1141. RSA Attack(RSA加密,扩展欧几里得算法)
1141. RSA Attack Time limit: 1.0 secondMemory limit: 64 MB The RSA problem is the following: given a ...
- 浅谈扩展欧几里得算法(exgcd)
在讲解扩展欧几里得之前我们先回顾下辗转相除法: \(gcd(a,b)=gcd(b,a\%b)\)当a%b==0的时候b即为所求最大公约数 好了切入正题: 简单地来说exgcd函数求解的是\(ax+by ...
- 『扩展欧几里得算法 Extended Euclid』
Euclid算法(gcd) 在学习扩展欧几里得算法之前,当然要复习一下欧几里得算法啦. 众所周知,欧几里得算法又称gcd算法,辗转相除法,可以在\(O(log_2b)\)时间内求解\((a,b)\)( ...
随机推荐
- 爬虫框架之Scrapy(一)
scrapy简介 scrapy是一个用python实现为了爬取网站数据,提取结构性数据而编写的应用框架,功能非常的强大. scrapy常应用在包括数据挖掘,信息处理或者储存历史数据的一系列程序中. s ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十║Vue基础终篇:传值+组件+项目说明
缘起 新的一天又开始啦,大家也应该看到我的标题了,是滴,Vue基础基本就到这里了,咱们回头看看这一路,如果你都看了,并且都会写了,那么现在你就可以自己写一个Demo了,如果再了解一点路由,ajax请求 ...
- JPA中EntityListeners注解的使用
使用场景 EntityListeners在jpa中使用,如果你是mybatis是不可以用的 它的意义 对实体属性变化的跟踪,它提供了保存前,保存后,更新前,更新后,删除前,删除后等状态,就像是拦截器一 ...
- Eureka服务下线后快速感知配置
现在由于eureka服务越来越多,发现服务提供者在停掉很久之后,服务调用者很长时间并没有感知到变化,依旧还在持续调用下线的服务,导致长时间后才能返回错误,因此需要调整eureka服务和客户端的配置,以 ...
- [SpringBoot guides系列翻译]调度任务
原文 调度任务 用spring实现一个任务调度. 你将做的 你将做一个应用每5秒钟打印当前时间,用@Scheduled注解. 你需要啥 15分钟 文本编辑器或者IDE JDK1.8+ Gradle4+ ...
- 利用SHA-1算法和RSA秘钥进行签名验签(带注释)
背景介绍 1.SHA 安全散列算法SHA (Secure Hash Algorithm)是美国国家标准和技术局发布的国家标准FIPS PUB 180-1,一般称为SHA-1.其对长度不超过264二进制 ...
- SQL Server 一列或多列重复数据的查询,删除(转载)
转载来源:https://www.cnblogs.com/sunxi/p/4572332.html 业务需求 最近给公司做一个小工具,把某个数据库(数据源)的数据导进另一个数据(目标数据库).要求导入 ...
- [转]当CPU飙高时,它在做什么
在开发过程中,有时候我们发现JVM占用的CPU居高不下,跟我们的预期不符,这时,CPU在做什么呢?是什么线程让CPU如此忙碌呢?我们通过如下几步,可以查看CPU在执行什么线程. 1.查找jvm进程ID ...
- Dotnetcore 开发速记
1.System.InvalidOperationException:"Internal connection fatal error." 全球固定模式,坑爹 https://gi ...
- 【设计模式】建造者模式 Builder Pattern
前面学习了简单工厂模式,工厂方法模式以及抽象工厂模式,这些都是创建类的对象所使用的一些常用的方法和套路, 那么如果我们创建一个很复杂的对象可上面的三种方法都不太适合,那么“专业的事交给专业人去做”,2 ...