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]两两互 ...
随机推荐
- 【leetcode 简单】 第七题 合并两个有序链表
将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2- ...
- JS设计模式——4.继承(概念)
类式继承 0.构造函数 一个简单的Person类 function Person(name){ this.name = name; } Person.prototype.getName = funct ...
- PHP7+Nginx的配置与安装教程详解
下面脚本之家小编把PHP7+Nginx的配置与安装教程分享给大家,供大家参考,本文写的不好还请见谅. 系统环境:centos6.5 x64 软件版本:nginx-1.10.0 php-7.0.6 安装 ...
- 62.Unique Paths---dp
题目链接 题目大意:给一个m*n的方格,从左上角走到右下角,中间无任何障碍,问有多少种走法. 法一:DFS,超时,简单模板深搜,无任何剪枝,结果一半的数据超时.代码如下: public int uni ...
- URAL题解一
URAL题解一 URAL 1002 题目描述:一种记住手机号的方法就是将字母与数字对应,如图.这样就可以只记住一些单词,而不用记住数字.给出一个数字串和n个单词,用最少的单词数来代替数字串,输出对应的 ...
- Python如何实现文本转语音
准备 我测试使用的Python版本为2.7.10,如果你的版本是Python3.5的话,这里就不太适合了. 使用Speech API 原理 我们的想法是借助微软的语音接口,所以我们肯定是要进行调用 相 ...
- angular中使用AMEXIO
1.用NPM添加依赖到项目中,amexio需要先添加以下四个依赖到项目 npm install jquery@3.2.1 --save npm install bootstrap@4.0.0-alp ...
- Linux/Unix 怎样找出并删除某一时间点的文件
Linux/Unix 怎样找出并删除某一时间点的文件 在Linux/Unix系统中,我们的应用每天会产生日志文件,每天也会备份应用程序和数据库,日志文件和备份文件长时间积累会占用大量的存储空间,而有些 ...
- java关键字(详解)
目录 1. 基本类型 1) boolean 布尔型 2) byte 字节型 3) char 字符型 4) double 双精度 5) float 浮点 6) int 整型 7) long 长整型 8) ...
- C语言实现knn
以后写代码一定要谨慎,提高代码的正确率. /*************************************** * 1.初始化距离为最大值 * 2.计算未知样本和每个训练样本的距离为dis ...