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个点,求射 ...
随机推荐
- 2015最新百度搜索引擎(seo优化)排名算法
多少年来,对于弄清百度排名算法成为了一代又一代站长的最高目标.随着百度推出了搜索引擎网页质量**,直接揭开了神秘的百度排名算法,这是作为站长福音啊.现在小编就来为大家介绍一下. 首先想要得到直接需要的 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- android 开发 drawerlayout出现退不回去的情况
问题原因: id_framelayout2 写在 id_linearlayout2 的后面了: 注意记得写: android:layout_gravity="start" 正确: ...
- The income statement
The income statement measures performance over some period of time,usually a quarter or a year.The ...
- 设置图片自适应DIV大小
可以利用CSS样式表中表示后代的复合选择器进行设置.例: <head> <style type="text/css"> #right img /*设定box ...
- poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...
- Spring Mail
想必大家在今天这个特殊的日子里,一定热血沸腾了一把.为上午的阅兵点70个赞! 进入Spring Mail的主题: 前后大概花了8个小时的时间来研究Spring封装的javaMail.本来觉得挺简单的应 ...
- 一些实用的 jQuery 技巧
jQuery如今已经成为Web开发中最流行的JavaScript库,通过jQuery和大量的插件,你可以轻松实现各种绚丽的效果. 本文将为你介绍一些实用的技巧,希望可以帮助你更加高效地使用jQuery ...
- 大漠推荐的教程:创建你自己的AngularJS -- 第一部分 Scopes
创建你自己的AngularJS -- 第一部分 Scopes http://www.html-js.com/article/1863
- RMQ(st)
int dp[1111][12]; int a[1111]; int n; void RMQ_init() { for(int i=1;i<=n;i++) { d ...