(light oj 1306) Solutions to an Equation 扩展欧几里得算法
2024-10-13 07:04:50
题目链接:http://lightoj.com/volume_showproblem.php?problem=1306
You have to find the number of solutions of the following equation: Ax + By + C = Where A, B, C, x, y are integers and x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2. Input
Input starts with an integer T (≤ ), denoting the number of test cases. Each case starts with a line containing seven integers A, B, C, x1, x2, y1, y2 (x1 ≤ x2, y1 ≤ y2). The value of each integer will lie in the range [-, ]. Output
For each case, print the case number and the total number of solutions.
Sample Input - -
- - - -
-
- - - -
-
Output for Sample Input
Case :
Case :
Case :
Case :
Case :
题意:给出AX+BY+C==0中的A,B,C。问在X1到X2与Y1到Y2的范围内有几组解
分析:利用扩展欧几里得算法
首先我们可以求出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<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include <map>
#include <string>
#include <vector>
#include<iostream>
using namespace std;
#define N 10006
#define INF 0x3f3f3f3f
#define LL long long
#define mod 1000000007
LL ex_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==)
{
x = ;
y = ;
return a;
}
LL g = ex_gcd(b,a%b,x,y);
LL t = x;
x = y;
y = t- a/b * y;
return g;
}
int sign(LL n)
{
if(n==)
return ;
return n>?:-;
}
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,con=;
scanf("%d",&T);
LL a,b,c,x1,x2,y1,y2,x,y;
while(T--)
{
scanf("%lld %lld %lld %lld %lld %lld %lld",&a,&b,&c,&x1,&x2,&y1,&y2);
printf("Case %d: ",con++);
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 s = -c/b;
if(s>=y1 && s<=y2)
printf("%lld\n",x2-x1+);
else
printf("0\n");
continue;
}
if(b==)
{
if(c%a!=)
{
printf("0\n");
continue;
}
LL s = -c/a;
if(s>=x1 && s<=x2)
printf("%lld\n",y2-y1+);
else
printf("0\n");
continue;
} LL g = ex_gcd(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 ;
}
最新文章
- react入门(2)
- 12.创建一个Point类,有成员变量x,y,方法getX(),setX(),还有一个构造方 法初始化x和y。创建类主类A来测试它。
- ABAP 加密解密程序
- 简单理解php的socket编程
- Java构造和解析Json数据的两种方法详解二
- Locker
- 使用ANR-WatchDog来检測ANR
- android Bind机制(二)
- 关于flying框架
- 试着把.net的GC讲清楚(2)
- asp.net core系列 40 Web 应用MVC 介绍与详细示例
- 2018-2019-2 20175317 实验一《Java开发环境的熟悉》实验报告
- Mybatis 与hibernate
- 【Python】【有趣的模块】【Requests】session &; cookie
- Redis 5.0 安装
- Linux下多节点SSH无密码互联实现
- 【转】UML类图符号 6种关系说明以及举例
- linux,chmod如何更改所有子目录的权限,要包括隐藏文件
- BeautifulSoup中的select方法
- 【RF库Collections测试】Remove From Dictionary