Educational Codeforces Round 26 E

本文介绍了一种高效计算特定函数f(x, y)的方法,该函数递归地依赖于两个参数的最大公约数。通过去除参数间的共同质因数并计算这一过程中累积的步数,实现了快速求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. Vasya’s Function
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya is studying number theory. He has denoted a function f(a, b) such that:

f(a, 0) = 0;
f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b.
Vasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calculating this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly.

Input
The first line contains two integer numbers x and y (1 ≤ x, y ≤ 1012).

Output
Print f(x, y).

Examples
input
3 5
output
3
input
6 3
output
1
我们知道a%b==0时计算停止,我们每计算一次就是消除a,b质因子之间的“差异”每消除一个差异就加一,所以我们只需计算出这种“差异”即可

#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
    int x,y;
    scanf("%I64d %I64d",&x,&y);
    int ans=0;
    while(y){
        int g=__gcd(x,y);
        x/=g;
        y/=g;
        int temp1=x,temp2=y;
        for(int i=2;i*i<=x;i++){
            if(temp1%i) continue;
            temp2=min(temp2,y%i);
            while((temp1%i)==0) temp1/=i;
        }
        if(temp1>1) temp2=min(temp2,y%temp1);
        y-=temp2;
        ans+=temp2;
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值