记录.
#include<bits/stdc++.h>
using namespace std;
struct node{int a, b, c;};
bool operator == (const node &a, const node &b){return a.a == b.a && a.b == b.b && a.c == b.c;}
bool operator != (const node &a, const node &b){return !(a==b);}
void srt(node &x)
{
if(x.a>x.b) swap(x.a,x.b);
if(x.b>x.c) swap(x.b,x.c);
if(x.a>x.b) swap(x.a,x.b);
}
pair<node,int> jump(node x)
{
srt(x);
int a = x.b - x.a, b = x.c - x.b, st = 0;
if(a<b)
{
while(x.b+a<x.c) x.a += a, x.b += a, st++;
// int c = x.a % a;
// int d = (x.c - c + a - 1) / a - 1;
// int e = c + d * a, bf = x.b;
// x.b = e, x.a = e - a;
// return make_pair(x,(e-bf)/a);
}
else
{
while(x.b-b>x.a) x.c -= b, x.b -= b, st++;
// int c = x.c % b;
// int d = (x.a - c) / b + 1;//注意!不能撞上去
// if(x.a-c<0&&(x.a-c)%b) d--;
// int e = c + d * b, bf = x.b;
// x.b = e, x.c = e + b;
// return make_pair(x,(bf-e)/b);
}
return make_pair(x,st);
}
pair<node,int> fafa(node x)
{
srt(x);
int st = 0;
while(x.a+x.c!=x.b*2)
{
pair<node,int> y = jump(x);
x = y.first, st += y.second;
}
return make_pair(x,st);
}
node fa(node x, int st)
{
srt(x);
while(st)
{
int a = x.b - x.a, b = x.c - x.b;
if(a<b) if(x.b+a*st<x.c)
{
x.a += a * st, x.b += a * st;
return x;
}
if(a>b) if(x.b-b*st>x.a)
{
x.b -= b * st, x.c -= b * st;
return x;
}
pair<node,int> y = jump(x);
x = y.first, st -= y.second;
}
return x;
}
int main()
{
node a, b;
cin>>a.a>>a.b>>a.c>>b.a>>b.b>>b.c;
srt(a), srt(b);
pair<node,int> aa = fafa(a);
pair<node,int> bb = fafa(b);
node ta = aa.first, tb = bb.first;
if(ta!=tb) return cout<<"NO", 0;
int da = aa.second, db = bb.second, ans = 0;
ans += abs(da-db);
if(da>db) a = fa(a,da-db), da = db;
else b = fa(b,db-da), db = da;
int l = 0, r = da, mid;
while(l < r)
{
mid = (l + r) >> 1;
if(fa(a,mid)==fa(b,mid)) r = mid;
else l = mid + 1;
}
ans += l * 2;
cout<<"YES\n"<<ans;
return 0;
}
本人直接暴力跳,并未使用辗转相减的方法,所以对于以下数据复杂度应为 3⋅1010(=109⋅log2n),但是在 Dev-c++ 上吸氧 0.9s 就过了。。。