LCM WalkTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 53 Accepted Submission(s): 31
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
Sample Output
Source
|
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int x,y;
int num;
int gcd(int x,int y)
{
return y==0?x:gcd(y,x%y);
}
void solve(int x,int y)
{
++num;
if(x<y)swap(x,y);
if(x%(y+1)==0)solve(x/(y+1),y);
}
int main()
{
scanf("%d",&casenum);
for(casei=1;casei<=casenum;++casei)
{
scanf("%d%d",&x,&y);
int g=gcd(x,y);x/=g;y/=g;
num=0;
solve(x,y);
printf("Case #%d: %d\n",casei,num);
}
return 0;
}
/*
【trick&&吐槽】
1,呀,正难则反的思想很关键
2,呀,敢于猜解答案的程序员才是好程序员,噗!
【题意】
我们的坐标是二维的,从位置(x,y)出发,每走一步,我们会走到(x+lcm(x,y),y)或者(x,y+lcm(x,y))。
现在反过来。我们告诉你,目前的位置是(x,y),问你经过若干步后,能走到这个位置的点有多少个。
【类型】
数论
【分析】
这题我是找规律AC的>_<,现在来理解一下。
首先,很显然,X和Y可以都除掉它们的gcd。毕竟这个gcd是我们消除不掉的,对答案无法影响。
然后,现在我们得到(X,Y),而且gcd(X,Y)==1。
那么我们有——
如果X==Y,肯定无法再走下去。
如果X>Y(交换位置是不影响的,如果X<Y就做交换)
我们倒着想不太好想,不如反过来,正着思考。
有(x,y),x可以表示成pt,y可以表示成qt,那么gcd是t,lcm就是pqt
然后比如说是x(pt)走一步,就变成了(p(q+1)t,qt)。
我们会发现,要如何求之前的x?用p(q+1)t/(q+1)t,就能得到x的t前的系数p。
这个gcd是永远也消不掉的,于是,我们不妨一开始就把gcd消除,这样t就变成了1.
即初始有(x,y),x可以表示成p,y可以表示成q,p和q互质,lcm就是pq
然后比如说是x(p)走一步,就变成了(p(q+1),q)。
因为p与q互质,q+1也与q互质,所以p(q+1)同样与q互质。
即后面不论走多少步都互质,即,这个性质可以不停地迭代调用。
我们要如何求之前的x?呀,式子都摆给你了,知道了q和p(q+1),让你求p。
于是直接用p(q+1)/(q+1),就得到p,也就是之前的x。也说明了每个位置的前驱位置,如果有,也必然唯一。
于是这道题就这么做完啦!
【时间复杂度&&优化】
O(Tlog(x+y))
*/