CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26
/*
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的更多相关文章
- 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 - 数论
Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = ...
- 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 ...
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
- Educational Codeforces Round 26
Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Educational Codeforces Round 59 (Rated for Div. 2) DE题解
Educational Codeforces Round 59 (Rated for Div. 2) D. Compression 题目链接:https://codeforces.com/contes ...
- Educational Codeforces Round 67
Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...
- Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)
这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...
随机推荐
- [转帖]Oracle 起诉 Google 事件
Oracle 起诉 Google 事件 https://www.cnblogs.com/panchanggui/p/9449842.html Oracle 是世界第二大软件公司 世界第一大DBMS公司 ...
- linux中学习中提炼出来的命令
Linux: 基于debain的系统(如:Ubuntu等)pms基本工具:aptitude[apt-get],dpkgaptitude search package_name 查看软件包是否已安装,如 ...
- 三分钟搞定Python中的装饰器
python的装饰器是python的特色高级功能之一,言简意赅得说,其作用是在不改变其原有函数和类的定义的基础上,给他们增添新的功能. 装饰器存在的意义是什么呢?我们知道,在python中函数可以调用 ...
- php中连接tcp服务的三种方式
首先需要现有一个 tcp 服务,我们使用 php中的 socket 系列函数实现 <?php //创建socket套接字 $socket = socket_create(AF_INET, SOC ...
- thinkphp5.1中使用Bootstrap4分页样式修改
1.找到thinkphp下的Boorstrap的源码 \thinkphp\library\think\paginator\driver\Bootstrap.php 2丶直接修改源码 <?php ...
- 【AC自动机】洛谷三道模板题
[题目链接] https://www.luogu.org/problem/P3808 [题意] 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. [题解] 不再介绍基础知识了,就是裸的模 ...
- Python 常用内置模块详解
Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...
- webpack打包时删除console.log,和debugger
开发过程中我们不可避免的需要console.log调试,然而在上线时如果不删除这些console.log可能会造成内存泄漏,因为console.log出来的变量是不会被GC的,webpack给我们提供 ...
- c#连接数据库SqlHelper报错
这是一个困扰了我好几天的问题,首先看一下报错信息 代码: private static string connectionString = ConfigurationManager.Connectio ...
- std::string与char*之临时缓冲区
std::string与char*之临时缓冲区 原文:https://blog.csdn.net/hsshh1988/article/details/80689330 c++文件读取流程如下: ifs ...