HDU 5584 LCM Walk (lcm/gcd)
LCM Walk
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1834 Accepted Submission(s): 955
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, or (x,y.
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!
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, or (x,y.
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.
⋅ 1≤ex,ey.
Every test case contains two integers ex and ey, which is the destination grid.
⋅ 1≤T.
⋅ 1≤ex,ey.
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的位置是无关的,我们先假定x<y那么y要跳回上一步的话,
可以先分解为y=lcm(x,k)+k;
可以先分解为y=lcm(x,k)+k;
我们知道当x与k互质的时候,lcm(x,k)=x*k;
那么让x,y互质的话,x与k也会互质
我们可以想想,让x,y互质是不会影响最终结果的
所以我们让x,y除以它们的最大公因数,然后y=x*k+k;
y=(x+1)k;即当y能够整除x+1时,存在对应的k,
这时候的k和x,又成为了新一轮的x,y;
不断的转化下去,看又多少轮即可
代码如下:
#include <cstdio> #include <algorithm> #include <iostream> using namespace std; typedef long long ll; int main() { ll t; ll a,b; ll ans; ll gcd1; ll Case=0; scanf("%lld",&t); while(t--) { scanf("%lld%lld",&a,&b); Case++; if(a>b)swap(a,b); gcd1=__gcd(a,b); a=a/gcd1; b=b/gcd1; ans=1; while(1) { if(b%(a+1)!=0||a==0||b==0)break; b=b/(a+1); if(a>b)swap(a,b); ans++; } printf("Case #%lld: %lld\n",Case,ans); } return 0; }