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. Flow Problem(最大流模板)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  2. ztree的异步加载

    js中代码为: //参数设置: var setting = { async: { enable: true,    url:"<%=path%>/role/getTreeData ...

  3. What are DESC and ASC Keywords?

    What are DESC and ASC Keywords? ASC is the short form for ascending DESC is the short form for desce ...

  4. Python3.6全栈开发实例[010]

    10.有字符串 "k:1|k1:2|k2:3|k3:4" 处理成字典 {'k':1,'k1':2....} s = "k:1|k1:2|k2:3|k3:4" d ...

  5. 图像分割之mean shift

    阅读目的:理解quick shift,同时理解mean shift原理,mean shift用于图像聚类,优点是不需要指定聚类中心个数,缺点是计算量太大(原因). mean shift主要用来寻找符合 ...

  6. Java替换字符串中的占位符

    在开发中,会有动态配置字符串其中的某些字符,如何使用字符中的占位符,并且在代码动态替换占位符实现动态配置字符串! 1.定义字符串时,再string文件添加字符串: 注意!记得要在字符文件中加上这些: ...

  7. java定时器的使用(Timer)(转发:https://blog.csdn.net/ecjtuxuan/article/details/2093757)

    1.在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等. 对于这样的操作最方便.高效的实现方式就是使用java.util.Timer工具类. private java.util.Tim ...

  8. C#对excel的操作

    本文先描述如何用c#连接.操作excel文件. 项目中需要引入的DLL文件为Interop.Excel.Interop.Microsoft.Office.Core.Interop.Office等. 操 ...

  9. Hadoop学习基础之三:MapReduce

    现在是讨论这个问题的不错的时机,因为最近媒体上到处充斥着新的革命所谓“云计算”的信息.这种模式需要利用大量的(低端)处理器并行工作来解决计算问题.实际上,这建议利用大量的低端处理器来构建数据中心,而不 ...

  10. 【数学建模】MATLAB学习笔记——函数式文件

    MATLAB学习笔记——函数式文件 引入函数式文件 说明: 函数式文件主要用于解决计算中的参数传递和函数调用的问题. 函数式的标志是它的第一行为function语句. 函数式文件可以有返回值,也可以没 ...