POJ-1061 frog Dating---extended euclidean algorithm

Source: Internet
Author: User
Tags gcd

Topic Links:

https://cn.vjudge.net/problem/POJ-1061

Main topic:

Two of the frogs met on the internet, and they chatted very happily, so they felt it was necessary to see one side. They were pleased to find that they lived on the same latitude line, so they agreed to jump westward until they met. But they forget a very important thing before they set out, neither to ask clearly the characteristics of each other, nor to meet the specific location. But the frogs are very optimistic, they feel as long as they have to jump in some direction, always meet each other. But unless the two frogs jump to the same point at the same time, they will never be able to meet. To help these two optimistic frogs, you are asked to write a procedure to determine if the two frogs will meet and when.
We have these two frogs are called Frog A and Frog B, and the latitude line on the longitude of 0 degrees at the origin, from east to West for the positive direction, the unit length of 1 meters, so that we get a first-to-last line. The starting point of setting Frog A is x, and Frog B's starting point coordinates are Y. Frog A can jump M m at a time, Frog B can jump n meters at a time, and two frogs will spend the same time jumping once. Latitude line total length l m. Now you have to find out how many times they have jumped before they meet.

Problem Solving Ideas:

Suppose T moment meets

With: (x + mt) = (y + nt) (mod L)

i.e. X + mt-y-NT = K * L

(m-n) * t + K * L = y-x

(because K takes an arbitrary integer, moving to the left end of the equation can be regardless of his positive or negative, anyway to find out the K does not handle)

That is:A * t + b * K = y-x A = m-n B = L solve T and K

When a = 0 is found, x = y solution is 0 and the inverse is no solution.

When a < 0 o'clock, mark A, then a positive number, the last to find the T and then take the opposite numbers

Solve for ax + by = C

Solve ax + by = gcd (A, b) first to find out x0,y0

If GCD (x, y) is divisible by C then there is a solution, but no solution

according to X0 y0 can get a solution:

X1 = x0 * C/GCD (A, B)

Y1 = y0 * C/GCD (A, B)

All solutions:

x = x1 + B/GCD (A, b) * t

y = y1 + a/gcd (A, b) * t

Minimum solution of Positive x

B ' = B/GCD (A, B)

A ' = A/GCD (A, B)

x2 = ((x1% B ') + B ')% B '

Y2 is based on X2.

In the same vein, the solution of the minimum positive number y is calculated

1#include <iostream>2#include <cstdio>3#include <cmath>4 using namespacestd;5typedefLong Longll;6ll EXTGCD (ll A, ll B, ll&x, ll&y)//Ax+by = Solution of gcd (A, B). The return value is GCD7 {8ll d =A;9     if(b)Ten     { OneD = EXTGCD (b, a%b, y, x); AY-= (A/b) *x; -     } -     Elsex =1, y =0; the     returnD; - } - intMain () - { + ll x, y, M, N, l; -Cin >> x >> y >> m >> n >>l; + ll xx, yy; All a = M-n, B =l; at     if(A = =0) -     { -         if(x = = y) cout<<"0"<<Endl; -         Elsecout<<"Impossible"<<Endl; -         return 0; -     } in     BOOLFlag =0; -     if(A <0) A =-A, flag =1; toll g =EXTGCD (A, B, xx, yy); +     if((y-x)% g) cout<<"Impossible"<<Endl; -     Else the     { *xx = xx * (y-x)/G; $         if(flag) XX =-xx;Panax NotoginsengL = L/G; -xx = ((xx% L) + L)%l; thecout<<xx<<Endl; +     } A     return 0; the}

POJ-1061 frog Dating---extended euclidean algorithm

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.