LCM Walk(hdu 5584;数论题

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5584

若起点是(x,y)下一个点可以是(x+z,y)或者(x,y+z)z是lcm(x,y)

题目给出终点,问你起点可能有几个(自己可以是自己的起点)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,j,t,n,m;
    cin>>t;
    int k1=1;
    while(t--)
    {
        cin>>n>>m;
        int sum=1;
        printf("Case #%d: ",k1++);
        while(1)
        {
            if(n>m)swap(n,m);
            int team=__gcd(n,m);
            int p=n/team;
            int q=m/((1+p)*team);
            if(m%((1+p)*team)!=0)break;
            n=p*team;
            m=q*team;
            sum++;
        }
        cout<<sum<<endl;
    }
}
View Code

注意当q不是整数时结束,退出循环

猜你喜欢

转载自www.cnblogs.com/ydw--/p/11730662.html