Topic Links:
http://poj.org/problem?id=1061
Main topic:
Chinese topic, test instructions at a glance, is the data range is surprisingly large.
Problem Solving Ideas:
Assuming that two frogs have skipped T-times, you can list the indefinite equation: P*l + (n-m) *t = = x-Y. After the equation is listed, the solution of the indefinite equation is calculated using the extended Euclidean. In finding the smallest solution of the whole place card for a long time, long.
To learn more about the use and proof of the extension of Euclid, you can look at the blog post of God Ox, I think the weak brain can not write so good, attached link: http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html
Code:
1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <algorithm>5#include <iostream>6#include <cmath>7#include <queue>8 using namespacestd;9 #defineLL Long LongTenll GCD (ll A, ll B, LL &x, LL &y)//Expand Euclidean templates One { A if(b = =0) - { -x =1; they =0; - returnA; - } -LL r = gcd (b, a%b, x, y), T; +t =x; -x =y; +y = t-a/b *y; A returnR; at } - intMain () - { - LL x, y, M, N, l; - while(SCANF ("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &l)! =EOF) - { in LL u, V, a, B, C; -A = N-m; tob =l; +c = gcd (A, B, U, v);//the U is gcd (A, b) = Au + BV. - if((x-y)% c! =0)//X*GCD (A, b) = A*u*x + b*v*x is established to find out the solution of the problem the { *printf ("impossible\n"); $ Continue;Panax Notoginseng } -LL s = b/C; theu = u * (x-y)/C;//this time is (x-y) = (n-m) *t + p*l The solution of T +U = (u% s + s)%s; Aprintf ("%lld\n", u); the } + return 0; -}
POJ 1061 Frog Dating (extended Euclid)