luogu2833 等式
题目大意
给出\(a,b,c,x_1,x_2,y_1,y_2\),求满足\(ax+by+c=0\),且\(x\in[x1,x2],y\in [y1,y2]\)的整数解有多少对。
题解
用扩展欧几里得算法算出方程\(ax+by=-c\)的一个解,再将该解移动到题目所要求的范围内。具体操作看代码。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
#define NoAns {printf("0\n");return 0;}
ll Exgcd(ll a, ll b, ll &x, ll &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
ll d = Exgcd(b, a % b, x, y);
ll tx = x;
x = y;
y = tx - y * (a / b);
return d;
}
ll Gcd(ll a, ll b)
{
return b ? Gcd(b, a%b) : a;
}
bool Solve(ll a, ll b, ll c, ll &x, ll &y, ll &deltaX, ll &deltaY)
{
int gcd = Gcd(a, b);
if (c%gcd != 0)
return false;
Exgcd(a, b, x, y);//易忘点:好好下定义,传入的参数不是a%gcd,b%gcd
x *= c / gcd;
y *= c / gcd;
deltaX = b / gcd;
deltaY = -a / gcd;//易忘点:负号
return true;
}
ll MoveToRange(ll orgP, ll delta, ll l, ll r, ll toP, bool &moveFailed)
{
ll k = (toP - orgP) / delta;
int higherP = delta > 0 ? 1 : -1;
if (orgP + k * delta > r)
k-=higherP;//注意此处不能直接--,因为delta<0时k越小k*delta越大
else if (orgP + k * delta < l)
k+=higherP;
if (orgP + k * delta > r || orgP + k * delta < l)
moveFailed = true;
return k;
}
int main()
{
ll a, b, c, x1, x2, y1, y2;
scanf("%lld%lld%lld%lld%lld%lld%lld", &a, &b, &c, &x1, &x2, &y1, &y2);
if (x2 < x1 || y2 < y1)
NoAns
if (a == 0 && b == 0)
{
if (c == 0)
printf("%lld\n", (x2 - x1 + 1) * (y2 - y1 + 1));
else
NoAns
return 0;
}
if (a == 0)
{
if (c%b == 0 && y1 <= -c / b && -c / b <= y2)
printf("%lld\n", x2 - x1 + 1);
else
NoAns
return 0;
}
if (b == 0)
{
if (c%a == 0 && x1 <= -c / a && -c / a <= x2)
printf("%lld\n", y2 - y1 + 1);
else
NoAns
return 0;
}
ll x, y, deltaX, deltaY;
if (!Solve(a, b, -c, x, y, deltaX, deltaY))
NoAns
bool moveFailed = false;
ll kx1 = MoveToRange(x, deltaX, x1, x2, x1, moveFailed);
ll kx2 = MoveToRange(x, deltaX, x1, x2, x2, moveFailed);
ll ky1 = MoveToRange(y, deltaY, y1, y2, y1, moveFailed);
ll ky2 = MoveToRange(y, deltaY, y1, y2, y2, moveFailed);
if (moveFailed)
NoAns
if (kx1 > kx2)
swap(kx1, kx2);
if (ky1 > ky2)
swap(ky1, ky2);
ll kLow = max(kx1, ky1), kHigh = min(kx2, ky2);
if (kLow > kHigh)
NoAns
printf("%lld\n", kHigh - kLow + 1);
return 0;
}
luogu2833 等式的更多相关文章
- NOIP200806 火柴棒等式【B005】
[B005]火柴棒等式[难度B]———————————————————————————————————————————————————————————— [题目要求] 给你n根火柴棍,你可以拼出多少个 ...
- BZOJ2118墨墨的等式[数论 最短路建模]
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1317 Solved: 504[Submit][Status][Discus ...
- 【bzoj2118】 墨墨的等式
http://www.lydsy.com/JudgeOnline/problem.php?id=2118 (题目链接) 题意 给出${B}$的取值范围${[Bmin,Bmax]}$,求方程${a_{1 ...
- Bzoj2118 墨墨的等式
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1488 Solved: 578 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+ ...
- NOIP2008提高组火柴棒等式(模拟)——yhx
题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 加号与等号各自 ...
- 数论+spfa算法 bzoj 2118 墨墨的等式
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1283 Solved: 496 Description 墨墨突然对等式很感兴 ...
- TYVJ P1012 火柴棒等式 Label:枚举
背景 NOIP2008年提高组第二题 描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法 ...
- noip2008 火柴棒等式
P1149 火柴棒等式 1.9K通过 3.7K提交 题目提供者该用户不存在 标签搜索/枚举模拟2008NOIp提高组 难度普及- 提交该题 讨论 题解 记录 题目描述 给你n根火柴棍,你可以拼出多 ...
- SCAU 1138 代码等式
1138 代码等式 时间限制:500MS 内存限制:65536K提交次数:59 通过次数:21 题型: 编程题 语言: 无限制 Description 一个代码等式就是形如x1x2...xi=y ...
随机推荐
- oracle排序union和union all区别
是这样的,表格中有几个属性,比如age吧是之一,age是字符类型的数字,select之间由union连接,此时是无法对前面的select语句进行order by的,也就是无法排序,无法达成我要的按ag ...
- 关于Core里的 StartUp里的方法的理解。
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; ...
- 折纸---珠穆朗玛问题----简单for 循环
一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)? package com.zuoye.test; public class Zhezhi { public s ...
- 时序分析:HMM模型(状态空间)
关于HMM模型:时序分析:隐马尔科夫模型 HMM用于手势识别: 训练时每一种手势对应一个HMM-Model,识别率取最大的一个HMM即可. 类似于一个封装的完成多类识别器功能单层网络. 优点: 尤其 ...
- (转)Arcgis for js加载天地图
http://blog.csdn.net/gisshixisheng/article/details/44494715 综述:本节讲述的是用Arcgis for js加载天地图的切片资源. 天地图的切 ...
- 关于JsonArray与JsonObject的使用
学习地址:http://blog.csdn.net/lishuangzhe7047/article/details/28880009 关于前台向后台传递数组(里面包含json格式) [{"i ...
- luoguP3979 遥远的国度 LCT+multiset维护子树信息
Code: #include<bits/stdc++.h> #define maxn 150000 #define ll long long #define inf 21474836470 ...
- Microsoft Visual Studio 常用快捷键总结
table tr:nth-child(odd){ background: #FFFFCC; } table tr:nth-child(even){ background: #FFFF99; } Mic ...
- centos7关闭防火墙以及查看防火墙状态
1.关闭firewall:systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止fir ...
- 渗透实战(周一):VMmare Fusion安装Kali Linux和win7虚拟机
高配笔记本电脑i5/8G/265G .VMware Fusion .Kali Linux镜像 .Win7镜像 第一:下载软件 1.如果你是苹果系统,建议下载最新VMware Fusion 11.0. ...