1. /*
  2. CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26
  3. 题意:
  4. f(a, 0) = 0;
  5. f(a, b) = 1 + f(a, b-gcd(a, b));
  6. 求 f(a, b) , a,b <= 1e12
  7. 分析:
  8. b 每次减 gcd(a, b) 等价于 b/gcd(a,b) 每次减 1
  9. 减到什么时候呢,就是 b/gcd(a,b)-k 后 不与 a 互质
  10. 可先将 a 质因数分解,b能除就除,不能除就减到最近的a的因子的倍数,即模拟整个过程
  11.  
  12. 由于 a 至多只有 64个因子 (a <= 2^64) ,复杂度挺低
  13. */
  14. #include <bits/stdc++.h>
  15. using namespace std;
  16. #define LL long long
  17. const LL INF = 1e18;
  18. const int N = 1e5;
  19. LL a, b;
  20. map<LL, int> mp;
  21. map<LL, int>::iterator it;
  22. void GetFactors(LL x)
  23. {
  24. for (LL i = 2; i*i <= x; i++)
  25. {
  26. if (x % i == 0)
  27. {
  28. while (x % i == 0)
  29. {
  30. mp[i]++;
  31. x /= i;
  32. }
  33. }
  34. }
  35. if (x != 1) mp[x] = 1;
  36. }
  37. int main()
  38. {
  39. scanf("%lld%lld", &a, &b);
  40. GetFactors(a);
  41. LL ans = 0;
  42. while (b)
  43. {
  44. for (it = mp.begin(); it != mp.end(); it++)
  45. {
  46. while ( (it->second) > 0 && b % (it->first) == 0)
  47. {
  48. b /= it->first;
  49. --(it->second);
  50. }
  51. }
  52. LL mi = INF, x = -1;
  53. for (it = mp.begin(); it != mp.end(); it++)
  54. {
  55. if ((it->second) > 0 && b % (it->first) < mi)
  56. {
  57. mi = b % (it->first);
  58. x = it->first;
  59. }
  60. }
  61. if (x == -1)
  62. {
  63. ans += b; break;
  64. }
  65. ans += mi;
  66. b -= mi;
  67. }
  68. printf("%lld\n", ans);
  69. }

  

CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26的更多相关文章

  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 - 数论

    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. CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)

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

  5. Educational Codeforces Round 26

    Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...

  6. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  7. Educational Codeforces Round 59 (Rated for Div. 2) DE题解

    Educational Codeforces Round 59 (Rated for Div. 2) D. Compression 题目链接:https://codeforces.com/contes ...

  8. Educational Codeforces Round 67

    Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...

  9. Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)

    这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...

随机推荐

  1. postgresSQL常用命令

    1.createdb 数据库名称  产生数据库2.dropdb  数据库名称  删除数据库 3.CREATE USER 用户名称  创建用户4.drop User 用户名称  删除用户 5.SELEC ...

  2. tabs 导航 及内容切换

    <!-- 导航头 --> <div class="col-md-6" style="padding: 0px"> <ul id=& ...

  3. ArrayList集合详解

    ArrayList 实现了List的接口,是长度可变的数组,空间是连续的 api默认提供了很多操作ArrayLis的方法,这些方法可以去api里面查询使用 一.这么多方法怎么学?1.熟练使用常见的方法 ...

  4. request方法

    获取请求行方法: getMethod()获取请求的方法 getContextPath()回去虚拟路径 getServletPath()获取路径(只有在servert中使用) getQueryStrin ...

  5. Do Not Try This Problem(分块思想)

    题意:https://codeforces.com/group/ikIh7rsWAl/contest/259944/problem/D 给你q个操作,4个数n,a,k,c,从n好位置开始每次加a的位置 ...

  6. 4.Linux系统命令及其使用详解

    运维工程师必会的109个Linux命令   文件管理basename:从文件名中去掉路径和扩展名 cat:把档案串连接后传到基本输出(屏幕或加 > filename 到另一个档案)cd:切换目录 ...

  7. DataTime.Now.Ticks

    getTime public long getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数. 返回: 自 1970 年 1 月 1 ...

  8. request.getScheme() 使用方法(转)

    今天在看代码时,发现程序使用了 request.getScheme() .不明白是什么意思,查了一下.结果整理如下: 1.request.getScheme() 返回当前链接使用的协议:一般应用返回h ...

  9. C# 连接 Socks5 代理

    public class Socks5ProxyHelp { private Socks5ProxyHelp() { } public static string[] errorMsgs = { &q ...

  10. thinkphp 4.8 漏洞测试

    首先要部署环境 这里利用docker的方便部署性,来直接找个现成的    git clone https://github.com/vulnspy/thinkphp-5.1.29.git 下载安装后, ...