`
java-mans
  • 浏览: 11390108 次
文章分类
社区版块
存档分类
最新评论

115 - The 12th Zhejiang University Programming Contest - C

 
阅读更多

好吧,说说方法

先解一个同余方程求出 一个解 x10 , x20

由于 a*b - b*a == 0;

那么可以求出它的整数解系,

x1 = x10 + K*b;

x2 = x20 - K*a;

我们可以证明在这其中不会存在其他的解,即求出了所有的解

然后思考一下,怎样使操作的次数最少,

可以发现若 x1 > 0 && x2 > 0 的话操作的次数就是 Max(x1,x2).

这样的话问题就被简化了,

问题就变成通过 +b 和 -a 的线性变换使 x1 尽量的接近 x2

寻找到解系中最接近的 x1 和 x2,问题也就解决了

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;

#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif

#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a)         for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a)        for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define SZ(a)           ((int)a.size())
#define PP(n,m,a)       puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");}
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-10;
const double pi = acos(-1.0);

i64 gcd(i64 _a, i64 _b)
{
    if (!_a || !_b)
    {
        return max(_a, _b);
    }
    i64 _t;
    while (_t = _a % _b)
    {
        _a = _b;
        _b = _t;
    }
    return _b;
};

i64 ext_gcd (i64 _a, i64 _b, i64 &_x, i64 &_y)
{
    if (!_b)
    {
        _x = 1;
        _y = 0;
        return _a;
    }
    i64 _d = ext_gcd (_b, _a % _b, _x, _y);
    i64 _t = _x;
    _x = _y;
    _y = _t - _a / _b * _y;
    return _d;
}

i64 invmod (i64 _a, i64 _p)
{
    i64 _ans, _y;
    ext_gcd (_a, _p, _ans, _y);
    _ans < 0 ? _ans += _p : 0;
    return _ans;
}

i64 x,y,a,b;

i64 ans;


i64 get(i64 x1,i64 x2)
{
    ans = inf64;

    i64 ca = x2-x1;
    if(ca<0)
    {
        ca = -ca;
    }

    i64 temp = ca/(a+b);

    i64 tx1,tx2;

    if( x1 < x2)
    {
        tx1 = x1 + temp*b;
        tx2 = x2 - temp*a;
        if(tx1 > 0)
        {
            ans = tx2;
        }
        else
        {
            ans = -tx1 + tx2;
        }

        tx1 += b;
        tx2 -= a;

        if(tx1 > 0 && tx2>0)
        {
            ans = min(ans,max(tx1,tx2));
        }
        else
        {
            ans = min(ans,tx1 - tx2);
        }
    }
    else
    {
        tx1 = x1 - temp*b;
        tx2 = x2 + temp*a;
        if(tx2 > 0)
        {
            ans = tx1;
        }
        else
        {
            ans = tx1 - tx2;
        }

        tx1 -= b;
        tx2 += a;

        if(tx1>0 && tx2>0)
        {
            ans = min(ans,max(tx1,tx2));
        }
        else
        {
            ans = min(ans,-tx1 + tx2);
        }
    }
    return ans;
}

int T;

int main()
{
    cin>>T;
    for(int tt=1;tt<=T;tt++)
    {
        cin>>x>>y>>a>>b;
        i64 gab = gcd(a,b);

        if(a>b)
        {
            swap(a,b);
        }

        a/=gab;
        b/=gab;

        if(x==y)
        {
            cout<<"0"<<endl;
            continue;
        }

        if(x>y)
        {
            swap(x,y);
        }

        i64 txy = y - x;

        if(txy % gab != 0)
        {
            cout<<"-1"<<endl;
            continue;
        }

        txy/=gab;

        i64 x1,x2;

        x1 = invmod(a,b);

        x1 *= txy;

        x1 %= b;

        x2 = txy - a*x1;
    //  assert( x2%b == 0 )
        x2 /= b;

        cout<<get(x1,x2)<<endl;

    }
    return 0;
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics