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 ...
随机推荐
- js如何使浏览器允许脚本异步加载
js如何使浏览器允许脚本异步加载 如果脚本体积很大,下载和执行的时间就会很长,因此造成浏览器堵塞,用户会感觉到浏览器“卡死”了,没有任何响应.这显然是很不好的体验,所以浏览器允许脚本异步加载,下面就是 ...
- Thunder团队第三周 - Scrum会议6
Scrum会议6 小组名称:Thunder 项目名称:i阅app Scrum Master:宋雨 工作照片: 代秋彤照相,所以图片中没有该同学. 参会成员: 王航:http://www.cnblogs ...
- pthon_flask小汇总
一.Jinja2中的关键字 1.include关键字 用include可以导入另外一个模板到当前模板中 <pre> {% include 'header.html' %} Body {% ...
- ACM hust 2.1
来自咸鱼王的呻吟 http://www.xiami.com/song/3599639?spm=a1z1s.3521865.23309997.1.PbLu7E 配合咸鱼食用效果更佳(右键新窗口打开) 题 ...
- lintcode-167-链表求和
167-链表求和 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和. 样例 给出两个链 ...
- 理解windows模型
同步 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回.按照这个定义,其实绝大多数函数都是同步调用(例如sin, isdigit等).但是一般而言,我们在说同步.异步的时候,特指 ...
- AndroidStudio3.0 注解报错Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor.
把Androidstudio2.2的项目放到3.0里面去了,然后开始报错了. 体验最新版AndroidStudio3.0 Canary 8的时候,发现之前项目的butter knife报错,用到注解的 ...
- context.getResourceAsStream获取的是部署在服务器上面的文件位置 而不是我们本地的工程位置 意思是说获取的都是web下面的文件位置
context.getResourceAsStream获取的是部署在服务器上面的文件位置 而不是我们本地的工程位置 意思是说获取的都是web下面的文件位置
- 【刷题】BZOJ 4827 [Hnoi2017]礼物
Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一个送给她.每个手环上各有 n 个装饰物,并且每个装饰物都有一定的亮度.但是在 ...
- bzoj2165: 大楼 (矩阵快速幂)
//========================== 蒟蒻Macaulish:http://www.cnblogs.com/Macaulish/ 转载要声明! //=============== ...