hdu 5584 LCM Walk(数学推导公式,规律)

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.

⋅ 1≤T≤1000.
⋅ 1≤ex,ey≤109.

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

Source

2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)

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值,故我们可以开始逆推了

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #include<queue>
 6 #include<stack>
 7 #include<math.h>
 8 #include<vector>
 9 #include<map>
10 #include<set>
11 #include<cmath>
12 #include<string>
13 #include<algorithm>
14 #include<iostream>
15 #define exp 1e-10
16 using namespace std;
17 const int N = 100005;
18 const int M = 10005;
19 const int inf = 1000000000;
20 const int mod = 10007;
21 int gcd(int x,int y)
22 {
23     if(x%y)
24         return gcd(y,x%y);
25     return y;
26 }
27 int main()
28 {
29     int t,k,c,p=1,x,y;
30     scanf("%d",&t);
31     while(t--)
32     {
33         c=1;
34         scanf("%d%d",&x,&y);
35         if(x>y)
36             swap(x,y);
37         k=gcd(x,y);
38         while(y%(x+k)==0)
39         {
40             c++;
41             y=y/(x/k+1);
42             if(x>y)
43                 swap(x,y);
44             k=gcd(x,y);
45         }
46         printf("Case #%d: %d\n",p++,c);
47     }
48     return 0;
49 }

时间: 2024-11-03 05:42:03

hdu 5584 LCM Walk(数学推导公式,规律)的相关文章

HDU - 5584 LCM Walk (数论 GCD)

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 sit

HDU 5844 LCM Walk(数学逆推)

http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k).现在给出终点坐标,求有多少个起点可以通过这种变化方式得到终点. 思路: 现在假设我们处于(x,y)这个坐标上,x和y的最大公约数为k,x和y用k来表示的话可以表示为x=$m_{1}$,y=$m_{2}$. 那么接下来可以得到($m_{1}$k,$m_{2}$k+$m_{1}$$m_{2}$k)或者 (

hdu 5312 Sequence【数学推导】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5312 解法: 这个题看上去是一个贪心, 但是这个贪心显然是错的. 事实上这道题目很简单, 先判断1个是否可以, 然后判断2个是否可以. 之后找到最小的整数k (k > 2), 使得(m - k) mod 6 = 0即可. 证明如下: 3n(n-1)+1 = 6(n*(n-1)/2)+1, 注意到n*(n-1)/2是三角形数, 任意一个自然数最多只需要3个三角形数即可表示. 枚举需要k个. 事实上,

[HDOJ5584]LCM Walk(数论,规律)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 给一个坐标(ex, ey),问是由哪几个点走过来的.走的规则是x或者y加上他们的最小公倍数lcm(x, y). 考虑(ex, ey)是由其他点走过来的,不妨设当走到(x,y)时候,gcd(x, y)=k,x=k*m1, y=k*m2. 下一步有可能是(x, y+x*y/gcd(x, y))或者是(x+x*y/gcd(x,y), y). 用k和m1,m2来表示为(k*m1, k*m2+m1*m2

hdu 5312 Sequence(数学推导——三角形数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1336    Accepted Submission(s): 410 Problem Description Today, Soda has learned a

hdu 5312 Sequence(数学推导+线性探查(两数相加版))

Problem Description Today, Soda has learned a sequence whose n-th (n≥1) item is 3n(n−1)+1. Now he wants to know if an integer m can be represented as the sum of some items of that sequence. If possible, what are the minimum items needed? For example,

HDOJ(HDU) 2524 矩形A + B(推导公式、)

Problem Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格. Input 第一行输入一个t, 表示有t组数据,然后每行输入n,m,分别表示网格的高和宽 ( n < 100 , m < 100). Output 每行输出网格中有多少个矩形. Sample Input 2 1 2 2 4 Sample Output 3 30 此方格其实就是求其中所有格子数,如果按宽度来算的话,1,2,3,-m,种情况,对每一种情况,有(1+2

hdu 5430 Reflect (数学推导题)

Problem Description We send a light from one point on a mirror material circle,it reflects N times and return the original point firstly.Your task is calcuate the number of schemes. ![](../../data/images/C628-1004-1.jpg) Input First line contains a s

hdu 5073 Galaxy (数学+推导)

Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 553    Accepted Submission(s): 126 Special Judge Problem Description Good news for us: to release the financial pressure, the government