我是靠谱客的博主 潇洒自行车,最近开发中收集的这篇文章主要介绍HDU 5584 (数学) LCM Walk,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

LCM Walk

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 87    Accepted Submission(s): 46


Problem Description
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) !
 

Input
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.

  1T1000 .
  1ex,ey109 .
 

Output
For 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
 


比赛的时候并没有经过严格的推导,后来还是推导了一下证明了正确性.

假设在位置(x,y),g = gcd (x,y),假设x/g = a,  y/g = b, 那么gcd (a, b) = 1,那么lcm (x, y) = a*b*g;

不妨设从位置(x, y) 跳到了(x+lcm, y),带入上面的数据,也就是跳到了(a*g*(b+1), b*g),新位置的两个坐标的除去这个g以后剩下的商分别是a*(b+1)和b,因为a和b互质,b+1和b也互质,所以a*(b+1)和b互质,也就是说新位置的gcd也等于g.

于是就得出了这样一种方法,求出新位置的gcd,两个坐标分别除以gcd,得到的就是a*(b+1)和b,然后如果存在前驱节点,大的必然能整除小的加一,商就是a,在乘以gcd就是前驱的x.这样就证明了方法的正确性.

#include <bits/stdc++.h>
using namespace std;
long long ans;
void dfs (long long a, long long b) {
if (a > b)
swap (a, b);
ans++;
long long g = __gcd (a, b);
long long aa = a/g, bb = b/g;
if (bb%(aa+1) == 0) {
long long ans = bb/(aa+1);
long long x = ans*g, y = a;
dfs (x, y);
}
}
int main () {
int t, kase = 0;
long long a, b;
cin >> t;
while (t--) {
cin >> a >> b;
ans = 0;
dfs (a, b);
printf ("Case #%d: %lldn", ++kase, ans);
}
return 0;
}


最后

以上就是潇洒自行车为你收集整理的HDU 5584 (数学) LCM Walk的全部内容,希望文章能够帮你解决HDU 5584 (数学) LCM Walk所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部