POJ 1061 Frog Dating (extended Euclid)

Source: Internet
Author: User

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)

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.