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( ...
随机推荐
- E - Rails (栈)
E - Rails Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description The ...
- 阻塞(sleep等等)区别 中断(interrupt)+ 中断的意义
不客气地说,至少有一半人认为,线程的"中断"就是让线程停止.如果你也这么认为,那你对多线程编程还没有入门. 在java中,线程的中断(interrupt)只是改变了线程的中断状态, ...
- SharePoint服务器端对象模型 之 访问网站和列表数据(Part 2)
(二)列表(SPList) 列表是SharePoint中最为重要的数据容器,我们一般保存在SharePoint中的所有数据,都是保存在列表中(文档库也是一种列表),因此列表对象在SharePoint的 ...
- 解决Mysql报错缺少libaio.so.1
解决Mysql报错缺少libaio.so.1 报错如上图,需要安装libaio.so.1 64位系统安装: wget http://mirror.centos.org/centos/6/os/x86_ ...
- Python3.6全栈开发实例[004]
4.计算传入函数的字符串中, 数字.字母.空格以及其他内容的个数,并返回结果. s1 = 'wan%$#(gwdwq\nwdhuaiww3 w02041718' def func1(s1): dic ...
- PAT 1071. 小赌怡情(15) JAVA
1071. 小赌怡情(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 常言道“小赌怡情”.这是一个很简单的 ...
- git钩子
定义: 钩子:由事件触发的函数 分类: 客户端钩子:由诸如提交和合并这样的操作触发 服务器端钩子:由诸如接收被推送的提交这样的联网操作触发 安装: a.钩子都被存储在 .git 目录下的 hooks ...
- adb通过TCP/IP连接提示 unable to connect to *, Connection refused的解决方法
通过串口连接板子进入命令行,然后执行: su setprop service.adb.tcp.port 5555 stop adbd start adbd
- oracle修改连接数后无法启动(信号量的问题)
当oracle11g修改最大连接数后启动报如下错误时,需要调整linux的信号量的内核参数: ORA-27154: post/wait create failedCause: internal err ...
- 剑指offer 面试4题
面试4题: 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 解题代码一:二 ...