思路:看题就知道用扩展的欧几里得算法做!!!

首先我们可以求出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 扩展的欧几里得的更多相关文章

  1. LightOJ 1306 - Solutions to an Equation 裸EXGCD

    本题是极其裸的EXGCD AX+BY+C=0 给你a b c 和x与y的区间范围,问你整数解有几组 作为EXGCD入门,题目比较简单 主要需要考虑区间范围的向上.向下取整,及正负符号的问题 问题是这正 ...

  2. 1306 - Solutions to an Equation

    1306 - Solutions to an Equation    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Lim ...

  3. [lightoj P1306] Solutions to an Equation

    [lightoj P1306] Solutions to an Equation You have to find the number of solutions of the following e ...

  4. (Relax 数论1.6)POJ 1061 青蛙的约会(扩展的欧几里得公式)

    /* * POJ_1061.cpp * * Created on: 2013年11月19日 * Author: Administrator */ #include <iostream> # ...

  5. (light oj 1306) Solutions to an Equation 扩展欧几里得算法

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...

  6. Solutions to an Equation LightOJ - 1306

    Solutions to an Equation LightOJ - 1306 一个基础的扩展欧几里得算法的应用. 解方程ax+by=c时,基本就是先记录下a和b的符号fla和flb(a为正则fla为 ...

  7. Jordan Lecture Note-6: The Solutions of Nonlinear Equation.

    The Solutions of Nonlinear Equation 本文主要介绍几种用于解非线性方程$f(x)=0$的一些方法. (1) Bisection Method. 算法: step 1: ...

  8. SGU 106 The equation 扩展欧几里得好题

    扩展欧几里得的应用……见算法竞赛入门经典p.179 注意两点:1.解不等式的时候除负数变号 2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 ...

  9. poj 2891 扩展欧几里得迭代解同余方程组

    Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...

随机推荐

  1. localStorage H5本地存储

    域内安全.永久保存.即客户端或浏览器中来自同一域名的所有页面都可访问localStorage数据且数据除了删除否则永久保存,但客户端或浏览器之间的数据相互独立. <!doctype html&g ...

  2. Java的继承和多态

    看了博客园里面的一个文章,关于java的继承和多态: class A ...{ public String show(D obj)...{ return ("A and D"); ...

  3. HTML表单属性与全局属性

    1.全局属性

  4. linux下的僵尸进程处理SIGCHLD信号【转】

    转自:http://www.cnblogs.com/wuchanming/p/4020463.html 什么是僵尸进程? 首先内核会释放终止进程(调用了exit系统调用)所使用的所有存储区,关闭所有打 ...

  5. cmder中文显示相关问题解决方案(1.3以上版本)

    cmder虽然Windows命令行的进阶版,虽然好看易用,但其中文编码一直是个问题.网上有不少博客给出解决方案,大部分都已因为版本更新失效.本文解决方案针对1.3以上版本的cmder用户 中文字体重叠 ...

  6. shell视频

    本帖最后由 Shell_HAT 于 2014-04-18 16:51 编辑 尚观全套RHCE视频http://pan.baidu.com/s/1pJvzVR1 马哥网络班-中级视频内容http://p ...

  7. Java显式锁学习总结之四:ReentrantLock源码分析

    概述 ReentrantLock,即重入锁,是一个和synchronized关键字等价的,支持线程重入的互斥锁.只是在synchronized已有功能基础上添加了一些扩展功能. 除了支持可中断获取锁. ...

  8. DB2和Oracle中Date比较

  9. tomcat arp

    tcnative-1应该放在JDK\bin目录下,而不是tomcat\bin目录下.

  10. yii2联表查询

    我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order          (id  order_ ...