hdu 5584 LCM Walk(数学推导公式,规律)
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)!
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≤.
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.
Case #:
Case #:
Case #:
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(数学推导公式,规律)的更多相关文章
- HDU 5584 LCM Walk 数学
LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...
- HDU 5584 LCM Walk(数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意:(x, y)经过一次操作可以变成(x+z, y)或(x, y+z)现在给你个点(ex, e ...
- HDU 5584 LCM Walk【搜索】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 分析: 这题比赛的时候卡了很久,一直在用数论的方法解决. 其实从终点往前推就可以发现, ...
- hdu 5584 LCM Walk
没用运用好式子...想想其实很简单,首先应该分析,由于每次加一个LCM是大于等于其中任何一个数的,那么我LCM加在哪个数上面,那个数就是会变成大的,这样想,我们就知道,每个(x,y)对应就一种情况. ...
- 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 ...
- HDU 5844 LCM Walk(数学逆推)
http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k).现 ...
- [HDOJ5584]LCM Walk(数论,规律)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 给一个坐标(ex, ey),问是由哪几个点走过来的.走的规则是x或者y加上他们的最小公倍数lcm ...
- hdu 5312 Sequence(数学推导——三角形数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others) ...
- acdream.LCM Challenge(数学推导)
LCM Challenge Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- 类加载器子系统——JVM之四
一.类加载器基本概念 顾名思义,类加载器(class loader)用来加载 Java 类到 Java 虚拟机中.一般来说,Java 虚拟机使用 Java 类的方式如下:Java 源程序(.java ...
- 无法关闭的QT程序——思路开阔一下,原来这么简单!
做一个无法关闭的QT程序(想关闭时要在任务管理器里关闭),看似很难, 其实它并不难,只要让程序在关闭时启动它自身就可以了. 上代码: #include <QtGui> class Temp ...
- thinkPHP模板引擎案例
1.if <if condition="$vo.business eq LS"> 零售 <elseif condition="$vo.business ...
- intro
懒得自己折腾wordpress又很想写博客. 作为一名把自己当成programmer的data scientist,毅然选择了博客园. 这里我想内容就是平时学到/使用的各种心得,更新频率不定. 兴趣范 ...
- 顶尖数据挖掘教学案例库(TipDM-C10)产品白皮书
顶尖数据挖掘教学案例库 (TipDM-C10) 产 品 说 明 书 广州泰迪智能科技有限公司 版权所有 地址: 广州市经济技术开发区科学城232号 网址: ht ...
- asp.net中,我们使用ashx获取数据列表,在前端使用$.ajax()解析
一直在想在asp.net中怎么才能向在java中那样用struts那样做页面请求. 当然asp.net mvc就是类似struts的东西吧,不过还没来得及学习. 今天就用ashx来接收页面请求,并调用 ...
- C# 中的关键字之:base、this 【转】
C# 中的关键字之:base.this. ba ...
- swift Dictionary 字典
// // main.swift // 字典 // // Created by zhangbiao on 14-6-15. // Copyright (c) 2014年 理想. All rig ...
- JS函数的四种调用模式
函数在js中具有四种身份,分别为函数.方法.构造函数.apply或call调用 函数调用 函数调用模式中this指全局对象(window) var f1 = function() { alert ...
- DropdownList的处理总结
创建一: List<SelectListItem> items = new List<SelectListItem>() { new SelectListItem(){Text ...