[HDOJ5584] LCM Walk (number theory, regularity)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5584

Give a coordinate (ex, EY) and ask what points it came from. The rules for walking are X or y plus their least common multiple LCM (x, y).

Consider (ex, EY) is coming from other points, it is advisable to set when walking to (x, y), GCD, =K,X=K*M1, y=k*m2.

The next step may be (x, Y+X*Y/GCD (x, y)) or (X+X*Y/GCD (y), y).

denoted by K and m1,m2 (k*m1, k*m2+m1*m2*k) = (k*m1, k*m2* (m1+1)), or (K*m1+m1*m2*k, k*m2) = (k*m1* (m2+1), k*m2).

The following is only concerned (k*m1* (m2+1), k*m2). M1 and M2 coprime and (m2+1) with M2 also coprime.

Then GCD (k*m1* (m2+1), k*m2) =k, that is, the largest common factor of any change, is K. This also proves that the path is unique, and we are asking for the number of possible starting positions, so consider directly the case of X to add each time (i.e., y>x, swap x, y). We can push back from (ex, EY). X ' =k1*m1/(m2+1), when x is not an integer multiple of (y + K), that is, x/(k* (m2+1)) does not end with 0.

1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> +  - using namespacestd; +  A intex, EY; at  - intMain () { -     //freopen ("in", "R", stdin); -     intT, _ =1; -scanf"%d", &T); -      while(t--) { inscanf"%d%d", &ex, &ey); -         intK =__GCD (ex, EY); to         if(Ex <ey) Swap (ex, EY); +         intAns =1; -          while(1) { the             if(ex% (ey + k)! =0) Break; *ans++; $ex = EX/(ey/k +1);Panax Notoginseng             if(Ex <ey) Swap (ex, EY); -K =__GCD (ex, EY); the         } +printf"Case #%d:%d\n", _++, ans); A     } the     return 0; +}

[HDOJ5584] LCM Walk (number theory, regularity)

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.