Educational Codeforces Round 26 E
来源:互联网 发布:斗牛牌型算法 编辑:程序博客网 时间:2024/06/04 23:17
E. Vasya’s Function
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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
我们知道a%b==0时计算停止,我们每计算一次就是消除a,b质因子之间的“差异”每消除一个差异就加一,所以我们只需计算出这种“差异”即可
#include <iostream>#include <stdio.h>#include <bits/stdc++.h>#define int long longusing namespace std;signed main(){ int x,y; scanf("%I64d %I64d",&x,&y); int ans=0; while(y){ int g=__gcd(x,y); x/=g; y/=g; int temp1=x,temp2=y; for(int i=2;i*i<=x;i++){ if(temp1%i) continue; temp2=min(temp2,y%i); while((temp1%i)==0) temp1/=i; } if(temp1>1) temp2=min(temp2,y%temp1); y-=temp2; ans+=temp2; } cout<<ans<<endl; return 0;}
- Educational Codeforces Round 26 E
- Educational Codeforces Round 21E
- Educational Codeforces Round 903E
- Educational Codeforces Round 26 E. Vasya's Function(数论)
- Educational Codeforces Round 26 E. Vasya's Function 数学,迭代法
- cf Educational Codeforces Round 26 E. Vasya's Function
- Educational Codeforces Round 26
- Educational Codeforces Round 26
- Educational Codeforces Round 12 E. Beautiful Subarrays
- Educational Codeforces Round 12-E. Beautiful Subarrays
- Educational Codeforces Round 12 E. Beautiful Subarrays
- Educational Codeforces Round 13 E 状压dp
- Educational Codeforces Round 12 E trie树
- Educational Codeforces Round 16 (A-E)
- Educational Codeforces Round 12E. Beautiful Subarrays
- Educational Codeforces Round 14 E.Xor-sequences
- Educational Codeforces Round 19 E. Array Queries
- Educational Codeforces Round 19-E. Array Queries
- Hadoop伪分布式环境搭建
- hdu1151最小路径覆盖
- 2017 Multi-University Training Contest
- 二维数组的查找
- Cookie与Session的工作原理解析
- Educational Codeforces Round 26 E
- PHP面向对象深入理解之二(封装、继承、多态、克隆)
- HDU 3460 Ancient Printer(字典树)
- Fractions Again?! UVA
- 另一种MapReduce实现矩阵乘法
- 48.图像旋转
- java-最长回文串
- [dts]DTS实例分析
- 【HDU 6048 Puzzle】 逆序对 & 思维