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自定义web框架、Jinja2
			
WSGI(Web Server Gateway Interface)是一种规范,它定义了使用python编写的web app与web server之间接口格式,实现web app与web server ...
 - Vue修改mint-ui默认样式(默认风格)
			
加入my-mint.css 我这里用的postcss的变量定义,如果亲们用的是其他预处理器,要改成其他处理器的定义方式 覆盖mint-ui的primary颜色,改为自己UI的主题色 --main-co ...
 - 77.PS接收来自PL的按键中断
			
本篇文章主要介绍外设(PL)产生的中断请求,在PS端进行处理. 在PL端通过按键产生中断,PS接受到之后点亮相应的LED. 本文所使用的开发板是zedboardPC 开发环境版本:Vivado 201 ...
 - http 错误代码解释 && nginx 自定义错误【转】
			
如果向您的服务器发出了某项请求要求显示您网站上的某个网页(例如,当用户通过浏览器访问您的网页或在 Googlebot 抓取该网页时),那么,您的服务器会返回 HTTP 状态代码以响应该请求. 此状态代 ...
 - openjudge-NOI 2.6-1775 采药
			
题目链接:http://noi.openjudge.cn/ch0206/1775/ 题解: 很经典的01背包问题,设时间为t,价值为v 一维压缩,状态转移方程fj=max(fj,fj-ti+vi) # ...
 - centos上git搭建
			
1 git的安装需要一些包: yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-Ex ...
 - jsonpath for js
			
/** * @license * JSONPath 0.8.0 - XPath for JSON * * Copyright (c) 2007 Stefan Goessner (goessner.ne ...
 - acm专题---键树
			
题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...
 - python函数库及函数标准库
			
一.系统库提供的内部函数 字符函数库: 1)str.islower() :字符串是否全部是小写 2)str.isspace() :字符串是否为空 3)help(str):查询字符串函数库 4)str. ...
 - phpmailer发送邮件出现错误:stream_socket_enable_crypto(): SSL operation failed with code 1.
			
如果开了调试,调试进去会看到错误提示: smtp_code:"stream_socket_enable_crypto(): SSL operation failed with code 1. ...