lightoj 1306 - Solutions to an Equation 扩展的欧几里得
思路:看题就知道用扩展的欧几里得算法做!!!
首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0.
继而可以得到ax+by=c的一个组解x1=c*x0/g , y1=c*y0/g。
这样可以得到ax+by=c的通解为:
x=x1+b*t;
y=y1-a*t;
再就是要注意符号问题!!!
代码如下:
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
ll gcd_extend(ll a,ll b,ll &x,ll &y)
{
if(b==){
x=;y=;
return a;
}
else{
ll g=gcd_extend(b,a%b,x,y);
ll t=x;
x=y;
y=t-a/b*y;
return g;
}
}
int sign(ll a)
{
if(a==) return ;
return a>?:-;
}
ll ceil(ll a,ll b) //向上取整,注意符号
{
int s=sign(a)*sign(b);
return b/a+(b%a!=&&s>);
}
ll floor(ll a,ll b) //向下取整,注意符号
{
int s=sign(a)*sign(b);
return b/a-(b%a!=&&s<);
}
int main()
{
int t,ca=;
ll a,b,c,x1,x2,y1,y2,x,y;
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld%lld%lld%lld%lld",&a,&b,&c,&x1,&x2,&y1,&y2);
printf("Case %d: ",++ca);
if(a==&&b==){
if(c==) printf("%lld\n",(x2-x1+)*(y2-y1+));
else printf("0\n");
continue;
}
if(a==){
if(c%b!=){
printf("0\n");
continue;
}
ll tt=-c/b;
if(y1<=tt&&tt<=y2) printf("%lld\n",x2-x1+);
else printf("0\n");
continue;
}
if(b==){
if(c%a!=){
printf("0\n");
continue;
}
ll tt=-c/a;
if(x1<=tt&&tt<=x2) printf("%lld\n",y2-y1+);
else printf("0\n");
continue;
}
ll g=gcd_extend(a,b,x,y);
if(c%g!=){
printf("0\n");
continue;
}
if(sign(g)*sign(b)<) swap(x1,x2);
ll l1=ceil(b,g*x1+c*x);
ll l2=floor(b,g*x2+c*x);
if(sign(-a)*sign(g)<) swap(y1,y2);
ll r1=ceil(-a,g*y1+c*y);
ll r2=floor(-a,g*y2+c*y);
l1=max(l1,r1);
r1=min(l2,r2);
if(l1>r1) printf("0\n");
else printf("%lld\n",r1-l1+);
}
return ;
}
lightoj 1306 - Solutions to an Equation 扩展的欧几里得的更多相关文章
- LightOJ 1306 - Solutions to an Equation 裸EXGCD
本题是极其裸的EXGCD AX+BY+C=0 给你a b c 和x与y的区间范围,问你整数解有几组 作为EXGCD入门,题目比较简单 主要需要考虑区间范围的向上.向下取整,及正负符号的问题 问题是这正 ...
- 1306 - Solutions to an Equation
1306 - Solutions to an Equation PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Lim ...
- [lightoj P1306] Solutions to an Equation
[lightoj P1306] Solutions to an Equation You have to find the number of solutions of the following e ...
- (Relax 数论1.6)POJ 1061 青蛙的约会(扩展的欧几里得公式)
/* * POJ_1061.cpp * * Created on: 2013年11月19日 * Author: Administrator */ #include <iostream> # ...
- (light oj 1306) Solutions to an Equation 扩展欧几里得算法
题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...
- 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: ...
- SGU 106 The equation 扩展欧几里得好题
扩展欧几里得的应用……见算法竞赛入门经典p.179 注意两点:1.解不等式的时候除负数变号 2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 ...
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
随机推荐
- python基本数据类型list,tuple,set,dict用法以及遍历方法
1.list类型 类似于java的list类型,数据集合,可以追加元素与删除元素. 遍历list可以用下标进行遍历,也可以用迭代器遍历list集合 建立list的时候用[]括号 import sys ...
- Cesium 初始化Viewer
<pre name="code" class="javascript"><script> var viewer = new Cesium ...
- python基础===用9种方式生成新的对象
class Point: def __init__(self, x, y): self.x = x self.y = y point1 = Point(1, 2) point2 = eval(&quo ...
- Android设备相关配置
http://source.android.com/devices/tech/storage/index.html Android supports devices with external sto ...
- 关于"轉淚點"与"轉捩點"
经常看台湾偶像剧或台湾综艺节目的人,一定听过"转泪点"这个词,虽然我一直不知道这三个字具体是怎么写, 但其意思很容易明白,就是"转折点"的意思.今天无聊在看凤凰 ...
- 未找到与约束 ContractName Microsoft.VisualStudio.Utilitues.IContentTypeRegistryService......
1.问题提出 用VS 2013 with Update5 开发项目,点击项目中的文件,发现打不开,抛出如下的错误. 错误提示: 未找到与约束 ContractName Microsoft.Visual ...
- 处理tomcat内存溢出问题
TOMCAT起步内存溢出问题Exception in thread ""http-bio-8080"-exec-java.lang.OutOfMemoryError: P ...
- Retrofit:类型安全的REST客户端for 安卓&Java
Retrofit:类型安全的REST客户端for 安卓&Java 2014年5月5日 星期一 21:11 官网: http://square.github.io/retrofit/ GitH ...
- 实习day2:@2X图片,git,coding.net,
@2X是5和6系列的图片,@3X是6P等大屏的图片 本公司目前只用@2X的图片适配. 比如20X27的图片 1x, 就是原始大小: 用2X, 就除以2,变成10X13.5: 如果用3X的, 就除以3, ...
- csu 1552(米勒拉宾素数测试+二分图匹配)
1552: Friends Time Limit: 3 Sec Memory Limit: 256 MBSubmit: 723 Solved: 198[Submit][Status][Web Bo ...