LCM Walk HDU - 5584

Posted cautx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LCM Walk HDU - 5584相关的知识,希望对你有一定的参考价值。

A frog has just learned some number theory, and can‘t wait to show his ability to his girlfriend.

Now the frog is sitting on a grid map of infinite rows and columns. Rows are numbered 1,2,? from the bottom, so are the columns. At first the frog is sitting at grid (sx,sy), and begins his journey.

To show his girlfriend his talents in math, he uses a special way of jump. If currently the frog is at the grid (x,y), first of all, he will find the minimum z that can be divided by both x and y, and jump exactly z steps to the up, or to the right. So the next possible grid will be (x+z,y), or (x,y+z).

After a finite number of steps (perhaps zero), he finally finishes at grid (ex,ey). However, he is too tired and he forgets the position of his starting grid!

It will be too stupid to check each grid one by one, so please tell the frog the number of possible starting grids that can reach (ex,ey)

!
InputFirst line contains an integer T, which indicates the number of test cases.

Every test case contains two integers ex and ey, which is the destination grid.

1T1000.
1ex,ey109.OutputFor every test case, you should output " Case #x: y", where x indicates the case number and counts from 1 and y is the number of possible starting grids.
Sample Input
3
6 10
6 8
2 8
Sample Output
Case #1: 1
Case #2: 2
Case #3: 3

OJ-ID:
hdu-5584

author:
Caution_X

date of submission:
20191021

tags:
math

description modelling:
青蛙跳,每次移动从(x,y)->(x,y+lcm(x,y))或(x,y)->(x+lcm(x,y),y)

major steps to solve it:
设当前位置(at,bt),则下一步为(at(1+b),bt)或(at,bt(1+a))
那么反过来推,可以得到当前步(at,bt),则上一步为(at,bt/(a+1))或(at/(1+b),bt)
以此类推直到b无法被(1+a)整除或者a无法被(1+b)整除

AC code:

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int get_gcd(int x,int y)
{
    if(!x)return y;
    return get_gcd(y%x,x);
}
int main()
{
    //freopen("input.txt","r",stdin);
    int n,x,y;
    scanf("%d",&n);
    for(int i=1; i<=n; ++i) {
        int ans=0;
        scanf("%d%d",&x,&y);
        int c=get_gcd(x,y);
        x/=c,y/=c;
        if(x>y)swap(x,y);
        while(y%(x+1)==0) {
            ans++;
            y/=(x+1);
            if(x>y)swap(x,y);
        }
        printf("Case #%d: %d
",i,++ans);
    }
    return 0;
}

 

以上是关于LCM Walk HDU - 5584的主要内容,如果未能解决你的问题,请参考以下文章

HDU - 5584 LCM Walk (数论 GCD)

hdu 5584 LCM Walk

HDU 5844 LCM Walk(数学逆推)

HDU 5407 CRB and Candies(LCM +最大素因子求逆元)

[HDU 5382] GCD?LCM!

[HDU5382]GCD?LCM!