zoukankan      html  css  js  c++  java
  • 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,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
     
    分析:
    最后的结果跟x,y的位置是无关的,我们先假定x<y那么y要跳回上一步的话,
    可以先分解为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
    ",Case,ans);
        }
        return 0;
    }
     
  • 相关阅读:
    企业架构-发布【企业架构框架-TOGAF v0.1.pdf】
    dropbox连接不上解决方法
    信息系统开发平台OpenExpressApp - 框架待完善工作事项
    微软全球Web Camps大会5月着落中国,免费报名参加2天原汁原味的活动,赶快报名
    BABOK - 企业分析(Enterprise Analysis)概要
    DNN(DotNetNuke)研究手札系列3-框架(概述)
    DNN(DotNetNuke)研究手札系列5之DNN的未来(蓝图)
    我乐意为博客园贡献一份力量:请签名
    DNN(DotNetNuke)研究手札系列4-框架(硬伤) 1
    感慨 20 之开源的前途/钱图?(1数据库)
  • 原文地址:https://www.cnblogs.com/a249189046/p/7629861.html
Copyright © 2011-2022 走看看