Discription

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).

Example

Input
3 5
Output
3
Input
6 3
Output
1

水水数论,可以发现随着运算的过程,gcd单调不减,所以我们可以每次处理gcd相同的一段。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll num[21],N=0,mn;
ll a,b,d,ans=0,A,B; ll gcd(ll x,ll y){
return y?gcd(y,x%y):x;
} inline void dvd(ll x){
for(int i=2;i*(ll)i<=x;i++) if(!(x%i)){
num[++N]=i;
while(!(x%i)) x/=i;
if(x==1) break;
}
if(x!=1) num[++N]=x;
} inline void solve(){
dvd(a); d=gcd(a,b);
while(b){
A=a/d,B=b/d,mn=1ll<<60;
if(A==1){
ans+=B;
break;
} for(int i=1;i<=N;i++) if(!(A%num[i])) mn=min(mn,B%num[i]);
ans+=mn,b-=mn*d,d=gcd(b,a);
} printf("%I64d\n",ans);
} int main(){
scanf("%I64d%I64d",&a,&b);
solve();
return 0;
}
 

Codeforces 837 E Vasya's Function的更多相关文章

  1. Codeforces 837E. Vasya's Function

    http://codeforces.com/problemset/problem/837/E   题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)) ...

  2. CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26

    /* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...

  3. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  4. ural 1353. Milliard Vasya's Function(背包/递归深搜)

    1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning ma ...

  5. ural 1353. Milliard Vasya's Function(dp)

    1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning ma ...

  6. 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) = ...

  7. Educational Codeforces Round 26 [ D. Round Subset ] [ E. Vasya's Function ] [ F. Prefix Sums ]

    PROBLEM D - Round Subset 题 OvO http://codeforces.com/contest/837/problem/D 837D 解 DP, dp[i][j]代表已经选择 ...

  8. Educational Codeforces Round 26 E - Vasya's Function

    数论题还是好恶心啊. 题目大意:给你两个不超过1e12的数 x,y,定义一个f ( x, y ) 如果y==0 返回 0 否则返回1+ f ( x , y - gcd( x , y ) ); 思路:我 ...

  9. Vasya's Function CodeForces - 837E (gcd)

    大意: 给定$a,b$, $1\le a,b\le 1e12$, 定义 $f(a,0)=0$ $f(a,b)=1+f(a,b-gcd(a,b))$ 求$f(a,b)$. 观察可以发现, 每次$b$一定 ...

随机推荐

  1. shell脚本,按字母出现频率降序排序。

    [root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...

  2. Swift 中 String 与 CChar 数组的转换

    在现阶段Swift的编码中,我们还是有很多场景需要调用一些C函数.在Swift与C的混编中,经常遇到的一个问题就是需要在两者中互相转换字符串.在C语言中,字符串通常是用一个char数组来表示,在Swi ...

  3. Fortran学习记录3(选择语句)

    流程控制语句 if的基本用法 if-else语句块 多重判断if-elseif语句 if语句嵌套 Select case语句 Goto语句 PAUSE CONTINUE STOP 流程控制语句 if的 ...

  4. 洛谷 P1663 山

    https://www.luogu.org/problemnew/show/P1663 可能在这里看会好一点:[题解]

  5. [LUOGU] 3959 宝藏

    https://www.luogu.org/problemnew/show/P3959 注意到n非常小,考虑状压/搜索. 发现状压需要枚举起点,跑n次,一个问题是转移不可以以数字大小为阶段了,考虑用d ...

  6. (42)zabbix使用IT services 了解服务器SLA整体情况

    什么是IT Services 服务器或者某项服务.业务的可用率,不懂技术的上级领导会过问最近服务器可用率如何.所有api的状况怎么样? 通常一些技术人员会说负载怎么样,哪些cpu使用率怎么样,硬盘使用 ...

  7. GIMP工具箱的自定义操作

    首选项 中还包含工具箱的自定义操作:

  8. python入门(一)作业

    一,流程控制之if...else 1. 如果:女人的年龄>30岁,那么:叫阿姨,否则:叫小妞 age_of_girl = 21 if age_of_girl >30: print('阿姨' ...

  9. perl学习之HERE文档

    Perl的here文档机制是从UNIX shell中的here文档机制派生而来的. 和在shell中一样,Perl中的here文档也是面向行的引用表单,要求提供<<运算符,其后跟随一个初始 ...

  10. 我的Python分析成长之路8

    Numpy数值计算基础 Numpy:是Numerical Python的简称,它是目前Python数值计算中最为基础的工具包,Numpy是用于数值科学计算的基础模块,不但能够完成科学计算的任而且能够用 ...