LightOJ 1306 - Solutions to an Equation 裸EXGCD
本题是极其裸的EXGCD
AX+BY+C=0
给你a b c 和x与y的区间范围,问你整数解有几组
作为EXGCD入门,题目比较简单 主要需要考虑区间范围的向上、向下取整,及正负符号的问题
*问题是这正负号判断考虑让我WA无数次* **我好菜阿**
> 补充:关于使用扩展欧几里德算法解决不定方程的办法
>对于不定整数方程pa+qb=c,若 c mod Gcd(p, q)=0,则该方程存在整数解,否则不存在整数解。
>上面已经列出找一个整数解的方法,在找到p * a+q * b = Gcd(p, q)的一组解p0,q0后,p * a+q * b = Gcd(p, q)的其他整数解满足:
>p = p0 + b/Gcd(p, q) * t
>q = q0 – a/Gcd(p, q) * t(其中t为任意整数)
>至于pa+qb=c的整数解,只需将p * a+q * b = Gcd(p, q)的每个解乘上 c/Gcd(p, q) 即可
>——摘自他人博客
/** @Date : 2016-10-21-15.24
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link :
* @Version : $
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <math.h>
#include <string>
#include <stack>
#include <queue>
#define LL long long
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5+2000;
LL exgcd(LL a, LL b, LL &x, LL &y)
{
LL d = a;
if(b == 0)
{
x = 1;
y = 0;
}
else
{
d = exgcd(b, a % b, y, x);
y -= (a / b)*x;
}
return d;
}
int main()
{
int T;
int cnt = 0;
cin >> T;
while(T--)
{
LL a, b, c, x1, x2, y1, y2;
scanf("%lld%lld%lld", &a, &b, &c);
scanf("%lld%lld%lld%lld",&x1, &x2, &y1, &y2);
c = -c;
if(a < 0)
{
a = -a;
swap(x1, x2);
x1 = -x1;
x2 = -x2;
}
if(b < 0)
{
b = -b;
swap(y1, y2);
y1 = -y1;
y2 = -y2;
}
printf("Case %d: ", ++cnt);
LL x0 = 0 , y0 = 0;
LL g = exgcd(a, b, x0, y0);
if(g!=0 && c % g != 0)
{
printf("0\n");
continue;
}
if(a == 0 && b == 0)
{
if(!c)
printf("%lld\n", (x2 - x1 + 1) * (y2 - y1 + 1));
else printf("0\n");
continue;
}
if(a == 0)
{
if(c / b >= y1 && c/b <= y2)
printf("%lld\n", x2 - x1 + 1);
else printf("0\n");
continue;
}
if(b == 0)
{
if(c / a >= x1 && c / a <= x2)
printf("%lld\n", y2 - y1 + 1);
else printf("0\n");
continue;
}
x0 = x0 * c / g;
y0 = y0 * c / g;
a /= g;
b /= g;
LL l = ceil((double)(x1 - x0)/(double)(b));
LL r = floor((double)(x2 - x0)/(double)(b));
LL w = ceil((double)(y0 - y2)/(double)(a));
LL e = floor((double)(y0 - y1)/(double)(a));
LL q = max(l, w);
LL p = min(r, e);
if(p < q)
printf("0\n");
else printf("%lld\n", p - q + 1);
}
return 0;
}
LightOJ 1306 - Solutions to an Equation 裸EXGCD的更多相关文章
- lightoj 1306 - Solutions to an Equation 扩展的欧几里得
思路:看题就知道用扩展的欧几里得算法做!!! 首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0. 继而可以得到ax+by=c的一个 ...
- [lightoj P1306] Solutions to an Equation
[lightoj P1306] Solutions to an Equation You have to find the number of solutions of the following e ...
- 1306 - Solutions to an Equation
1306 - Solutions to an Equation PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Lim ...
- Solutions to an Equation LightOJ - 1306
Solutions to an Equation LightOJ - 1306 一个基础的扩展欧几里得算法的应用. 解方程ax+by=c时,基本就是先记录下a和b的符号fla和flb(a为正则fla为 ...
- Jordan Lecture Note-6: The Solutions of Nonlinear Equation.
The Solutions of Nonlinear Equation 本文主要介绍几种用于解非线性方程$f(x)=0$的一些方法. (1) Bisection Method. 算法: step 1: ...
- (light oj 1306) Solutions to an Equation 扩展欧几里得算法
题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...
- [ACM_数学] Counting Solutions to an Integral Equation (x+2y+2z=n 组合种类)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27938#problem/E 题目大意:Given, n, count the numbe ...
- [LeetCode] Solve the Equation 解方程
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...
- [Swift]LeetCode640. 求解方程 | Solve the Equation
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...
随机推荐
- JavaScript中的事件代理/委托
事件委托在JS高级程序设计中的定义为"利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件" 如何理解上面的这句话呢,在网上,大牛们一般都使用收快递这个例子来解释的, ...
- C++ STL victor
一.介绍 vector是表示可变大小数组的序列容器. 就像数组一样,vector也采用的连续存储空间来存储元素.也就是意味着可以采用下标对vector的元素进行访问,和数组一样高效.但是又不像数组,它 ...
- DAY2敏捷冲刺
站立式会议 工作安排 (1)服务器配置 (2)数据库连接 (3)页面创意 燃尽图 代码提交记录 感想 林一心:centos配置服务器真的算是一个不小的坑,目前数据库配置清楚,脚本部署好明天测试交互,还 ...
- 3dContactPointAnnotationTool开发日志(五)
今天要做的第一件事就是把obj文件里不同的对象分割开来. 通过仔细观察发现obj文件中以"o "开头的行会跟着一个对象的名字.g代表对象所属组名,我这里只要用到对象名就行了 ...
- [CLR via C#]引用类型和值类型
一.引用类型与值类型的区别 CLR支持两种类型:引用类型和值类型.引用类型总是从托管堆上分配的,C#的new操作符会返回对象的内存地址.使用引用类型时,必须注意到一些性能问题. 1)内存必须从托管堆上 ...
- Android Camera多屏幕适配解决预览照片拉伸
通常,拍照预览页面的照片拉伸主要与下面两个因素有关: 1. Surfaceview的大小 2. Camera中的Preview的大小 如下图: 图中preview显示的是手机支 ...
- [C/C++] 字符串错题集
1. 答案:A 这里考查转义字符,注意 \\ 表示字符 \\123表示字符 {\t 表示制表符这些都是一个字符. 2. 答案C 先不看有没有重复的,共5个字母,有5×4×3×2×1 = 120种组合. ...
- hdu 1054 Strategic Game (二分匹配)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Hadoop——HDFS的构架
在使用一个工具之前,应该先对它的机制.组成等有深入的了解,以后才会更好的使用它.下面来介绍一下什么是HDFS,以及他的构架是什么样的. 1.什么是HDFS? Hadoop主要是用于进行大数据处理,那么 ...
- Linux相关——画图软件安装
其实也不知道算不算Linux相关了... 装个画图软件还是很方便的,刚刚试了一下kolourpaint,感觉还行,就记录下来吧. 先记录几个快捷键emmmm print ---->全屏截图 al ...