HDU-5584-LCM Walk(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584

Description

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)!

Input

First 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.

? 1≤T≤1000.

? 1≤ex,ey≤109.

Output

For 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

题意:

给你(x,y) 然后可以走到(x+lcm(x,y),y)或者走到(x,y+lcm(x,y))

然后现在给你一个位置,问你起点有多少种。

假设x = at,y =bt ;

所以(at,bt),那么下一步就可以走到(at(1+b),bt)或者走到(at,(1+a)bt)

除一下gcd,那么我们走到了(x,y) 上一步就是 (x/(y+1),y)和(x,y/(x+1))。

//#include<bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXN 200005
long long mod = 1e9+7;
int get_gcd(int x,int y)
{
    if(!x)return y;
    return get_gcd(y%x,x);
}
int main()
{
    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;
}
时间: 2024-11-09 10:29:25

HDU-5584-LCM Walk(数学)的相关文章

hdu 5584 LCM Walk(数学推导公式,规律)

Problem Description 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

HDU - 5584 LCM Walk (数论 GCD)

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 sit

HDU 5844 LCM Walk(数学逆推)

http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k).现在给出终点坐标,求有多少个起点可以通过这种变化方式得到终点. 思路: 现在假设我们处于(x,y)这个坐标上,x和y的最大公约数为k,x和y用k来表示的话可以表示为x=$m_{1}$,y=$m_{2}$. 那么接下来可以得到($m_{1}$k,$m_{2}$k+$m_{1}$$m_{2}$k)或者 (

hdu 4710 Balls Rearrangement (数学思维)

题意:就是  把编号从0-n的小球对应放进i%a编号的盒子里,然后又买了新盒子, 现在总共有b个盒子,Bob想把球装进i%b编号的盒子里.求重置的最小花费. 每次移动的花费为y - x ,即移动前后盒子编号的差值的绝对值. 算法: 题目就是要求                  先判断  n与  lcm(a,b)的大小,每一个周期存在循环,这样把区间缩短避免重复计算. 如果n>lcm(a,b)则   ans = (n/lcm)*solve(lcm)+solve(n%lcm) 否则   ans =

HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spherical deep-sea submersible which was unpowered and lowered into the ocean on a cable, and was used to conduct a series of dives under the sea. The Bathys

HDU 4937 Lucky Number (数学,进制转换)

题目 参考自博客:http://blog.csdn.net/a601025382s/article/details/38517783 //string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last); //把[first0,last0)之间的部分替换成[first,last)之间的字符串 /* 题意: 我们将3,4,5,6认为是幸运数字.给定一个十进制数n. 现在可以讲起任意转

HDU 1018 Big Number 数学题解

Problem Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of

hdu 4823 Energy Conversion(数学)

题目链接:hdu 4823 Energy Conversion 题目大意:中文题,不解释. 解题思路:首先判断一下m是否已经大于n了,如果大于那么就是0,假设中间变换的各个值为ai,那么bi=ai+c,bi数组为等比数组(可推),所以就有了cnt=log((n+c)a)log(double(k)),结果为浮点数,需要向上取整. #include <cstdio> #include <cstring> #include <cmath> int main () { int

HDU 1005 Number Sequence (数学规律)

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 104190    Accepted Submission(s): 25232 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

HDU5584 LCM Walk 数论

LCM Walk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 47    Accepted Submission(s): 31 Problem Description A frog has just learned some number theory, and can't wait to show his ability to hi