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.
⋅ 1≤T≤1000.
⋅ 1≤ex,ey≤109.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=; i<=n; ++i) {
int ans=;
scanf("%d%d",&x,&y);
int c=get_gcd(x,y);
x/=c,y/=c;
if(x>y)swap(x,y);
while(y%(x+)==) {
ans++;
y/=(x+);
if(x>y)swap(x,y);
}
printf("Case #%d: %d\n",i,++ans);
}
return ;
}
LCM Walk HDU - 5584的更多相关文章
- L - LCM Walk HDU - 5584 (数论)
题目链接: L - LCM Walk HDU - 5584 题目大意:首先是T组测试样例,然后给你x和y,这个指的是终点.然后问你有多少个起点能走到这个x和y.每一次走的规则是(m1,m2)到(m1+ ...
- HDU 5584 LCM Walk 数学
LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...
- HDU5584 LCM Walk 数论
LCM Walk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- hdu-5584 LCM Walk(数论)
题目链接:LCM Walk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 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(数学推导公式,规律)
Problem Description A frog has just learned some number theory, and can't wait to show his ability t ...
- 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 ...
随机推荐
- day01(无用)
第一讲:1,基本理论知识 第一天内容:抽象.枯燥. 2,工具的操作: 三个工具: 2个发包工具: Jmeter.PostMan 1个抓包工具: Fiddler 3,安全测试的内容: 初级,工具的使用: ...
- 黄聪:wordpress登录后台后load-scripts.php载入缓慢
今天一个微信群里一个好友问大鸟,他的wordpess后台载入非常缓慢,缓慢到什么程度,我们看图: 这个真的是超级慢了,这类问题怎么解决呢,我们登录后台后,按下F12打开控制台,接着点击network, ...
- GO 键盘输入和打印输出
键盘输入和打印输出 一.打印输出 1.1 fmt包 fmt包实现了类似C语言printf和scanf的格式化I/O.格式化verb('verb')源自C语言但更简单. 详见官网fmt的API:http ...
- Android Studio 提高开发效率的插件
好久没有更新博客了,最近搞个listview搞得半死不活的,心累~~ 今天带来的是Android Studio插件的整理,全是我已经安装使用的,写这篇博文的目的也是因为我怕我自己给忘记怎么用(尴尬) ...
- 二维码生成 Gma.QrCodeNet (目前测试支持.net4.0及以上,但vs版本2010不可以 NuGet中搜索不到程序包)
1.添加程序包 2.生产二维码方法 #region 二维码 /// <summary> /// 生成二维码 /// </summary> /// <param name= ...
- Python中最常用的字符串方法!
字符串是字符序列.Python中内置的string类代表基于Unicode国际字符集的字符串.除了Python中常见的操作外,字符串还有一些专属于它们的附加方法.下图显示了所有这些可用的方法: Pyt ...
- UGUI Manual
以Unity 5.5 的官方文档为例 Canvas UI元素的前后顺序:SetAsFirstSibling, SetAsLastSibling, and SetSiblingIndex BasicLa ...
- 2. java 运算符
运算符 一.算术运算符 1. 四则与取模 + - * / % ++ -- (1) 单独使用++/--,前++和后++没有任何区别. (2) 混合使用,有区别 ①如果是前++,那么变量立刻马上 +1,然 ...
- RabbitMQ学习笔记(二、RabbitMQ结构)
目录: RabbitMQ几大组件 交换器类型 RabbitMQ运行流程 RabbitMQ几大组件:(与RabbitMQ第一节中AMQP一样,不细说) 1.生产者.消费者.消息 2.Broker:简单的 ...
- scrapy 爬取视频
利用FilesPipeline 下载视频 1.setting.py # 保存log信息的文件名 LOG_LEVEL = "INFO" # LOG_STDOUT = True # L ...