扩展欧几里得的应用……见算法竞赛入门经典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 扩展欧几里得好题的更多相关文章

  1. SGU 106 The Equation 扩展欧几里得应用

    Sol:线性不定方程+不等式求解 证明的去搜下别人的证明就好了...数学题. #include <algorithm> #include <cstdio> #include & ...

  2. SGU 106 The equation 扩展欧几里德

    106. The equation time limit per test: 0.25 sec. memory limit per test: 4096 KB There is an equation ...

  3. Codeforces7C 扩展欧几里得

    Line Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...

  4. 51nod1256 乘法逆元【扩展欧几里得】

    给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的. Input 输入2个数M, N中间用 ...

  5. [poj1061]青蛙的约会<扩展欧几里得>

    题目链接:http://poj.org/problem?id=1061 其实欧几里得我一直都知道,只是扩展欧几里得有点蒙,所以写了一道扩展欧几里得裸题. 欧几里得算法就是辗转相除法,求两个数的最大公约 ...

  6. SGU 140 扩展欧几里得

    题目大意: 给定序列a[] , p , b 希望找到一个序列 x[] , 使a1*x1 + a2*x2 + ... + an*xn = b (mod p) 这里很容易写成 a1*x1 + a2*x2 ...

  7. poj 2891 扩展欧几里得迭代解同余方程组

    Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...

  8. [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 ...

  9. 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个点,求射 ...

随机推荐

  1. 【Binary Tree Inorder Traversal】cpp

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  2. share干什么的

    share到底干什么的 //--------------------打开GameServer,share中加载------------------------- .加载nBodyID //玩家的nBo ...

  3. 如何使用CSL(翻译总结自TI官方文档)

    为了使用CSL来进行编译和连接,必须先配置CCS开发环境. 1.指定目标设备 Project/options/complier/preprocessor,在define symbols中输入设备支持符 ...

  4. 【Asp.Net WebFrom】分页

    Asp.Net WebForm 分页 一. 前言 Asp.Net WebForm 内置的DataPager让人十分蛋疼 本文使用的分页控件是第三方分页控件 AspNetPager,下载地址: 链接: ...

  5. iOS7 状态栏 修改为白色字体的步骤

    1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的ViewController中在ViewDidLoad方 ...

  6. PHP-Java-Bridge的使用(平安银行支付功能专版)

    去年做平安银行的时候,用到了PHP-Java-Bridge,后来写了一篇博客记录使用PHP-Java-Bridge的一些心得(连接:http://my.oschina.net/kenblog/blog ...

  7. C#&java重学笔记(变量与操作符)

    声明:自用参看读物 C#部分 1.C#中的byte和sbyte并不是二进制的比特,而是无符号 和 有符号的 8位整数. 2.decimal和float double都用来表示小数,前者用e的10的几次 ...

  8. poj 2723

    Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7295   Accepted: 2778 Des ...

  9. POJ 1573

    #include<iostream> #include<stdio.h> #define MAXN 15 using namespace std; char _m[MAXN][ ...

  10. POJ 3177 Redundant Paths(Tarjan)

    题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf ...