HDU-4596 Yet another end of the world exgcd property

Keywords: PHP


Title Link
Title:

Given some groups of x, y, z, we ask if we can find an integer d so that at least two groups of x, y, Z satisfy y < d%x < Z.

Train of thought:

First of all, you can think of finding two groups directly.
Assuming that the integer is W, there are:
Wmod(x1)=c1
Wmod(x2)=c2
Can get
W=k1∗x1+c1
W=k2 x2+c2
k1 X1 k2 x2=c2 c1, if found, then the above formula needs to be understood. Obviously, this is an extended Euclidean a x+b y=c, and an extended Euclidean solution condition c% GCD (a, b) = 0. Then the subject requires (c2 c1) mod (gcd (x1, x2) = 0. Because c1c2 is an interval range, we can determine the maximum and minimum values of both and then satisfy max/gcd-(min-1)/gcd >= 1.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e3+5;
struct node
{
    ll x, y, z;
}a[maxn];
int n;

int main(void)
{
    while(~scanf("%d", &n))
    {
        for(int i = 1; i <= n; i++)
            scanf("%lld%lld%lld", &a[i].x, &a[i].y, &a[i].z);
        bool ok = 0;
        for(int i = 1; i <= n; i++)
            for(int j = i+1; j <= n; j++)
            {
                ll gcd = __gcd(a[i].x, a[j].x);
                if(gcd == 1) ok = 1;
                else
                {
                    ll Min = min(abs(a[i].y-a[j].y), abs(a[i].y-a[j].z));
                    Min = min(Min, min(abs(a[i].z-a[j].y), abs(a[i].z-a[j].z)));

                    ll Max = max(abs(a[i].y-a[j].y), abs(a[i].y-a[j].z));
                    Max = max(Max, max(abs(a[i].z-a[j].y), abs(a[i].z-a[j].z)));
                    if(Max/gcd-(Min-1)/gcd >= 1) ok = 1;
                    if(!((a[i].z < a[j].y)||(a[i].y > a[j].z))) ok = 1;
//                    if(a[i].x == a[j].x )
//                    cout << Max << ' ' << Min <<endl;
                }
            }
        puts(ok ? "Cannot Take off" : "Can Take off");
    }
    return 0;
}

Posted by morphy on Mon, 11 Feb 2019 16:21:18 -0800