Educational Codeforces Round 26 E

xiaoxiao2021-02-28  117

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; }
转载请注明原文地址: https://www.6miu.com/read-19331.html

最新回复(0)