hdu5584逆推/acmicpc2015上海现场赛3号题

LCM Walk

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1079    Accepted Submission(s): 567


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
 唉,自己拿这场现场赛练了练手,结果对比榜,血崩。。。。
原因在于一道计算几何不知错哪,然后这逆推题,死活没思路。。。做题太少,类型见得太少了。。。
赛后查了下各大菊苣的题解,才发现自己多蠢(不过同时也涨了姿势,gcd,lcm类题可设未知数搞搞。。
废话不多说,下面介绍下AC思想
从终点一步一步往前推的思路,但也有所根据。首先,我们假设起点为(x,y),x=m1*k,y=m2*k(这样设的好处在于下一步lcm很容易列出),,m1*m2*k即为lcm(m1*k,m2*k),这一点应该无需赘言,则下一步为(m1*k+m1*m2*k,m2*k)或者(m1*k,m2*k++m1*m2*k),式子可化为(m1*(1+m2)*k,m2*k),这时我们能够发现,(m1,m2)是互质的,(m2,m2+1)也是互质的,所以这样的gcd仍然是k,则可推出结论,从起点开始到终点的gcd没变过。这样既可逆推。只需判断m1是否能够为整数,既可找到多少个起点。(这里的思想是看的各位神犇的博客,确实厉害!)
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<stack>
#include<queue>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#define eps 1e-8
#define zero(x) (((x>0?(x):-(x))-eps)
#define mem(a,b) memset(a,b,sizeof(a))
#define memmax(a) memset(a,0x3f,sizeof(a))
#define pfn printf("\n")
//#define ll __int64
#define mod 1000000007
#define sf(a) scanf("%d",&a)
#define sf64(a) scanf("%I64d",&a)
#define sf264(a,b) scanf("%I64d%I64d",&a,&b)
#define sf364(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define sf2(a,b) scanf("%d%d",&a,&b)
#define sf3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define sff(a) scanf("%f",&a)
#define sfs(a) scanf("%s",a)
#define sfs2(a,b) scanf("%s%s",a,b)
#define sfs3(a,b,c) scanf("%s%s%s",a,b,c)
#define sfd(a) scanf("%lf",&a)
#define sfd2(a,b) scanf("%lf%lf",&a,&b)
#define sfd3(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)
#define sfd4(a,b,c,d) scanf("%lf%lf%lf%lf",&a,&b,&c,&d)
#define sfc(a) scanf("%c",&a)
#define debug printf("***\n")
const double PI = acos(-1.0);
const double e = exp(1.0);
const int INF = 0x7fffffff;;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
bool cmpbig(int a, int b){ return a>b; }
bool cmpsmall(int a, int b){ return a<b; }
using namespace std;
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int main()
{
    freopen("data.in","r",stdin);
    int t,tot=1;
    sf(t);
    while(t--)
    {
        int n,m,x,y,cnt=1;
        sf2(n,m);
        x=max(n,m),y=min(n,m);
        int k=gcd(x,y);
        while(x%(k+y)==0)
        {
            n=x/(k+y)*k,m=y;
            cnt++;
            //printf("%d %d\n",x,y);
            x=max(n,m),y=min(n,m);
            k=gcd(x,y);
        }
        printf("Case #%d: %d\n",tot++,cnt);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值