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) = 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
- 3 5
- 3
- 6 3
- 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的更多相关文章
- 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)) ...
- CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26
/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- 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 ...
- 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 ...
- 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) = ...
- 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]代表已经选择 ...
- Educational Codeforces Round 26 E - Vasya's Function
数论题还是好恶心啊. 题目大意:给你两个不超过1e12的数 x,y,定义一个f ( x, y ) 如果y==0 返回 0 否则返回1+ f ( x , y - gcd( x , y ) ); 思路:我 ...
- 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$一定 ...
随机推荐
- tomcat常用的优化和配置
Tomcat 5常用优化和配置 1.JDK内存优化: Tomcat默认可以使用的内存为128MB,Windows下,在文件{tomcat_home}/bin/catalina.bat,Unix下,在文 ...
- 当数据量很少的时候,tableview会显示多余的cell--iOS开发系列---项目中成长的知识二
当数据量很少的时候,tableview会显示很多的cell,而且是空白的,这样很不美观 所以使用下面的方法可以去掉多余的底部的cell 原理是:设置footerView为frame 是 CGRectZ ...
- 随机生成一份试卷,试卷的种类分为单选、多选、判断三种题型。nodejs6.0 mysql
背景:从数据库中,随机生成一份试卷,试卷的种类分为单选.多选.判断三种题型. 首先我需要生成随机数id(在这之前我需要知道数据库中各个题型的题数,这样我才能设置随机数),并依据生成的随机数id,去查找 ...
- 解读tensorflow之rnn【转】
转自:https://blog.csdn.net/mydear_11000/article/details/52414342 from: http://lan2720.github.io/2016/0 ...
- Ecshop的积分商城-对不起,该商品库存不足,现在不能兑换
1. 打开Ecshop积分商城文件 "根目录/exchange.php" 发现248行与289行都有库存不足时报错的提示代码: 248行: /* 查询:检查兑换商品是否有库 ...
- grep理解
http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html部分摘录于此 grep与正规表达式 字符类 字符类的搜索:如果我想要搜 ...
- 在 shell中, 我們可用 $0, $1, $2, $3 ... 這樣的变量分別提取命令行中变量
代码: script_name parameter1 parameter2 parameter3 ...我們很容易就能猜出 $0 就是代表 shell script 名称(路径)本身,而 $1 就是其 ...
- Android自动化测试Uiautomator--UiCollection接口简介
这个对象可以理解为一个对象的集合,因为UiSelector描述后得到的有可能是多个满足条件的控件集合,因此可以用来生成UiCollection,继承自UiObject. 用于枚举一个容器的用户界面(U ...
- Java线程和多线程(三)——线程安全和同步
线程安全在Java中是一个很重要的课题.Java提供的多线程环境支持使用Java线程.我们都知道多线程共享一些对象实例的话,可能会在读取和更新共享数据的事后产生数据不一致问题. 线程安全 之所以会产生 ...
- PYDay10&11&12&13-常用模块:time|datetime|os|sys|pickle|json|xml|shutil|logging|paramiko、configparser、字符串格式化、py自动全局变量、生成器迭代器
1.py文件自动创建的全局变量 print(vars()) 返回值:{'__name__': '__main__', '__package__': None, '__loader__': <_f ...