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( ...
随机推荐
- bootstrap获取总条目数
$('#table').on('load-success.bs.table', function () {alert($('#table').bootstrapTable('getOptions'). ...
- 关于Springboot中dubbo整合注意的误区(不对之处请指出)
这是我的客户端配置! 这是生产的配置, 首先注意一下 scan 我之前尝试这样的客户端配置 然后 果断客户端不能注册接口 @Reference(version="1.0") ...
- 从“关于Java堆与栈的思考”一帖看错误信息的传播
我对转贴的信息一直有敌意,原因如下:首先,除了制造更多的信息垃圾,转贴不会带来新的价值,想收藏的话一个链接足矣:其次,将错误信息以讹传讹,混淆视听.不妨选一个典型的例子说明一二. 相信<关于Ja ...
- 如何将大数据保存到 MySql 数据库
1. 什么是大数据 1. 所谓大数据, 就是大的字节数据,或大的字符数据. 2. 标准 SQL 中提供了如下类型来保存大数据类型: 字节数据类型: tinyblob(256B), blob(64K), ...
- 解决john不能开多个进程的问题
在使用john进行shadow文件破解时,如果已经开了一个john的进程,这回提示以下错误: Crash recovery file is locked: /root/.john/john.rec ...
- python 中 for使用小技巧
testDict = {i: i * i for i in xrange(10)} testSet = {i * 2 for i in xrange(10)} print(testSet) print ...
- vuejs组件通信
<body> <div id="example"> <father></father> </div> </body ...
- Commit 函数WAIT = 'X'.
BAPI_TRANSACTION_COMMIT IF WAIT EQ SPACE. COMMIT WORK. ELSE. COMMIT WORK AND WAIT. IF SY-SUBRC NE . ...
- Webpack,Browserify和Gulp三者之间到底是怎样的关系
转:https://zhidao.baidu.com/question/1799220342210982227.html怎么解释呢?因为 Gulp 和 browserify / webpack 不是一 ...
- (扫盲)DTO数据传输对象
DTO即数据传输对象.但从定义上看就是简单的用来传递数据的.主要用途是在框架中定义DTO来绑定表现层中的数据.学过MVC.EF实体模型的都应该知道,我们可以定义一个Model实体来实现前后台数据的交互 ...