zoukankan      html  css  js  c++  java
  • UESTC 1722

     1 #include <iostream>
     2 #include <string.h>
     3 #include <string>
     4 #include <fstream>
     5 #include <algorithm>
     6 #include <stdio.h>
     7 #include <vector>
     8 #include <queue>
     9 #include <set>
    10 #include <cmath>
    11 using namespace std;
    12 const double eps = 1e-8;
    13 const double pi=acos(-1.0);
    14 const int INF=0x7fffffff;
    15 unsigned long long uINF = ~0LL;
    16 #define MAXN 1007
    17 #define mod 1000000007
    18 typedef long long LL;
    19 LL gcd(LL a,LL b)
    20 {
    21     return b==0?a:gcd(b,a%b);
    22 }
    23 //ax+by=d;
    24 void gcd(LL a,LL b,LL& d,LL& x,LL& y)
    25 {
    26     if(!b){d=a;x=1;y=0;}
    27     else {gcd(b,a%b,d,y,x);y-=x*(a/b);}
    28 }
    29 LL a,b,A,B,c,d,x,y;
    30 LL Find(LL i,LL j)
    31 {
    32     LL temp;
    33     if(i>=0&&j>=0)temp=max(i,j);
    34     else if(i<=0&&j<=0)temp=-min(i,j);
    35     else if(i>j)temp=i-j;
    36     else temp=j-i;
    37     //cout<<temp<<endl;
    38     if(temp)return temp;
    39     return INF;
    40 }
    41 int main()
    42 {
    43     int T;
    44     //freopen("0.in","r",stdin);
    45     scanf("%d",&T);
    46     while(T--)
    47     {
    48         scanf("%lld%lld%lld%lld",&A,&B,&a,&b);
    49         if(A>B)swap(A,B);
    50         c=B-A;
    51         gcd(a,b,d,x,y);
    52         if(c%d!=0)printf("-1
    ");
    53         else if(c==0)printf("0
    ");
    54         else
    55         {   x=x*(c/d);y=y*(c/d);
    56             a/=d;b/=d;c/=d;
    57             //cout<<x<<' '<<y<<endl;
    58             LL ans=INF;
    59             LL temp=c/(a+b);
    60             LL x1,y1,k1;
    61             k1=(temp-x)/b;y1=y+k1*a;x1=x-k1*b;
    62             //cout<<x1<<' '<<y1<<endl;
    63             LL x2,y2,k2;
    64             y2=y-k1*a;x2=x+k1*b;
    65             //cout<<x2<<' '<<y2<<endl;
    66 
    67             for(int i=0;i<=10;i++)
    68             {
    69                 ans=min(ans,Find(x1-b*i,y1+a*i));
    70                 ans=min(ans,Find(x2-b*i,y2+a*i));
    71                 ans=min(ans,Find(x1+b*i,y1-a*i));
    72                 ans=min(ans,Find(x2+b*i,y2-a*i));
    73             }
    74 
    75             printf("%lld
    ",ans);
    76         }
    77     }
    78 
    79     return 0;
    80 }

    扩展gcd

  • 相关阅读:
    1、1、2、3、5、8、13、21、34...... 求第X位数是多少,2种实现方式
    数据库设计的三大范式
    Parse与TryParse的区别
    常用Lambda范例
    【转载】说说JSON和JSONP,
    Lambda表达式
    Winform部署时强名称签名mshtml.dll无效的解决方案
    .net面试问答(大汇总)
    Asp.net MVC3 一语道破
    在asp.net中使用 log4net
  • 原文地址:https://www.cnblogs.com/TO-Asia/p/3223884.html
Copyright © 2011-2022 走看看