题目链接:http://lightoj.com/volume_showproblem.php?problem=1306

You have to find the number of solutions of the following equation:

Ax + By + C = 

Where A, B, C, x, y are integers and x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2.

Input
Input starts with an integer T (≤ ), denoting the number of test cases. Each case starts with a line containing seven integers A, B, C, x1, x2, y1, y2 (x1 ≤ x2, y1 ≤ y2). The value of each integer will lie in the range [-, ]. Output
For each case, print the case number and the total number of solutions.
Sample Input - -
- - - -
-
- - - -
-
Output for Sample Input
Case :
Case :
Case :
Case :
Case :

题意:给出AX+BY+C==0中的A,B,C。问在X1到X2与Y1到Y2的范围内有几组解

分析:利用扩展欧几里得算法

首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0.

继而可以得到ax+by=c的一个组解x1=c*x0/g , y1=c*y0/g。

这样可以得到ax+by=c的通解为:

                  x=x1+b*t;

                  y=y1-a*t;

再就是要注意符号问题!!!

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include <map>
#include <string>
#include <vector>
#include<iostream>
using namespace std;
#define N 10006
#define INF 0x3f3f3f3f
#define LL long long
#define mod 1000000007
LL ex_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==)
{
x = ;
y = ;
return a;
}
LL g = ex_gcd(b,a%b,x,y);
LL t = x;
x = y;
y = t- a/b * y;
return g;
}
int sign(LL n)
{
if(n==)
return ;
return n>?:-;
}
LL ceil(LL a,LL b)
{
int s = sign(a) * sign(b);
return b/a + (b%a!= && s>);
}
LL floor(LL a,LL b)
{
int s = sign(a) * sign(b);
return b/a - (b%a!= && s<);
}
int main()
{
int T,con=;
scanf("%d",&T);
LL a,b,c,x1,x2,y1,y2,x,y;
while(T--)
{
scanf("%lld %lld %lld %lld %lld %lld %lld",&a,&b,&c,&x1,&x2,&y1,&y2);
printf("Case %d: ",con++);
if(a== && b==)
{
if(c==)
{
printf("%lld\n",(x2-x1+)*(y2-y1+));
}
else
printf("0\n");
continue;
}
if(a==)
{
if(c%b!=)
{
printf("0\n");
continue;
}
LL s = -c/b;
if(s>=y1 && s<=y2)
printf("%lld\n",x2-x1+);
else
printf("0\n");
continue;
}
if(b==)
{
if(c%a!=)
{
printf("0\n");
continue;
}
LL s = -c/a;
if(s>=x1 && s<=x2)
printf("%lld\n",y2-y1+);
else
printf("0\n");
continue;
} LL g = ex_gcd(a,b,x,y);
if(c%g!=)
{
printf("0\n");
continue;
}
if(sign(g) * sign(b) <) swap(x1,x2);
LL l1 = ceil(b, g*x1 + c*x);
LL l2 = floor(b, g*x2 + c*x);
if(sign(-a) * sign(g) <) swap(y1,y2);
LL r1 = ceil(-a,g * y1 + c*y);
LL r2 = floor(-a,g*y2 + c*y);
l1 = max(l1,r1);
r1 = min(l2,r2);
if(l1>r1) printf("0\n");
else
printf("%lld\n",r1-l1 +);
}
return ;
}

(light oj 1306) Solutions to an Equation 扩展欧几里得算法的更多相关文章

  1. lightoj 1306 - Solutions to an Equation 扩展的欧几里得

    思路:看题就知道用扩展的欧几里得算法做!!! 首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0. 继而可以得到ax+by=c的一个 ...

  2. 1306 - Solutions to an Equation

    1306 - Solutions to an Equation    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Lim ...

  3. 扩展欧几里得算法(extgcd)

    相信大家对欧几里得算法,即辗转相除法不陌生吧. 代码如下: int gcd(int a, int b){ return !b ? gcd(b, a % b) : a; } 而扩展欧几里得算法,顾名思义 ...

  4. noip知识点总结之--欧几里得算法和扩展欧几里得算法

    一.欧几里得算法 名字非常高大上的不一定难,比如欧几里得算法...其实就是求两个正整数a, b的最大公约数(即gcd),亦称辗转相除法 需要先知道一个定理: gcd(a, b) = gcd(b, a  ...

  5. 欧几里得算法与扩展欧几里得算法_C++

    先感谢参考文献:http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html 注:以下讨论的数均为整数 一.欧几里得算法(重点是证 ...

  6. vijos1009:扩展欧几里得算法

    1009:数论 扩展欧几里得算法 其实自己对扩展欧几里得算法一直很不熟悉...应该是因为之前不太理解的缘故吧这次再次思考,回看了某位大神的推导以及某位大神的模板应该算是有所领悟了 首先根据题意:L1= ...

  7. ****ural 1141. RSA Attack(RSA加密,扩展欧几里得算法)

    1141. RSA Attack Time limit: 1.0 secondMemory limit: 64 MB The RSA problem is the following: given a ...

  8. 浅谈扩展欧几里得算法(exgcd)

    在讲解扩展欧几里得之前我们先回顾下辗转相除法: \(gcd(a,b)=gcd(b,a\%b)\)当a%b==0的时候b即为所求最大公约数 好了切入正题: 简单地来说exgcd函数求解的是\(ax+by ...

  9. 『扩展欧几里得算法 Extended Euclid』

    Euclid算法(gcd) 在学习扩展欧几里得算法之前,当然要复习一下欧几里得算法啦. 众所周知,欧几里得算法又称gcd算法,辗转相除法,可以在\(O(log_2b)\)时间内求解\((a,b)\)( ...

随机推荐

  1. 记一次尴尬的Java应用内存泄露排查

    这星期被线上JVM内存占用不断增大的问题所困扰,自己提出了一些假设,然后去实施验证都一一失败了,有一些经验和教训在这里分享下. 之所以是尴尬,是最后因为修复了另一个看似不相关的问题导致内存不再上升,但 ...

  2. 测试 ASP.NET Core API Controller

    本文需要您了解ASP.NET Core MVC/Web API, xUnit以及Moq相关知识. 这里有xUnit和Moq的介绍: https://www.cnblogs.com/cgzl/p/917 ...

  3. ASP.NET Core中使用GraphQL - 第四章 GraphiQL

    ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...

  4. 1.6部署到CentOS「深入浅出ASP.NET Core系列」

    希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. 安装.NET Core 官方安装地址: https://www.microsoft.com/net/learn/d ...

  5. SLAM+语音机器人DIY系列:(八)高阶拓展——2.centos7下部署Django(nginx+uwsgi+django+python3)

    0.安装步骤预览(1)系统默认自带python2.x,所以需要先安装python3.x(2)python2对应pip,python3对应pip3,用源码安装python3后pip3也自动安装了(3)用 ...

  6. Django-restframework 之权限源码分析

    Django-restframework 之权限源码分析 一 前言 上篇博客分析了 restframework 框架的认证组件的执行了流程并自定义了认证类.这篇博客分析 restframework 的 ...

  7. 程序猿想聊天 - 創問 4C 團隊教練心得(二)

    在第二天裡,主要談的是關於 Courage (勇氣) . Co-Create (共創) 的部分因為我們不是真實的團隊,就沒有繼續下去了 一早開始先回顧了一下前一天的內容,接下來我們就開始玩小遊戲 這個 ...

  8. aspx中的checkbox 取值问题

    问题:前台checkbox控件,选中值为1,不选值为0: 解决方案: 插入一行 <input type="hidden" name="RemberPwd" ...

  9. Elasticsearch与Hadoop集成大数据处理介绍

    传统大数据处理 现代数据架构 Hadoop在20业务场景的应用 DataLake A data lake is a system or repository of data stored in its ...

  10. 通用查询类封装之Mongodb篇

    查询在应用程序中很重要,花样也特别多,不同得业务需求需要不同的查询条件,还要支持and.or ……事实上也确实如此,程序中有N多个查询类,并且很可能其中有多个类查询同一张表,所以特别想弄一个通用的查询 ...