/*
CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26
题意:
f(a, 0) = 0;
f(a, b) = 1 + f(a, b-gcd(a, b));
求 f(a, b) , a,b <= 1e12
分析:
b 每次减 gcd(a, b) 等价于 b/gcd(a,b) 每次减 1
减到什么时候呢,就是 b/gcd(a,b)-k 后 不与 a 互质
可先将 a 质因数分解,b能除就除,不能除就减到最近的a的因子的倍数,即模拟整个过程 由于 a 至多只有 64个因子 (a <= 2^64) ,复杂度挺低
*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL INF = 1e18;
const int N = 1e5;
LL a, b;
map<LL, int> mp;
map<LL, int>::iterator it;
void GetFactors(LL x)
{
for (LL i = 2; i*i <= x; i++)
{
if (x % i == 0)
{
while (x % i == 0)
{
mp[i]++;
x /= i;
}
}
}
if (x != 1) mp[x] = 1;
}
int main()
{
scanf("%lld%lld", &a, &b);
GetFactors(a);
LL ans = 0;
while (b)
{
for (it = mp.begin(); it != mp.end(); it++)
{
while ( (it->second) > 0 && b % (it->first) == 0)
{
b /= it->first;
--(it->second);
}
}
LL mi = INF, x = -1;
for (it = mp.begin(); it != mp.end(); it++)
{
if ((it->second) > 0 && b % (it->first) < mi)
{
mi = b % (it->first);
x = it->first;
}
}
if (x == -1)
{
ans += b; break;
}
ans += mi;
b -= mi;
}
printf("%lld\n", ans);
}

  

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. xpath的一些常用使用

    xml文档<html> <head> <title>My page</title> </head> <body> <h2& ...

  2. MyBatis 源码篇-插件模块

    本章主要描述 MyBatis 插件模块的原理,从以下两点出发: MyBatis 是如何加载插件配置的? MyBatis 是如何实现用户使用自定义拦截器对 SQL 语句执行过程中的某一点进行拦截的? 示 ...

  3. 简单分析FactoryBean

    1. 什么是FactoryBean FactoryBean本质上是一种Bean,只是它可以产生其他的Bean,比较特殊.在上下文getBean的时候,如果传入FactoryBean的名称,得到的是Fa ...

  4. java lesson09总结

    package Super; public class SuperTest {  String color;  // public SuperTest(String color) {this.colo ...

  5. Install CUDA 6.0 on Ubuntu 14.04 LTS

    Ubuntu 14.04 LTS is out, loads of new features have been added. Here are some procedures I followed ...

  6. 你真的了解new function(){} 和 function(){}()吗?

    只要 new 表达式之后的 constructor 返回(return)一个引用对象(数组,对象,函数等),都将覆盖new创建的匿名对象,如果返回(return)一个原始类型(无 return 时其实 ...

  7. 分布式全局ID的几种生成方案

    前言 在互联网的业务系统中,涉及到各种各样的ID,如在支付系统中就会有支付ID.退款ID等. 那一般生成ID都有哪些解决方案呢?特别是在复杂的分布式系统业务场景中,我们应该采用哪种适合自己的解决方案是 ...

  8. nhandled rejection Error: EPERM: operation not permitted, open 'C:\Program Files\nodejs\node_cache npm ERR! cb() never called!

    安装全局包时报错,之前已经遇到过,结果第二次又忘记解决方法,果然还是要记下来,好记性不如烂笔头哇 $ npm i electron -gUnhandled rejection Error: EPERM ...

  9. 多进程之multiprocessing模块和进程池的实现

    转载:https://www.cnblogs.com/xiaobeibei26/p/6484849.html Python多进程之multiprocessing模块和进程池的实现 1.利用multip ...

  10. Delphi Tobject类