Solutions to an Equation LightOJ - 1306
Solutions to an Equation LightOJ - 1306
一个基础的扩展欧几里得算法的应用。
解方程ax+by=c时,基本就是先记录下a和b的符号fla和flb(a为正则fla为1,为负则fla为-1,flb相同),然后对a和b取绝对值。求出ax+by=gcd(a,b)的一组解x=ansx,y=ansy,那么只有当c是gcd(a,b)的倍数时原方程才可能有解。设g=gcd(a,b),通解是x=ansx*(c/g)*fla+k*b/g*fla,y=ansy*(c/g)*flb-k*a/g*flb。这里设xa=ansx*(c/g)*fla,xb=b/g*fla,ya=ansy*(c/g)*flb,yb=-(a/g*flb)。
那么这题就是根据通解的式子和x和y的范围去求k的范围,基本操作就是手算一下解不等式。
举例:不等式(x相关):xa+k*xb>=x1,xa+k*xb<=x2
(解一下就会发现第一个式子解出的是k的最小值还是最大值,与xb的符号有关)
理论上不难,但是符号之类的细节实现起来有难度(...)。另外,a为0或b为0或a、b都为0时都需要特判(...)。
错误记录:
未特判0
upd:
以上应该有错。通解应该是x=ansx*fla+k*(b/g)*fla,y=ansy*flb-k*(a/g)*flb
通解就是那个没错的,但是要注意ansx和ansy是ax+by=gcd(a,b)的解,不是原方程的解。
如果有了一组原方程的解,求通解,那么就不要乘c/g
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
LL T,x,y,a,b,c,x1,x2,y11,y2,g;
LL gcd(LL a,LL b)
{
LL t;
while(b!=)
{
t=a;
a=b;
b=t%b;
}
return a;
}
LL exgcd(LL a,LL b,LL& x,LL& y)
{
if(b==)
{
x=;
y=;
return a;
}
else
{
LL t=exgcd(b,a%b,x,y);
LL t1=x;
x=y;
y=t1-a/b*y;
return t;
}
}
int main()
{
LL TT,fla,flb,xa,xb,ya,yb,kl,kr,kl1,kr1;
scanf("%lld",&T);
for(TT=;TT<=T;TT++)
{
scanf("%lld%lld%lld%lld%lld%lld%lld",&a,&b,&c,&x1,&x2,&y11,&y2);
c=-c;
if(a==&&b==)
{
if(c==)
{
printf("Case %lld: %lld\n",TT,max(0ll,(x2-x1+)*(y2-y11+)));
}
else
{
printf("Case %lld: %lld\n",TT,0ll);
}
continue;
}
if(a==)
{
if(c%b==&&(c/b>=y11)&&(c/b<=y2))
printf("Case %lld: %lld\n",TT,x2-x1+);
else
printf("Case %lld: %lld\n",TT,0ll);
continue;
}
if(b==)
{
if(c%a==&&(c/a>=x1)&&(c/a<=x2))
printf("Case %lld: %lld\n",TT,y2-y11+);
else
printf("Case %lld: %lld\n",TT,0ll);
continue;
}
fla=;flb=;
if(a<)
{
a=-a;
fla=-;
}
if(b<)
{
b=-b;
flb=-;
}
g=gcd(a,b);
if(c%g!=)
{
printf("Case %lld: %lld\n",TT,0ll);
continue;
}
exgcd(a,b,x,y);
xa=x*(c/g)*fla;
xb=b/g*fla;
ya=y*(c/g)*flb;
yb=-(a/g*flb);
//x=xa+k*xb,y=ya+k*yb
if(xb>)
{
kl=ceil((double)(x1-xa)/xb);
kr=floor((double)(x2-xa)/xb);
}
else
{
kr=floor((double)(x1-xa)/xb);
kl=ceil((double)(x2-xa)/xb);
}
if(yb>)
{
kl1=ceil((double)(y11-ya)/yb);
kr1=floor((double)(y2-ya)/yb);
}
else
{
kr1=floor((double)(y11-ya)/yb);
kl1=ceil((double)(y2-ya)/yb);
}
//if(kl1>kr1) swap(kl1,kr1);
printf("Case %lld: %lld\n",TT,max(min(kr1,kr)-max(kl1,kl)+,0ll));
}
return ;
}
Solutions to an Equation LightOJ - 1306的更多相关文章
- [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 ...
- Jordan Lecture Note-6: The Solutions of Nonlinear Equation.
The Solutions of Nonlinear Equation 本文主要介绍几种用于解非线性方程$f(x)=0$的一些方法. (1) Bisection Method. 算法: step 1: ...
- lightoj 1306 - Solutions to an Equation 扩展的欧几里得
思路:看题就知道用扩展的欧几里得算法做!!! 首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0. 继而可以得到ax+by=c的一个 ...
- LightOJ 1306 - Solutions to an Equation 裸EXGCD
本题是极其裸的EXGCD AX+BY+C=0 给你a b c 和x与y的区间范围,问你整数解有几组 作为EXGCD入门,题目比较简单 主要需要考虑区间范围的向上.向下取整,及正负符号的问题 问题是这正 ...
- (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 ...
随机推荐
- C语言的一些特殊使用方法————————【Badboy】
一:特殊的字符串宏 [cpp] #define A(x) T_##x #define B(x) #@x #define C(x) #x 我们如果x=1, 则上面的宏定义会被解释成下面的样子 A(1)- ...
- springboot对传参的拦截统一处理
在学习某网<java秒杀系统方案优化>的课程中,学到了一种springboot对传参的拦截统一处理的方式,特记录一下. 如后台方法一般需要根据token从Session中获取User对象, ...
- hihoCoder 1586 Minimum 【线段树】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1586 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, …, a2 ...
- gradle配置
一.你不想看到的 Gradle Build Running 话说在天朝当程序员也是很不容易的,不管是查阅资料还是下载东西,很多时候你会发现自己上网姿势不对,当然对大多数程序员来说,这都不是事儿.这次重 ...
- #1241 : Best Route in a Grid
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N行N列的非负整数方阵,从左上角(1,1)出发,只能向下或向右走,且不能到达值为0的方格,求出一条到达右下角的最佳 ...
- xalion三层与Web开发帖子一览表 good
使用http.sys,让delphi 的多层服务飞起来(Delphi借用http.sys充当http服务器,也就可以发送返回JSON等信息,当然浏览器也可以使用)http://www.cnblogs. ...
- vue 使用html2canvas将DOM转化为图片
一.前言 我发现将DOM转化为图片是一个非常常见的需求,而自己手动转是非常麻烦的,于是找到了html2canvas这个插件,既是用得比较多的也是维护得比较好的一个插件. 注意:版本比较多,这里介绍最新 ...
- File.Copy的时候Could not find a part of the path
https://developercommunity.visualstudio.com/content/problem/378265/filecopy-did-not-throw-the-correc ...
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- java8的常用的新特征
一.Java 8 对接口做了进一步的增强.(默认方法和静态方法) a. 在接口中可以添加使用 default 关键字修饰的非抽象方法.即:默认方法(或扩展方法不用被实现)如:comparator接口. ...