Codeforces 837E Vasya's Function - 数论

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

  题目大意 (题目太简洁,不需要大意)

  因为Codeforces 837E Vasya's Function - 数论,所以最终一定会到达边界情况。

  所以我们考虑如果a,b的gcd不为1,那么f(a, b - gcd(a, b))在干的事情相当于把b表示成gcd(a, b) * x的形式,每次递归就相当于就是让x减少某个数,如果设g = gcd(a, b),那么就有f(a. b) = f(a / g, b / g)。

  如果a和b的gcd是1,那么我们考虑下一个和a不互质的数。这个数一定是a的某个质因子的倍数,所以我们根号大暴力将a质因数分解,然后for一遍,挨个计算不超过b的最大的是pi的倍数的数,然后继续上面的做法,递归求解。

  因为当gcd不为1时,至少为2,所以递归的层数不超过Codeforces 837E Vasya's Function - 数论层,因为a至多有log2a个不同的质因子,所以总时间复杂度为Codeforces 837E Vasya's Function - 数论

Code

 /**
* Codeforces
* Problem#837E
* Accepted
* Time: 15ms
* Memory: 2048k
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
typedef bool boolean;
#define smax(a, b) a = max(a, b)
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} #define LL long long template<typename T>
T gcd(T a, T b) {
return (b == ) ? (a) : (gcd(b, a % b));
} LL a, b; inline void init() {
readInteger(a);
readInteger(b);
LL g = gcd(a, b);
a /= g, b /= g;
} vector<LL> fac;
void getFactor(LL x) {
fac.clear();
for(LL i = ; i * i <= x; i++) {
if((x % i) == ) {
while((x % i) == ) x /= i;
fac.push_back(i);
}
}
if(x > ) fac.push_back(x);
} LL f(LL a, LL b) {
if(b <= ) return b;
getFactor(a);
LL near = , g;
for(int i = ; i < (signed)fac.size(); i++)
smax(near, b / fac[i] * fac[i]);
g = gcd(a, near);
return b - near + f(a / g, near / g);
} inline void solve() {
printf(Auto, f(a, b));
} int main() {
init();
solve();
return ;
}
上一篇:让Dock自动 显示/隐藏 不再有延迟


下一篇:linux下启动tomcat出现“This file is needed to run this program ”