SGU 106 The equation 扩展欧几里得好题
扩展欧几里得的应用……见算法竞赛入门经典p.179
注意两点:1.解不等式的时候除负数变号
2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 && b=0 && c!=0 ) ( a=0 && b!=0 )( a!=0 && b=0 )
能加深对扩展欧几里得的理解,不错的一题
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm> #define LL long long int using namespace std; void ExGcd( LL a, LL b, LL &d, LL &x, LL &y )
{
if ( !b )
d = a, x = , y = ;
else
{
ExGcd( b, a % b, d, y, x );
y -= x * ( a / b );
}
return;
} int main()
{
LL a, b, c, x1, x2, y1, y2;
while ( scanf( "%I64d%I64d%I64d%I64d%I64d%I64d%I64d", &a, &b, &c, &x1, &x2, &y1, &y2 ) == )
{
if ( a == && b == )
{
if ( c == )
printf( "%I64d\n", (x2 - x1 + )*(y2 - y1 + ) );
else puts("");
continue;
}
if ( a == )
{
if ( (-c) % b == )
{
LL y = (-c) / b;
if ( y >= y1 && y <= y2 ) puts("");
else puts("");
}
else puts(""); continue;
}
if ( b == )
{
if ( (-c) % a == )
{
LL x = (-c) / a;
if ( x >= x1 && x <= x2 ) puts("");
else puts("");
}
else puts("");
continue;
}
LL g, x0, y0;
ExGcd( a, b, g, x0, y0 );
if ( (-c) % g == ) //如果有解
{
x0 = x0 * (-c) / g;
y0 = y0 * (-c) / g; LL aa = a / g;
LL bb = b / g;
LL low, high;
if ( aa > && bb > )
{
low = max( (x0 - x1) / bb, (y0 - y2) / aa );
high = min( (x2 - x0) / bb, (y0 - y1) / aa );
printf("%I64d\n", high - low + );
}
else if ( aa > && bb < )
{
low = max( (x2 - x0) / bb, (y0 - y2) / aa );
high = min( (x0 - x1) / bb, (y0 - y1) / aa );
printf("%I64d\n", high - low + );
}
else if ( aa < && bb > )
{
low = max( (x0 - x1) / bb, (y0 - y1) / aa );
high = min( (x2 - x0) / bb, (y0 - y2) / aa );
printf("%I64d\n", high - low + );
}
else if ( aa < && bb < )
{
low = max( (x2 - x0) / bb, (y0 - y1) / aa );
high = min( (x0 - x1) / bb, (y0 - y2) / aa );
printf("%I64d\n", high - low + );
}
}
else puts("");
}
return ;
}
SGU 106 The equation 扩展欧几里得好题的更多相关文章
- SGU 106 The Equation 扩展欧几里得应用
Sol:线性不定方程+不等式求解 证明的去搜下别人的证明就好了...数学题. #include <algorithm> #include <cstdio> #include & ...
- SGU 106 The equation 扩展欧几里德
106. The equation time limit per test: 0.25 sec. memory limit per test: 4096 KB There is an equation ...
- Codeforces7C 扩展欧几里得
Line Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
- 51nod1256 乘法逆元【扩展欧几里得】
给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的. Input 输入2个数M, N中间用 ...
- [poj1061]青蛙的约会<扩展欧几里得>
题目链接:http://poj.org/problem?id=1061 其实欧几里得我一直都知道,只是扩展欧几里得有点蒙,所以写了一道扩展欧几里得裸题. 欧几里得算法就是辗转相除法,求两个数的最大公约 ...
- SGU 140 扩展欧几里得
题目大意: 给定序列a[] , p , b 希望找到一个序列 x[] , 使a1*x1 + a2*x2 + ... + an*xn = b (mod p) 这里很容易写成 a1*x1 + a2*x2 ...
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
- [codeforces 200 E Tractor College]枚举,扩展欧几里得,三分
题目出自 Codeforces Round #126 (Div. 2) 的E. 题意大致如下:给定a,b,c,s,求三个非负整数x,y,z,满足0<=x<=y<=z,ax+by+cz ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
随机推荐
- Why Every Professional Should Consider Blogging
转自http://www.pixelstech.net/article/1327829407-Why-Every-Professional-Should-Consider-Blogging ften ...
- 【Merge K Sorted Lists】cpp
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- 《我是一只IT小小鸟》阅读笔记
<我是一只IT小小鸟>,这本书对我来说,有可能我现在并不懂得那其中的道理,但是,我觉得它写的很好,很现在的我很相似,但是在里面,我看到了他们都在说,一开始可能并不对IT这门课有很深的见解, ...
- 小技巧---查doc文档的index.html怎么用的和chm一样
看包里面是否有E:\Java\hibernate3.3.2\hibernate-annotations-3.4.0.GA\hibernate-annotations-3.4.0.GA\doc\refe ...
- 设计模式之状态模式(State)
状态模式原理:随着状态的变化,对象的行为也发生变化 代码如下: #include <iostream> #include <string> #include <list& ...
- mysql解决错误的方法-MySQL日志
1.使用ps -ef|grep mysql查询是否有与MySQL相关的僵尸进程,如果有则强制杀掉 2.在配置文件my.cnf中配置启动错误日志: log_error = /var/log/mysql/ ...
- extern关键字的使用
A.置于变量或者函数前,以标示变量或者函数的定义在别处,提示编译器遇到此变量和函数时在其他地方寻找其定义. B.可用来进行链接指定. 1.使用extern声明外部变量 1.1在一个文件内声明外部变量 ...
- HDOJ 1466 计算直线的交点数
将n 条直线排成一个序列,直线2和直线1最多只有一个交点,直线3和直线1,2最多有两个交点,......,直线n 和其他n-1条直线最多有n-1个交点.由此得出n条直线互不平行且无三线共点的最多交点数 ...
- 【面试题041】和为s的两个数字VS和为s的连续正数序列
[面试题041]和为s的两个数字VS和为s的连续正数序列 题目一: 输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s.如果有多对数字的和等于s,输出任意一对即可. ...
- touch事件学习
window.onload = function(){ var touch = { movetarget : false, touchStart : function (e) { console.lo ...