2015ACM/ICPC亚洲区上海站LCM Walk(数论lcm考察和欧几里得定理的应用)

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

数论挑战赛,刚接触数论这玩意真的伤脑子。

题目给出终点(ex,ey)反推起点(x,y);

那从起点开始,下一个点的坐标必是(x+z,y)或者是(x,y+z)这两种情况;

我们不妨设起点x=at,y=bt,z=lcm(a,b)=abt;

当然,根据裴蜀定理和扩展欧几里得定理a与b互素

那对于(ex,ey)=(x,y+z)

=(at,bt+abt)=t(a,(b(1+a));

因为a与b互素,

所以说t=gcd(ex,ey)

x=at=ex;

y=bt=ey*t/(ex+t);

那第二种情况x,y+z来说,根据裴蜀定理和欧几里得定理,保证ex<ey的前提,也就是说对xy将进行位置互换,然后在处理,

最后得到即可

Talk is cheap. Show me the code.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int x,y;
    int n;
    cin>>n;
    for(register int i=1;i<=n;i++)//对于每一个输入
    {
        cin>>x>>y;
        if(x<y)
        swap(x,y);
        int t=__gcd(x,y);//求公约数
        int ans=1;
        while(x%(y+t)==0)//判断是否满足条件处理
        {
            ans++;//是一种情况
            x=x/((y/t)+1);//反推
            if(x<y)//第二种情况
            swap(x,y);
        }
        printf("Case #%d: %d\n",i,ans);
    }
    return 0;
}

 

posted @ 2022-05-04 14:43  江上舟摇  阅读(21)  评论(0编辑  收藏  举报