zoukankan      html  css  js  c++  java
  • hdu-5584 LCM Walk(数论)

    题目链接:LCM Walk

    Time Limit: 2000/1000 MS (Java/Others)    

    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 491    Accepted Submission(s): 254


    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+z,y)或(x,y+z),z为x和y的最小公倍数,问有多少个地方最终可以到达给的目的地(ex,ey)包括自身;
    思路:lcm(x,y)=x*y/gcd(x,y)    gcd(ex,ey)=gcd(x+x*y/gcd(x,y),y)(y比x小的时候,不然x,y换一下也行)
       由于gcd(a,n)=gcd(a+k*n,n)所以gcd(x,y)=gcd(ex,ey);所以就可以求出x了,然后循环这些步骤就可以计算一共有多少个位置满足题意了,记得算出一个x要代回去验证是否成立;
    AC代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define ll long long
    using namespace std;
    ll gcd(ll a,ll b)
    {
        if(b==0)return a;
        return gcd(b,a%b);
    }
    int main()
    {
        int t,cnt=1;
        ll x,y;
        scanf("%d",&t);
        while(t--)
        {
            ll ans=1;
            cin>>x>>y;
            while(1)
            {
            if(x<y)
            {
                ll fx=x;
                x=y;
                y=fx;
            }
            ll fy=y/gcd(x,y);
            if(x%(fy+1)!=0)break;
            else
            {
                ll ax=x/(fy+1);
                if(ax+ax*y/gcd(ax,y)!=x)break;
                else ans++,x=ax;
            }
            }
            cout<<"Case #"<<cnt<<": "<<ans<<"
    ";
            cnt++;
        }
    
        return 0;
    }
  • 相关阅读:
    非静态成员的sizeof
    Android中java.lang.ClassNotFoundException: ***.**** in loader dalvik.system.PathClassLoader[/data/app
    手机 SIM卡的EF
    android 获取手机ip的三种方式
    获取图片倒影效果
    python基础
    Python学习_数据处理split方法
    Python学习_从文件读取数据和保存数据
    Python学习_列表推导和Lambda表达式
    python学习_数据处理编程实例(一)
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5261588.html
Copyright © 2011-2022 走看看