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. oracle调用子存储过程+游标循环实例

    一,有子节点的部门的子节点的排序,调用子存储过程 CREATE OR REPLACE PROCEDURE "PRO_INIT_SORT" AS CURSOR cur_departm ...

  2. Bootstrap select(选择列表)

    当您想让用户从多个选项中进行选择,但是默认情况下只能选择一个选项,则使用选择框 1.使用<select>展示列表选项 2.使用multiple="multiple"允许 ...

  3. javascript顺序数组简单实现个二分查找

    直接上码了注释写得很详细: function bsearch(A,x){ //l:查找范围左 r:查找范围右 let l = 0, //查询范围左边界 r = A.length-1, //查找范围右边 ...

  4. HDU-2544-最短路(Bellman-Ford)

    Bellman-Ford算法是一个时间复杂度很高,但是它可以用来判断负环 负环就是上面的图,那个环的整体值小于零了,所以就是负环. 我们用Bellman-Ford算法进行更新,打一个表出来: k a ...

  5. RN服务

    https://facebook.github.io/react-native/docs/headless-js-android.html 当app在 后台运行 时,我们可以使用RN服务来同时地刷新数 ...

  6. 气泡小角的css实现

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> ...

  7. angular中几种加载css的方法

    1.Style URLs in Metadata We can load styles from external CSS files by adding a styleUrls attribute ...

  8. redis安装与安全设置

    redis Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件 yum安装redis 1.yum安装   #前提得配置好阿里云yum源,epel源 # ...

  9. python 四——线程、进程、协程

    内容概要 1.进程与线程优.缺点的比较 2.适用情况 3.线程 线程的创建 setDaemon join event RLock 队列 4.进程 创建进程 setDaemon join 线程与进程,数 ...

  10. 关于面试总结-app测试面试题

    前言 现在面试个测试岗位,都是要求全能的,web.接口.app啥都要会测,那么APP测试一般需要哪些技能呢? 面试app测试岗位会被问到哪些问题,怎样让面试管觉得你对APP测试很精通的样子? 本篇总结 ...