The Balance(exgcd+三分)

 

题目传送门

题解大佬文献

三分参考

好吧,如你所见我是巨人肩膀上的大懒汉。


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define int long long
#define endl "\n"
#define yes cout<<"Yes\n";
#define no cout<<"No\n";
using namespace std;
const int M=1e6+10;
const int inf=0x3f3f3f3f;
typedef pair<int,int> P;

int tj=1000000;
int n;

int L;
int k;
int a,b,d;
int gcd(int x,int y)
{
    return y%x==0?x:gcd(y%x,x);
}
int qpow(int n)
{
    int x=2;
    int ans=1;
    while(n)
    {
        if(n&1) ans*=x;
        x*=x; n>>=1;
    }
    return ans;
}
int exGcd(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    int g=exGcd(b,a%b,x,y);
    int t=x;
    x=y;
    y=t-(a/b)*y;
    return g;
}
void sol()
{

    int x,y;
    int g=exGcd(a,b,x,y);
    a/=g,b/=g,d/=g;
    x*=d,y*=d;
    int l=-1e5;
    int r=1e5;
    int ansx,ansy;
    while(l<=r)
    {
        int m1=l+(r-l)/3;
        int m2=r-(r-l)/3;
        int x1=abs(x-b*m1),y1=abs(y+a*m1);
        int x2=abs(x-b*m2),y2=abs(y+a*m2);
        if(x1+y1<x2+y2 || (x1+y1==x2+y2 && a*x1+b*y1<=a*x2+b*y2))
        {
            r=m2-1;
            ansx=x1,ansy=y1;
        }
        else
        {
            l=m1+1;
            ansx=x2,ansy=y2;
        }
    }
    cout<<ansx<<" "<<ansy<<endl;
    return ;
}
signed main()
{
    ios;
    while(cin>>a>>b>>d,a,b,d)
    {
        sol();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

活在当下 北乔

不爱多bb,朴实一句话:感谢您

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值