【HDU5583 2015上海赛区L】【找规律 正难则反】LCM Walk 目标状态(x,y)哪些点走公倍数能走到它

 
 

LCM Walk

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

  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
 

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))

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值