题意:

  • f(a, 0) = 0;
  • f(a, b) = 1 + f(a, b - gcd(a, b))
  • 输出f(a,b)

a=A*gcd(a,b)    b=B*gcd(a,b)

一次递归后,变成了 f(A*gcd(a,b),(B-1)*gcd(a,b))

若gcd(A,(B-1))=1,那么 这一层递归的gcd(a,b)仍等于上一层递归的gcd(a,b)

也就是说,b-gcd(a,b),有大量的时间减的gcd(a,b)相同,

若计算出减了S次相同的gcd(a,b)

那就可以直接由f(a,b)到 f(a,b-S*gcd(a,b))+ S

设T是A的任意因子

那么S满足 (B-S)%T=0,且S最小

移项得 S=B%T

即S是B对A的所有因子取模得到的数中最小的那个

新的一次递归中,

a ' =a,b ' =(B-S)*gcd(a,b),gcd ' = gcd*T

如何得到T和S?

枚举所有因子会TLE

我们发现,整个过程a始终没有改变,只是参与了求gcd

对于输入的a,b

令A=a/gcd(a,b),B=b/gcd(a,b)

这样A,B 就没有公因子

这样 原来的b-S*gcd 相当于 现在的B-S

求出A的所有素因子

因为A为定值,所以下一次求S用A的素因子时,可以从上一次求S用的A的素因子剩下的里选

就是说

如果这次某个因子是B的因子,那么下一次就不会用这个因子了,B/=这个因子

即这个因子参与构成新的gcd

如果这次某个因子不是B的因子,下一次就可以考虑它

#include<vector>
#include<iostream>
using namespace std;
typedef long long LL;
LL ans;
vector<LL>p;
vector<LL> :: iterator it;
LL gcd(LL a,LL b) { return !b ? a:gcd(b,a%b); }
void work(LL y)
{
if(!y) return;
LL k=y;
for(it=p.begin();it!=p.end();++it) k=min(k,y%(*it));
ans+=k; y-=k;
vector<LL>q;
for(it=p.begin();it!=p.end();++it)
if(y%(*it)) q.push_back(*it);
else y/=(*it);
swap(p,q);
work(y);
}
int main()
{
LL a,b;
cin>>a>>b;
LL g=gcd(a,b);
a/=g; b/=g;
for(LL i=;i*i<=a;i++)
while(a%i==)
{
p.push_back(i);
a/=i;
}
if(a>) p.push_back(a);
work(b);
cout<<ans;
}
E. Vasya's Function
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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

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

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

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

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

  3. Codeforces 837E Vasya's Function 数论 找规律

    题意:定义F(a,0) = 0,F(a,b) = 1 + F(a,b - GCD(a,b).给定 x 和 y (<=1e12)求F(x,y). 题解:a=A*GCD(a,b) b=B*GCD(a ...

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

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

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

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

  7. CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)

    /* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...

  8. 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$一定 ...

  9. Codeforces 837 E Vasya's Function

    Discription Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) =  ...

随机推荐

  1. 【BZOJ1096】【ZJOI2007】仓库建设(斜率优化,动态规划)

    [BZOJ1096][ZJOI2007]仓库建设(斜率优化,动态规划) 题面 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原 ...

  2. 【NOI2014】起床困难综合症(贪心)

    [NOI2014]起床困难综合症(贪心) 题面 Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚 ...

  3. 【SDOI2009】HH去散步(矩阵快速幂)

    题面 题目描述 HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但是同时HH又是个喜欢变化的人,所以他不会立刻沿着刚刚走来的路走回. 又因为HH是 ...

  4. [BZOJ1000] A+B Problem

    Description Calculate a+b Input Two integer a,b (0<=a,b<=10) Output Output a+b Sample Input 1 ...

  5. [BZOJ3293] [Cqoi2011] 分金币 (贪心)

    Description 圆桌上坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值. Inpu ...

  6. 含隐变量模型求解——EM算法

    1 EM算法的引入1.1 EM算法1.2 EM算法的导出2 EM算法的收敛性3EM算法在高斯混合模型的应用3.1 高斯混合模型Gaussian misture model3.2 GMM中参数估计的EM ...

  7. hadoop第一课

    Hadoop基本概念 在当下的IT领域,大数据很"热",实现大数据场 景的Hadoop系列产品更"热". Hadoop是一个开源的分布式系统基础架构,由 Apa ...

  8. JavaScript中的私有成员[翻译]

    原作者:Douglas Crockford,原文地址:http://www.crockford.com/javascript/private.html JavaScript 是世界上被误解最深的编程语 ...

  9. 初识 .net core和vs code

    定义:什么是.net core? .net core是一个跨各个不同操作系统运行的平台.时至今日,windows上.net framework已经发展成熟,可以用来开发windows平台下的几乎所有应 ...

  10. STM32F10x的启动汇编分析

    ;******************** (C) COPYRIGHT 2009 STMicroelectronics ********************;* File Name         ...