题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=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
题目大意:
有一只青蛙,它从起点(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) .求出k=gcd(x,y), 可以设为x=n*k,y=m*k(这里n=x/k,m=y/k,所以n,m互质,n*(m+1) 与m也互质,m*(1+n) 与m也互质),lcm(x,y)=n*m*k,所以可以重点可以设为(n*k*(1+m),m*k) 或者(n*k,m*k*(1+n)) ,
所以
可以时刻保证n>m,并且(n%(m+1)==0)就可以继续的往前寻找起始点,
注意:原先的位置也算一个。
This is the code
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x7f7f7f7f //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
LL gcd(LL x,LL y)
{
if(!y)
return x;
else gcd(y,x%y);
}
int main()
{
LL T;
scanf("%lld",&T);
for(LL t=1;t<=T;++t)
{
LL ans=1;
LL x,y;
scanf("%lld%lld",&x,&y);
if(x<y)
swap(x,y);
LL k=gcd(x,y);
x/=k;
y/=k;
while(x%(y+1)==0)
{
ans++;
x/=(y+1);
if(x<y)
swap(x,y);
}
printf("Case #%lld: %lld\n",t,ans);
}
}