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 ...
随机推荐
- Android开发 使用 adb logcat 显示 Android 日志
作者 : 万境绝尘 转载请著名出处 eclipse 自带的 LogCat 工具太垃圾了, 开始用 adb logcat 在终端查看日志; 1. 解析 adb logcat 的帮助信息 在命令行中输入 ...
- 有关c#的学习笔记整理与心得
[ 塔 · 第 一 条 约 定 ] 整理c#:Array Arraylist List Hashtable Dictionary Stack Queue等 Array 的容量是固定的,而 ArrayL ...
- Java中的死锁问题
死锁问题: 例如有两个线程, 线程1与线程2. 线程1在执行的过程中, 要锁定对象1, 2才能完成整个操作, 首先锁定对象1, 再锁定对象2. 线程2在执行的过程中, 要锁定对象2, 1才能完成整个操 ...
- kmeans算法理解及代码实现
github:kmeans代码实现1.kmeans代码实现2(包含二分k-means) 本文算法均使用python3实现 1 聚类算法 对于"监督学习"(supervised ...
- iOS开发解决 jsonModel 属性跟系统的重复
-(id)initWithDic:(NSDictionary *)dic { if (self = [super init]) { [self setValuesForKeysWithDictiona ...
- IOS开发NSBundle使用
bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBu ...
- centOS6.5如何从启动界面直接进入命令行界面
进入系统后,按Ctrl+Alt+Fn可以切换控制台,其中F1~F6是字符控制台,F7~F12是X控制台 如果启动直接进入字符控制台,而不是X Window,可以编辑/etc/inittab将id:5: ...
- Jenkins系列-Jenkins构建触发器
触发器说明 build whenever a snapshot dependency is built,当job依赖的快照版本被build时,执行本job. 触发远程构建 (例如,使用脚本):这里使用 ...
- centos 安装mod_wsgi
如果自定义升级过了python到2.7 #./configure --with-apxs=/usr/sbin/apxs --with-python=/usr/local/python27/bin/py ...
- 浅述Try {} Catch{} 作用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test ...