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 扩展欧几里德的更多相关文章

  1. SGU 106 The equation 扩展欧几里得好题

    扩展欧几里得的应用……见算法竞赛入门经典p.179 注意两点:1.解不等式的时候除负数变号 2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 ...

  2. SGU 106 The Equation 扩展欧几里得应用

    Sol:线性不定方程+不等式求解 证明的去搜下别人的证明就好了...数学题. #include <algorithm> #include <cstdio> #include & ...

  3. 数论 + 扩展欧几里得 - 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< ...

  4. SGU 106 The equation

    H - The equation Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Subm ...

  5. SGU 106 The equation【扩展欧几里得】

    先放一张搞笑图.. 我一直wa2,这位不认识的大神一直wa9...这样搞笑的局面持续了一个晚上...最后各wa了10发才A... 题目链接: http://acm.hust.edu.cn/vjudge ...

  6. 扩展欧几里德 SGU 106

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=106   题意:求ax + by + c = 0在[x1, x2], [y1, y2 ...

  7. (扩展欧几里德算法)zzuoj 10402: C.机器人

    10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...

  8. [BZOJ1407][NOI2002]Savage(扩展欧几里德)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...

  9. 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm

    欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...

随机推荐

  1. 【BZOJ4898】[Apio2017]商旅 分数规划+SPFA

    [BZOJ4898][Apio2017]商旅 Description 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所深深吸引,决定定居于此,做一个 ...

  2. 【BZOJ2555】SubString 后缀自动机+LCT

    [BZOJ2555]SubString Description 懒得写背景了,给你一个字符串init,要求你支持两个操作         (1):在当前字符串的后面插入一个字符串         (2 ...

  3. mysql_数据库_操作

    1.查看数据库 show databases; # 默认数据库: test - 用于用户测试数据 information_schema - MySQL本身架构相关数据 2.创建数据库 #utf- 编码 ...

  4. Docker Metasploit Framework

    https://hub.docker.com/r/usertaken/metasploit-framework/ docker pull usertaken/metasploit-framework ...

  5. 全局enter回车键js

    js实现敲回车键触发事件 document.onkeydown = function(e){ var ev = document.all ? window.event : e; ){ alert(&q ...

  6. 洛谷 P4768 [NOI2018]归程

    洛谷 361行代码的由来 数据分治大发好啊- NOI的签到题,可怜我在家打了一下午才搞了80分. 正解应该是kruskal重构树或排序+可持久化并查集. 我就分点来讲暴力80分做法吧(毕竟正解我也没太 ...

  7. 启动 nodemanger 报错javax.security.sasl.SaslException: GSS initiate failed

    最近启动 Hadoop, nodemanger 老挂,报kerberos 验证错误,各种查找原因,时间也同步,kint 也能登录到kerberos,一直找不到原因,最后发现是网关和远端的时间同步,但是 ...

  8. opencv3计算机视觉+Python(一)

    基本I/O脚本 读/写图像文件 OpenCV的imread函数和imwrite函数能支持各种静态图像文件格式.不同系统支持的文件格式不一样,但都支持BMP格式,通常还应该支持PNG.JPEG和TIFF ...

  9. CoreThink主题开发(九)使用H-ui开发博客主题之用户个人主页

    感谢H-ui.感谢CoreThink!  效果图: 这里使用table布局 /Theme/Blog/User/Index/home.html <extend name="$_home_ ...

  10. mapInfo文件格式详解

    from:http://hotolee.blog.163.com/blog/static/3815229920098434956370 MapInfo以表(Tab)的形式存储信息,每个表是由一组Map ...