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 ...
随机推荐
- C++入门到理解阶段二基础篇(4)——C++运算符
目录 算术运算符(进行四则运算) 赋值运算符(表达式的值赋给变量) 比较运算符(表达是比较,返回一个真值或假值) 逻辑运算符(返回表格式的结果真或假) 位运算符 杂项运算符 C++ 中的运算符优先级 ...
- 推荐 | 中文文本标注工具Chinese-Annotator(转载)
自然语言处理的大部分任务是监督学习问题.序列标注问题如中文分词.命名实体识别,分类问题如关系识别.情感分析.意图分析等,均需要标注数据进行模型训练.深度学习大行其道的今天,基于深度学习的 NLP 模型 ...
- linux tmux用法
1. 安装工具 Centos : yum install tmux 2. 基本操作 新建会话:tmux new -s session-name 查看会话:tmux ls 进入会话:tmux a -t ...
- Zuul 1.x 的工作原理
Zuul简介 Zuul在微服务架构中,可以作为提供动态路由,监控,弹性,安全等边缘服务的框架.在Netflix,被用作所有请求到达streaming application的前门.Zuul使用一系列不 ...
- call , apply的this指向实现原理并自己实现封装
实现this指向原理 var value = 'value' var obj = { value: 'obj' } function func() { console.log(this.value) ...
- arcgis api 3.x for js 解决 textSymbol 文本换行显示(附源码下载)
前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...
- docker系列(三):docker容器
1 引言 在前面博文中,我们介绍了镜像.如果说镜像犹如面向对象中的类,本节要说的容器就是由类实例化出来的对象了,有了类才可以创建容器. 先从拉取一个官方提供的ubuntu最新镜像: $ docker ...
- Hive性能调优(二)----数据倾斜
Hive在分布式运行的时候最害怕的是数据倾斜,这是由于分布式系统的特性决定的,因为分布式系统之所以很快是由于作业平均分配给了不同的节点,不同节点同心协力,从而达到更快处理完作业的目的. Hive中数据 ...
- Troubleshooting ORA-1628 - max # extents (32765) reached for rollback segment <SEGMENT_NAME> (Doc ID 1580182.1)
Troubleshooting ORA-1628 - max # extents (32765) reached for rollback segment <SEGMENT_NAME> ( ...
- 渗透测试学习 二十、 其他漏洞汇总之PHP相关漏洞
大纲: PHP相关漏洞 JSP相关漏洞 其他漏洞汇总 PHP相关漏洞 文件包含漏洞 php://input等伪协议利用 代码执行漏洞 变量覆盖漏洞 文件包含漏洞 程序开发人员一般会把重复使用的函数写到 ...