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 ,,⋯ 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.

⋅ ≤T≤.
⋅ ≤ex,ey≤.
Output
For every test case, you should output "Case #x: y", where x indicates the case number and counts from  and y is the number of possible starting grids.
Sample Input

 
Sample Output
Case #:
Case #:
Case #:
 
Source

http://blog.csdn.net/queuelovestack/article/details/50094499  这个博客讲的很好,就直接复制了。

题意:有一只青蛙,它从起点(x,y)出发,每次它会走LCM(x,y)步[LCM(x,y)就是x,y的最小公倍数]到达点(x+LCM(x,y),y)或点(x,y+LCM(x,y)),最终,它会到达点(ex,ey),现给你终点(ex,ey),要你求出它的起点有多少种可能

解题思路:我们暂时假设x,y的最大公约数gcd(x,y)=k,那么我们不妨用来表示x,用来表示y,那么新得到的点必定是,因为x与y的最小公倍数

我们不妨求解一下新的点x和y的gcd值,以点为例

因为时互质的,也是互质的,故

所以,我们可以发现先得到的点和原来的点有相同的最大公约数,故我们可以利用这一点来根据终点求解原先的起点

还有一点需要提及的是,对于当前点(x,y),x,y中小的那个值必定是之前那个点中的x值或y值,故我们可以开始逆推了

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = ;
const int M = ;
const int inf = ;
const int mod = ;
int gcd(int x,int y)
{
if(x%y)
return gcd(y,x%y);
return y;
}
int main()
{
int t,k,c,p=,x,y;
scanf("%d",&t);
while(t--)
{
c=;
scanf("%d%d",&x,&y);
if(x>y)
swap(x,y);
k=gcd(x,y);
while(y%(x+k)==)
{
c++;
y=y/(x/k+);
if(x>y)
swap(x,y);
k=gcd(x,y);
}
printf("Case #%d: %d\n",p++,c);
}
return ;
}

hdu 5584 LCM Walk(数学推导公式,规律)的更多相关文章

  1. HDU 5584 LCM Walk 数学

    LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...

  2. HDU 5584 LCM Walk(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意:(x, y)经过一次操作可以变成(x+z, y)或(x, y+z)现在给你个点(ex, e ...

  3. HDU 5584 LCM Walk【搜索】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 分析: 这题比赛的时候卡了很久,一直在用数论的方法解决. 其实从终点往前推就可以发现, ...

  4. hdu 5584 LCM Walk

    没用运用好式子...想想其实很简单,首先应该分析,由于每次加一个LCM是大于等于其中任何一个数的,那么我LCM加在哪个数上面,那个数就是会变成大的,这样想,我们就知道,每个(x,y)对应就一种情况. ...

  5. HDU - 5584 LCM Walk (数论 GCD)

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

  6. HDU 5844 LCM Walk(数学逆推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k).现 ...

  7. [HDOJ5584]LCM Walk(数论,规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 给一个坐标(ex, ey),问是由哪几个点走过来的.走的规则是x或者y加上他们的最小公倍数lcm ...

  8. hdu 5312 Sequence(数学推导——三角形数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others)  ...

  9. acdream.LCM Challenge(数学推导)

     LCM Challenge Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. 阿里云 centos 修改iptables

    一.检查iptables服务状态 首先检查iptables服务的状态 [root@woxplife ~]# service iptables status iptables: Firewall is ...

  2. WPF GroupBox 样式分享

    原文:WPF GroupBox 样式分享 默认样式 GroupBox 样式分享" title="WPF GroupBox 样式分享"> 添加样式后 GroupBox ...

  3. linux系统监控常用工具

    linux系统监控常用工具 一.系统核心工具包(coreutils) 1./bin/df 报告系统的磁盘空间用量 df -h  显示磁盘分区fdisk -l 2./bin/uname 显示系统信息 u ...

  4. Makefile与Shell的问题

    http://blog.csdn.net/absurd/article/details/636418 Makefile与Shell的问题 大概只要知道Makefile的人,都知道Makefile可以调 ...

  5. AJAX提交到Handler.ashx一般处理程序返回json数据 (字符串拼接方式)

    <%@ WebHandler Language="C#" Class="Handler" %> using System; using System ...

  6. Hadoop CLI MiniCluster

    CLI MiniCluster Use the climonicluster, users can simply start and stop a single-node hadoop cluster ...

  7. js实现form表单提交,图片预览功能

    代码如下 <html> <head> <meta http-equiv="Content-Type" content="text/html; ...

  8. uva 10366 Faucet Flow

    题意: 给出l和r,然后从l坐标到r坐标每隔两个位置有一个档板,给出挡板的高度,然后想(-1, 1)中间加水,问什么时候会溢出. 分析: 两边先找到距离(-1,1)最近的最大值L和R.接着比较两个L和 ...

  9. tabbedApliction

    一.手动创建UITabBarController 最常见的创建UITabBarController的地方就是在application delegate中的 applicationDidFinishLa ...

  10. locate linux文件查找命令

    locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...