Poj 1061 frog troubles

Source: Internet
Author: User

There is nothing to say. Find out the formula, first find a group of special solutions, and finally obtain the desired solutions...

Train of Thought: Set up a frog to jump X times before meeting, get the equation (a + XM) % L = (B + Xn) % L to release X (m-N) + yl = B-. according to the Extended Euclidean algorithm, a group of solutions of X (m-n) + yl = gcd (m-N, L) can be obtained,

When (B-A) is a multiple of gcd (m-N, L), the original equation has an integer solution. Now we need to find the minimum number of beats X. The idea is referring to comeon4mydream's ~~ Materials ~~

To minimize the number of beats X, simply put, it is achieved through the general solution x = X1 + K * l/gcd (m-N, L ).Calculate the number of the first return values greater than 0.This number is increased in units of L/gcd (m-N, L.

# Include <iostream>

Using namespace STD;
_ Int64 gcd (_ int64 A ,__ int64 B ){
If (B = 0) return;
Return gcd (B, A % B );
}
_ Int64 kzgcd (_ int64 A ,__ int64 B ,__ int64 & X ,__ int64 & Y ){
If (B = 0) {x = 1, y = 0; return ;}
Int res = kzgcd (B, A % B, Y, X );
Y-= A/B * X;
Return res;
}
Int main (){
_ Int64 X1, Y1, m, n, l;
While (CIN> x1> Y1> m> N> L ){
_ Int64 c = x1-y1;
_ Int64 A = N-m;
_ Int64 B = L;
_ Int64 x = 0, y = 0;
_ Int64 flag = gcd (A, B );
If (n = M | C % flag) {cout <"impossible" <Endl; continue ;}
A/= Flag, B/= Flag, C/= flag;
Kzgcd (a, B, x, y );
X * = C; // X is the exclusive solution of AX + by = 1. Now it must be converted to the exclusive solution of AX + by = C.
X % = B; // returns the minimum positive integer.
While (x <0) x + = B;
Cout <x <Endl;
}
}

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.