码迷,mamicode.com
首页 > 其他好文 > 详细

LCM Walk HDU - 5584

时间:2019-10-21 22:16:11      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:lease   cas   sub   ble   init   clip   HERE   space   stream   

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\n",i,++ans);
    }
    return 0;
}

 

LCM Walk HDU - 5584

标签:lease   cas   sub   ble   init   clip   HERE   space   stream   

原文地址:https://www.cnblogs.com/cautx/p/11716611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!