SGU 106 The equation 扩展欧几里德
106. The equation
time limit per test: 0.25 sec.
memory limit per test: 4096
KB
There is an equation ax + by + c = 0. Given a,b,c,x1,x2,y1,y2 you must
determine, how many integer roots of this equation are satisfy to the
following conditions : x1<=x<=x2, y1<=y<=y2. Integer root
of this equation is a pair of integer numbers (x,y).
Input
Input contains integer numbers a,b,c,x1,x2,y1,y2 delimited by spaces and line breaks. All numbers are not greater than 108 by absolute value.
Output
Write answer to the output.
Sample Input
1 1 -3
0 4
0 4
Sample Output
4
思路:ax+by=-c;
扩展欧几里德求解;
x=x0+b/gcd(a,b)*t;
y=y0+a/gcd(a,b)*t;
求x1<=x<=x2&&y1<=y<=y2的条件下,t的可行解;
找到x的范围的t的可行解[lx,rx];
同理 [ly,ry];
ans=min(rx,ry)-max(lx,ly)+1;
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 1e-13
const int N=1e3+,M=1e6+,inf=1e9+,mod=;
void extend_Euclid(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
return;
}
extend_Euclid(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - (a / b) * y;
}
ll gcd(ll a,ll b)
{
if(b==)
return a;
return gcd(b,a%b);
}
int main()
{
ll a,b,c;
ll lx,rx;
ll ly,ry;
scanf("%I64d%I64d%I64d",&a,&b,&c);
scanf("%I64d%I64d",&lx,&rx);
scanf("%I64d%I64d",&ly,&ry);
c=-c;
if(lx>rx||ly>ry)
{
printf("0\n");
return ;
}
if (a == && b == && c == )
{
printf("%I64d\n",(rx-lx+) * (ry-ly+));
return ;
}
if (a == && b == )
{
printf("0\n");
return ;
}
if (a == )
{
if (c % b != )
{
printf("0\n");
return ;
}
ll y = c / b;
if (y >= ly && y <= ry)
{
printf("%I64d\n",rx - lx + );
return ;
}
else
{
printf("0\n");
return ;
}
}
if (b == )
{
if (c % a != )
{
printf("0\n");
return ;
}
ll x = c / a;
if (x >= lx && x <= rx)
{
printf("%I64d\n",ry - ly + );
return ;
}
else
{
printf("0\n");
return ;
}
}
ll hh=gcd(abs(a),abs(b));
if(c%hh!=)
{
printf("0\n");
return ;
}
else
{
ll x,y;
extend_Euclid(abs(a),abs(b),x,y);
x*=(c/hh);
y*=(c/hh);
if(a<)
x=-x;
if(b<)
y=-y;
a/=hh;
b/=hh;
ll tlx,trx,tly,trry;
if(b>)
{
ll l=lx-x;
tlx=l/b;
if(l>=&&l%b)
tlx++;
ll r=rx-x;
trx=r/b;
if(r<&&r%b)
trx--;
}
else
{
b=-b;
ll l=x-rx;
tlx=l/b;
if(l>=&&l%b)
tlx++;
ll r=x-lx;
trx=r/b;
if(r<&&r%b)
trx--;
}
if(a>)
{
ll l=-ry+y;
tly=l/a;
if(l>=&&l%a)
tly++;
ll r=-ly+y;
trry=r/a;
if(r<&&r%a)
trry--;
}
else
{
a=-a;
ll l=ly-y;
tly=l/a;
if(l>=&&l%a)
tly++;
ll r=ry-y;
trry=r/a;
if(r<&&r%a)
trry--;
}
printf("%I64d\n",(max(0LL,min(trry,trx)-max(tly,tlx)+)));
return ;
}
return ;
}
SGU 106 The equation 扩展欧几里德的更多相关文章
- SGU 106 The equation 扩展欧几里得好题
扩展欧几里得的应用……见算法竞赛入门经典p.179 注意两点:1.解不等式的时候除负数变号 2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 ...
- SGU 106 The Equation 扩展欧几里得应用
Sol:线性不定方程+不等式求解 证明的去搜下别人的证明就好了...数学题. #include <algorithm> #include <cstdio> #include & ...
- 数论 + 扩展欧几里得 - SGU 106. The equation
The equation Problem's Link Mean: 给你7个数,a,b,c,x1,x2,y1,y2.求满足a*x+b*y=-c的解x满足x1<=x<=x2,y满足y1< ...
- SGU 106 The equation
H - The equation Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Subm ...
- SGU 106 The equation【扩展欧几里得】
先放一张搞笑图.. 我一直wa2,这位不认识的大神一直wa9...这样搞笑的局面持续了一个晚上...最后各wa了10发才A... 题目链接: http://acm.hust.edu.cn/vjudge ...
- 扩展欧几里德 SGU 106
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=106 题意:求ax + by + c = 0在[x1, x2], [y1, y2 ...
- (扩展欧几里德算法)zzuoj 10402: C.机器人
10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...
- [BZOJ1407][NOI2002]Savage(扩展欧几里德)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...
- 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm
欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...
随机推荐
- GridView中给DropDownList动态绑定数据,及选择列表值后自动更新数据库
protected void sgvFile1_RowDataBound(object sender, GridViewRowEventArgs e) { DropDownList ddlAM = ( ...
- 新提交审核app保留检查更新入口将被拒绝
3月起要求关闭所有App内的检查更新功能,苹果App Store将向用户自动提示更新,新提交审核版本如果保留检查更新入口审核时将被拒绝,请各产品团队重点关注. 10.6 - Apple and our ...
- C#设计模式-单实例
单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点. 1.经典的模式 namespace singleClass { class ...
- .net 取得类的属性、方法、成员及通过属性名取得属性值
//自定义的类 model m = new model(); //取得类的Type实例 //Type t = typeof(model); //取得m的Type实例 Type t = m.G ...
- 微信开发模板--easywechat
链接地址:https://easywechat.org/zh-cn/docs/installation.html
- python——random模块
用法示例: import random # 1)随机小数 print(random.random()) # 获取大于0且小于1 之间的小数 random.random() print(random.u ...
- 《Python机器学习》笔记(五)
通过降维压缩数据 在前面已经介绍了几种不同的特征选择技术对数据集进行降维的方法.另一种常用于降维的特征选择方法就是特征抽取.数据压缩也是机器学习领域中的一个重要内容.数据压缩技术可以帮助我们对数据及逆 ...
- rar 7z文件打包
把D:\file目录下的所有东西打包为file.rar放到D:\目录下, Rar.exe是放在c盘根目录下 >>C:\Rar.exe a -k -r -s -m1 D:\file.rar ...
- Easyui 遮罩实现方式
项目中在提交Ajax请求时,后台处理数据时间有点长,需要一个遮罩,就随便找了一个实现一下:包含两种方式,个人比较喜欢第二种 第一种: $("#saveMaterial").clic ...
- HDF 文件数据的读取
http://www.cams.cma.gov.cn/cams_973/cheres_docs/cheres_doc_sat.modis.1b.html一. HDF文件格式 1.概述 HDF 是美国国 ...