UESTC 288 frog appointment extension GCD

Source: Internet
Author: User

If two frogs skip step t, the coordinates of a are x + MT and B: Y + nt. To make them at the same point, the following conditions must be met: x + MT-(Y + nt) = kl (P is an integer)

Convert to: (n-m) T + KL = x-y (L> 0), then it becomes to solve the same-division equation: (n-m) T transform (x-y) mod l, solved With the extension GCD. At this time, the solution is available only when (x-y) % gcd (n-M, L) = 0.

If we have obtained a pair of x0 and Y0 for solving the homogeneous equation AX + by = m, then X0 = x * m/gcd (a, B). x0 may not be a positive integer at this time, it is not necessarily the smallest positive integer solution, so further processing is required.

Make G = gcd (A, B), for a * x0 + B * Y0 = D, there are (A/G) * x0 + (B/g) * Y0 = D/g Deformation: (A/G) (x0 + K * (B/G) + (B/g) (y0-K * (A/G) = D/g is still true. All solutions can be found based on the value of K. Therefore, X = x0 + K (B/g), so that B/g = t, x = x0 + KT, so you can use X = (x0% T + T) % t returns the minimum positive integer x.

TIPS: to prevent gcd (n-M, L) from becoming negative, first determine the positive and negative of N-M. If it is negative, then n-M is reversed to m-N, if x-y is reversed to Y-X, the correct result can still be obtained.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#define Mod 1000000007#define SMod 10007#define lll __int64#define ll long longusing namespace std;#define N 1000007ll exgcd(ll a,ll b,ll &x,ll &y){    if(!b)    {        x = (ll)1,y = (ll)0;        return a;    }    ll r = exgcd(b,a%b,x,y);    ll t = x;    x = y;    y = t - a/b*y;    return r;}ll gcd(ll a,ll b){    if(!b)        return a;    return gcd(b,a%b);}int main(){    ll x,y,m,n,L;    ll kx,ky;    //freopen("1.txt","r",stdin);    //freopen("2.txt","w",stdout);    while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L)!=EOF)    {        ll nm = n-m;        ll delta = x-y;        if(nm < 0)        {            nm = m-n;            delta = y-x;        }        ll d = exgcd(nm,L,kx,ky);        if(delta%d)        {            puts("Impossible");            continue;        }        ll t = L/d;        //printf("%lld\n",t);        ll res = (((delta/d)*kx)%t + t)%t;        printf("%lld\n",res);    }    return 0;}
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.