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 ...
随机推荐
- JAVA笔记1-00
package chapter1; public class Demo1 { public static void main(String[] args) { System.out.println(& ...
- NFC的安全性
NFC近距离无线技术具有快捷.易用.安全等特性,其中安全是支付行业最关心的特性. 众所周知传统磁条银行卡的账号信息,是记录在磁条中,当进行刷卡交易时,POS机将交易金额,银行卡中的账号信息以及用户输入 ...
- Inno Setup 安装inf文件的一个例子
原文 http://zwkufo.blog.163.com/blog/static/2588251201063033524889/ ; INF安装例子; [Setup]; 注意: AppId 的值是唯 ...
- LinearLayout的gravity属性以及其子元素的layout_gravity何时有效;RelativeLayout如何调整其子元素位置只能用子元素中的属性来控制,用RelativeLayout中的gravity无法控制!!!
LinearLayout的gravity属性以及其子元素的layout_gravity何时有效:RelativeLayout如何调整其子元素位置只能用子元素中的属性来控制,用RelativeLayou ...
- Valentino发布2013秋冬高级定制系列_菁华时尚生活,时尚生活电子杂志,Fine Born China
Valentino发布2013秋冬高级定制系列_菁华时尚生活,时尚生活电子杂志,Fine Born China alentino发布2013秋冬高级定制系列 编辑: Mavis 图源于网络 图源于网络 ...
- leetcode_question_104 Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Xcode8 注释快捷键无效, 解决方案
这个是因为苹果解决xcode ghost.把插件屏蔽了.解决方法命令运行: sudo /usr/libexec/xpccachectl 然后必须重启电脑后生效
- Adding Pagination 添加分页
本文来自: http://www.bbsmvc.com/MVC3Framework/thread-206-1-1.html You can see from Figure 7-16 that all ...
- swift基本用法-for循环遍历,遍历字典,循环生成数组
// Playground - noun: a place where people can play import UIKit //--------------------------------- ...
- 【最大点独立集】【poj1419】【Graph Coloring】
题意: 最多能选取多少点,没有边相连. 解法: 取反图,求最大团 代码: #include<cstdio> #include<cstring> #include<iost ...