H - The equation

Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

Description

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

我的思路就是首先把一个基本解求出来,然后看在x1、x2的范围内x的范围是多少,然后找到对应的y的范围,再看y的范围有多少个解是在y1、y2范围之内的,这个就是最后的答案。

当然,对于含有a=0或b=0的情况要特判一下。

附上一个很不错的网址:传送门

#include <iostream>
using namespace std;
typedef long long LL;
LL a,b,c,x1,x2,y1,y2,x,y,tmp,ans=;
LL mini = -361168601842738790LL;
LL maxi = 322337203685477580LL;
int extendedGcd(int a,int b){
if (b==){
x=;y=;
return a;
}
else{
int tmp = extendedGcd(b,a%b);
int t = x;
x=y;
y=t-a/b*y;
return tmp;
}
}
LL extendedGcd(LL a,LL b){
if (b == ){
x=;y=;
return a;
}
else{
LL TEMP = extendedGcd(b,a%b);
LL tt=x;
x=y;
y=tt-a/b*y;
return TEMP;
}
}
LL upper(LL a,LL b){
if (a<=)
return a/b;
return (a-)/b + ;
}
LL lower(LL a,LL b){
if (a>=)
return a/b;
return (a+)/b - ;
}
void update(LL L,LL R,LL wa){
if (wa<){
L=-L;R=-R;wa=-wa;
swap(L,R);
}
mini=max(mini,upper(L,wa));
maxi=min(maxi,lower(R,wa));
}
int main(){
cin >> a >> b >> c >> x1 >> x2 >> y1 >> y2;c=-c;
if (a== && b==){
if (c==) ans = (x2-x1+) * (y2-y1+);
}
else if (a== && b!=){
if (c % b==) {
tmp = c/b;
if (tmp>=y1 && tmp<=y2) ans = ;
}
}
else if (a!= && b==){
if (c % a==){
tmp = c/a;
if (tmp>=x1 && tmp<=x2) ans = ;
}
}
else{
LL d = extendedGcd(a,b);
if (c%d == ){
LL p = c/d;
update(x1-p*x,x2-p*x,b/d);
update(y1-p*y,y2-p*y,-a/d);
ans = maxi-mini+;
if (ans<) ans=;
}
}
cout << ans << endl;
}

SGU 106 The equation的更多相关文章

  1. SGU 106 The equation 扩展欧几里德

    106. The equation time limit per test: 0.25 sec. memory limit per test: 4096 KB There is an equation ...

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

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

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

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

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

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

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

  6. The equation SGU - 106

    题目链接:https://codeforces.com/problemsets/acmsguru/problem/99999/106 这个题是关于EXGCD特别好的一个题目.题目大意:有一个等式ax+ ...

  7. The equation - SGU 106(扩展欧几里得)

    题目大意:有一个二元一次方程,给出系数值和x与y的取值范围,求出来总共有多少对整数解. 分析:有以下几点情况. 1,系数a=0, b=0, 当c != 0的时候结果很明显是无解,当c=0的时候x,y可 ...

  8. 扩展欧几里德 SGU 106

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

  9. SGU 106.Index of super-prime

    时间限制:0.25s 空间限制:4M 题目大意:                 在从下标1开始素数表里,下标为素数的素数,称为超级素数(Super-prime),给出一个n(n<=10000) ...

随机推荐

  1. 积木(DP)问题

    问题:Do you remember our children time? When we are children, we are interesting in almost everything ...

  2. TYVJ1864 守卫者的挑战

    P1864 [Poetize I]守卫者的挑战 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜 ...

  3. NOIP2014 day2 T2 洛谷P2296 寻找道路

    题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...

  4. C#获取本机IP且过滤非真实网卡(如虚拟机网卡)

    参考了网上的文章,具体地址不记得了. 下面的方法可以过滤掉虚拟机的网卡等无效网卡,进而只留下真实的网卡. using System; using System.Collections.Generic; ...

  5. c++ struct的两个注意点

    1.C++的结构体变量在声明的时候可以省略struct,在c中这样是不可以的,例子如下 #include<iostream> #include<string> using na ...

  6. 如何在ECSHOP前台后台中讲版权内容清除

    如何在ECSHOP前台后台中讲版权内容清除 作者:河南电脑学校网 点击次数:1065 发布时间:2012-02-02 20:13:58 一.ECSHOP前台显示的页面的版权在下面几个地方修改:(本人不 ...

  7. Unable to open liblaunch_sim.dylib. Try reinstalling Xcode or the simulator

    关于Xcode7 Beta报错 simulator runtime is not available. Unable to open liblaunch_sim.dylib Try reinstall ...

  8. java中不带package和带package的编译运行方式

    Java中不带package的程序和带package的程序编译的方式是不同的. 一.不带package的程序建立个HelloWorld.java的文件,放入C:\,内容如下:public class ...

  9. 移动端bug总结

    1. 移动端按钮点击蓝框去除 button,input[type="button"] { outline:none; }

  10. python集合类型set

    set 类型的简单粗暴取出并集合交集  |   & li=[11,22,33] n_li=[44,55] b= (list(set(li)&set(n_li))) b2=set(li) ...