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;
    }
  • 相关阅读:
    NetScaler ‘Counters’ Grab-Bag!
    NetScaler + Wireshark = A Perfect Combination!
    What’s That NetScaler Reset Packet?
    Give NetScaler a “Tune-Up”
    51Nod 1080 两个数的平方和(数论,经典题)
    51Nod 1289 大鱼吃小鱼(模拟,经典好题)
    1082 与7无关的数(思维题,巨坑)
    51Nod 1003 阶乘后面0的数量(数学,思维题)
    2017广东工业大学程序设计竞赛决赛 题解&源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)
    后缀数组(一堆干货)
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5261588.html
Copyright © 2011-2022 走看看