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. [转帖]Intel 上一代 可扩展CPU的简单报价

    8.1万元人间毒物!Intel 28核铂金版Xeon 8180零售上市 http://news.mydrivers.com/1/541/541670.htm 猜你想看:英特尔 CPU处理器 Xeon ...

  2. 1.3.3 并发容器类MAP/LIST/SET/QUEUE

    HashMap 下标计算方法:hashCode & (length-1) ,&按位与操作,二进制相同位都是1,该位才为1 JDK1.7与JDK1.8中HashMap区别: JDK1.8 ...

  3. Photon Server 实现注册与登录(四) --- 服务端响应登陆和注册

    前面已经整理过了服务端代码,MyGameServer.cs 和 ClientPeer.cs 对请求和响应进行了拆分.接下来处理对前端的响应 一.响应登陆请求 之前整理中,响应前端请求主要在类Clien ...

  4. 码云以及Git的使用

    码云以及Git的使用 码云就是一个远程管理的仓库,Git是用来上传和下载数据的工具. 首先访问网站 https://gitee.com/ 进行注册 注册完成后,进入如下页面 点击新建仓库 设置自己的仓 ...

  5. tensorflow lite 之生成 tflite 模型文件

    下载最新的的tensorflow源码. 1.配置 tflite 文件转换所需环境 安装 bazel 编译工具 https://docs.bazel.build/versions/master/inst ...

  6. S02_CH16 等精度频率计实验

    S02_CH16 等精度频率计实验 在了解了AXI总线之后,今天我们自己动手设计一个带AXI4-Lite总线的IP,来完成频率计的实验. 频率计虽然小,但是也算五脏俱全,涉及到zynq的方方面面,比如 ...

  7. Job和Service

    Job及CronJob: ---apiVersion: batch/v1kind: Jobmetadata:  name: job-demospec:  template:    metadata:  ...

  8. 牛客 197E 01串

    大意: 给定01串, 单点修改, 询问给定区间$[l,r]$, 假设$[l,r]$从左往右得到的二进制数为$x$, 每次操作增加或减少2的幂, 求最少操作数使得$x$为0. 线段树维护2*2矩阵表示低 ...

  9. editormd 富文本编辑器转 html

    // html <div id="markdown-view"> <textarea id="markdownView" style=&quo ...

  10. Qt调用VS生成的dll

      预备知识: 1.如果在没有导入库文件(.lib),而只有头文件(.h)与动态链接库(.dll)时,我们才需要显示调用,如果这三个文件都全的话,我们就可以使用简单方便的隐式调用. 2.通常Windo ...