Poj1061-frog Dating---extended euclidean algorithm for minimum integer solution

Source: Internet
Author: User
Tags gcd

Extended Euclidean algorithm template

#include <cstdio>#include<cstring>#definell Long Longusing namespaceStd;ll EXTEND_GCD (ll A, ll B, LL&x, LL &y) {    if(b = =0) {x=1, y =0; returnA; }    Else{ll R= EXTEND_GCD (b, a%b, y, x); Y-= x* (A/b); returnR; }}

1. For equations such as a*x0 + B*y0 = N, in order to solve the x0 and y0, the X and y of a*x + b*y = gcd (A, b) can be obtained by extending Euclid first.

2. Easy to obtain, if (x-y)%gcd (A, B) ==0, then the indefinite equation has an integer solution, otherwise there is no qualified integer solution.

3. After obtaining x and Y, it is possible to find the x0 by x0 = X*N/GCD (A, B) , which is the key to the X0 .

4. In the actual problem, we often need the smallest integer solution, we can find the smallest integer solution by the following method:

Make t = B/GCD (A, B), X is a special solution of equation a*x + b*y = N, then xmin = (x% t + t)% t

The date of the frog
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 113227 Accepted: 23091

Description

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.

Input

The input includes only one line of 5 integer x,y,m,n,l, where X≠y < 2000000000,0 < M, n < 2000000000,0 < L < 2100000000.

Output

Output the number of hops required to meet, and output a line of "impossible" if it is never possible to meet

Sample Input

1 2 3) 4 5

Sample Output

4

Analysis:
when two frogs jump T-step, the coordinates of a are x+mt-p1l (P1∈z and X+mt-p1l<l), the coordinates of B are y+nt-p2l (P2∈z and Y+nt-p2l<l), and the sufficient and necessary condition for A and B encounters is x+mt-p1l = y+ nt-p2l. Finishing available (x-y) + (m-n) T = (P1-P2) l, i.e. (n-m) T + (P1-P2) L = x-ySet P = p1-p2 Finishing (n-m) * t + L * p = x-ySee a * x + b * y = gcd (A, b) look like?  Call EXTEND_GCD (n-m, L, T, p) to calculate the GCD (N-m, L), T, p. Then the minimum integer solution can be calculated using the method above.
#include"Cstdio"#include"iostream"using namespacestd;#defineLL Long Longll EXTGCD (ll A,ll b,ll&x,ll&y)///Templates{    if(b==0) {x=1; y=0; returnA; } LL ans=EXTGCD (b,a%b,y,x); Y-=a/b*x; returnans;}intMain () {LL n,m,t,l,x,y,p;  while(~SCANF ("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l)) {LL ans=EXTGCD (nm,l,t,p); if((x-y)%ans)            {///1. printf ("impossible\n"); }        Else        {            ///an algorithm for finding the smallest integer solutiont= (x-y)/ans*t;///first, make X a special solution 2.LL temp= (l/ans); T= (t%temp+temp)%temp;///then the formula is calculated by 3.printf"%lld\n", T); }    }}

Summary: For this type of problem,
What we need to do is, 1. Understand Formula memorize formula
2. Absorb this great power from mathematics

Poj1061-frog Dating---extended euclidean algorithm for minimum integer solution

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.