UVa 10673 Play with Floor and Ceil(扩展欧几里得)

题意:

给定x, k,求p, q使得:

思路:

http://blog.csdn.net/fioman/article/details/2455698

利用扩展的欧几里得定理,ax + by = gcd(a, b);x, y一定有整数解。

ax1+by1=gcd(a,b)=gcd(b,a%b)=bx2+(a%b)y2=bx2+(a-(a/b)*b)y2

#include <cstdio>
#include <cstring>
#include <cstring>
#include <cmath>

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

int main()  
{  
    int cases;
    scanf("%d", &cases);
    while (cases--)
    {
        int a, b, c, k;
        scanf("%d %d", &c, &k);
        a = (int)floor(1.0*c/k);
        b = (int)ceil(1.0*c/k);
        int d, x, y;
        gcdextend(a, b, d, x, y);
        x *= (c / d);
        y *= (c / d);
        printf("%d %d\n", x, y);
    }
    return 0;
}  

 

posted @ 2012-12-03 21:46  kedebug  阅读(262)  评论(0编辑  收藏  举报