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 ...
随机推荐
- python函数中的位置参数、默认参数、关键字参数、可变参数区别
一.位置参数 调用函数时根据函数定义的参数位置来传递参数. #!/usr/bin/env python # coding=utf-8 def print_hello(name, sex): sex_d ...
- $http.get(...).success is not a function 错误解决
$http.get(...).success is not a function 错误解决 1.6 新版本的AngularJs中用then和catch 代替了success和error,用PRomis ...
- Git 命令基本应用
两种建立仓库的方法: (1)在本地文件路径下建立仓库:git init (2)在代码托管网站上克隆项目:git clone [url] 查看该分支下的文件情况:git status 添加远程仓库源:g ...
- 寒假作业end
开始写博客的个人体会 自己打的链表过不了,果然,心存侥幸是不行的,被揪出来也不错,很感谢畅畅酱. 学术诚信的重要性 爱因斯坦说过:"大多数人说是才智造就了伟大的科学家,他们错了,是人格.&q ...
- 福大软工1816:Alpha(7/10)
Alpha 冲刺 (7/10) 队名:Jarvis For Chat 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.完成 ...
- c++远征
---恢复内容开始--- 这两天初步接触了C++,抱着一种对这两个加号的理解的心态走进这门语言的学习. 1.mooc--慕课网c++课程链接:http://www.imooc.com/learn/34 ...
- Ubuntu使用时遇到的问题
启动时显示System program problem detected 解决办法: 打开命令行窗口:Ctrl+Alt+T 执行命令:sudo gedit /etc/default/apport 把e ...
- Python的7种性能测试工具:timeit、profile、cProfile、line_profiler、memory_profiler、PyCharm图形化性能测试工具、objgraph
1.timeit: >>> import timeit >>> def fun(): ): a = i * i >>> timeit.timeit ...
- PHP中如何使用Redis接管文件存储Session详解
https://www.jb51.net/article/151580.htm 前言 php默认使用文件存储session,如果并发量大,效率会非常低.而redis对高并发的支持非常好,可以利用red ...
- 浅述Try {} Catch{} 作用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test ...