poj 1061 + poj 2115 + poj 2142 (exgcd)

poj 1061:

题意:两只青蛙,在地球的同一纬度上,各自相对起点的初始坐标为x,y,现在他们从起点开始向同一个方向开始跳,第一只每次可以跳m,第二只可以跳n。

         给这个圆的长度l,问最小第几次时,两只青蛙能相遇。

        

解析:1. 设当t次跳跃时两娃相遇,则第一只青蛙的坐标为:x + m * t,第二只为:y + n * t 。

              所以当他们相遇时: (x + m * t) - (y + n * t ) = k * l  (0 <= k && k为整数)(因为整个路径是圆,等价与坐标相同,即距离为0)。

              整理得:( n - m ) * t  + k * l = x - y.

              即:          a * x   + b * y = c.    其中 a -> (n - m)   ,   x -> t,     b -> k,       y -> l,          c -> (x - y) .

          2. 用拓展gcd求出当 a * x + b * y = gcd(a, b) 时的一个解与d = gcd(a,b)

          3. 当c是d的倍数时,原方程的一组解为(x*c/d, y*c/d). 当c不是d的倍数时无解

          4. 方程ax≡b (mod n)的最小整数解为:( x % r + r ) % r.   r = b / d.


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long

using namespace std;
const int maxn = 1e6;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = 4 * atan(1.0);
const double ee = exp(1.0);

void exgcd(LL a, LL b, LL& d, LL& x, LL& y)//ax + by = d, d = gcd(a, b)
{
    if (b == 0)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        exgcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    LL x, y, m, n, l;
    while (scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &l) !=  EOF)
    {
        LL a = n - m;
        LL b = l;
        LL c = x - y;
        LL X, Y, D;
        exgcd(a, b, D, X, Y);
        if (c % D != 0)
        {
            printf("Impossible\n");
        }
        else
        {
            LL r = b / D;
            X = c / D * X;
            X = (X % r + r) % r;
            printf("%lld\n", X);
        }
    }
    return 0;
}


poj 2115 解法同上,不同的就是几个参数。


poj 2142:

题意:

给a, b, c, 求:

ax + by = c

的解x,y,要求x + y最小以及|ax| + |by|最小。


解析:

因为题目保证有解,所以先将整个方程两边同时除以(a,b),这样可以保证求解出的个数x和y是最小的。

在保证秤砣的个数时最小之后,只要保证和最小就行了,和最小就在两种方案间产生,一种是最小的a×x,一种时最小的b×y。

见代码。


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long

using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = 4 * atan(1.0);
const double ee = exp(1.0);

const int maxn = 30 + 10;

LL gcd(LL a, LL b)
{
    return b ? gcd(b, a % b) : a;
}

void exgcd(LL a, LL b, LL& d, LL& x, LL& y)
{
    if (b == 0)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        exgcd(b, a % b, d, y, x);
        y -= a / b * x;
    }
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    LL a, b, c;
    while (scanf("%lld%lld%lld", &a, &b, &c) && a && b && c)
    {
        ///保证个数最小
        LL d = gcd(a, b);
        a /= d;
        b /= d;
        c /= d;

        LL x, y;
        exgcd(a, b, d, x, y);

        LL x1, y1, x2, y2;
        ///计算两种情况
        ///x 最小时
        x1 = x * c;///   x' * c / d
        x1 = (x1 % b + b) % b;
        y1 = (c - a * x1) / b;
        ///y 最小时
        y2 = y * c;
        y2 = (y2 % a + a) % a;
        x2 = (c - b * y2) / a;

        if (abs(x1 + 0.0) + abs(y1 + 0.0) < abs(x2 + 0.0) + abs(y2 + 0.0))
            x = abs(x1 + 0.0), y = abs(y1 + 0.0);
        else
            x = abs(x2 + 0.0), y = abs(y2 + 0.0);
        printf("%lld %lld\n", x, y);
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值