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 ...
随机推荐
- js调试
在chrome下的调试案例 1.console.log() $("#typeid").change(function(){ var id = $(this).val(); cons ...
- OpenSSL win 下报错 OPENSSL_Uplink(58D46000,08): no OPENSSL_Applink
python 中使用 M2Crypto 想读写文件时总是报: OPENSSL_Uplink(58D46000,08): no OPENSSL_Applink windows 平台下使用 openssl ...
- 判图的连通性(dfs,并查集)
一.无向图 欧拉回路:每个顶点度数都是偶数 欧拉路:所有点度数为偶数,或者只有2个点度数为奇数 当然判连通性 hdu 1878 欧拉回路 两种判连通的方法 dfs #include <iostr ...
- spring 定时任务(3)--配置多个定时任务
<!-- 定义调用对象和调用对象的方法 --> <!-- 定时任务 A start --> <bean id="jobtask" class=&quo ...
- poj3233之经典矩阵乘法
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 12346 Accepted: ...
- C++求二叉树的最大高度差
#include <iostream> #include <string.h> using namespace std; template<typename Type&g ...
- 小黑小波比.coding的使用
1_Coding的演示 1_html的演示 1_先查看帮助 1.它支持的语言非常多.下面是链接地址 https://coding.net/u/bobo159357456/p/html/paas/hel ...
- hdu1814 Peaceful Commission,2-sat
题目大意:一国有n个党派.每一个党派在议会中都有2个代表,现要组建和平委员会,要从每一个党派在议会的代表中选出1人,一共n人组成和平委员会.已知有一些代表之间存在仇恨,也就是说他们不能同一时候被选为和 ...
- PHP-xml-1
SimpleXML 解析xml 假设xml没有声明encoding默认使用utf-8进行解码.终于得到的数据编码为utf-8格式. 我猜想它内部实现了iconv(encoding='utf-8', ' ...
- PropertyGrid—默认属性,默认事件,属性默认值
零.引言 PropertyGrid显示一个对象的属性和事件时,可以设置其默认属性和事件,也就是当你选中对象时,propertyGrid中焦点在哪一个属性或事件上.为对象的属性提供默认值,使Proper ...