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 ...
随机推荐
- Solr 8.2 使用指南
1 Solr简介 1.1 Solr是什么 Solr是一个基于全文检索的企业级应用服务器.可以输入一段文字,通过分词检索数据.它是单独的服务,部署在 tomcat. 1.2 为什么需要Solr 问题:我 ...
- 【洛谷】P5348 密码解锁
[洛谷]P5348 密码解锁 很显然我们可以推导出这个式子 设\(a(m)\)为\(m\)位置的值 \[ \mu(m) = \sum_{m | d} a(d) \\ a(m) = \sum_{m|d} ...
- 剑指offer46:圆圈中最后剩下的数字(链表,递归)
1 题目描述 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随 ...
- JavaScript Basics_Fundamentals Part 1_Variables
JavaScript Variables JavaScript 变量(Variables)是用于存储数据值的容器. 创建一个 JavaScript 变量,可以使用关键字 let. Example le ...
- multer使用
使用multer 1. 在项目中下载multer操作模块 Npm install multer --save 前端代码: <form class="layui-form&q ...
- PLSQL导出表结构和数据的三种方式
1.导出表结构和数据方式1.tools->export user objects是导出表结构 tools ->export user object 选择选项,导出.sql文件 说明:导出的 ...
- 【python】多进程、多线程、序列
一.多进程 1.子进程永远返回0,而父进程返回子进程的ID.这样做的理由是,一个父进程可以fork出很多子进程,所以,父进程要记下每个子进程的ID,而子进程只需要调用getppid()就可以拿到父进程 ...
- Image Processing and Analysis_8_Edge Detection:Finding Edges and Lines in Images by Canny——1983
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...
- Computer Vision_1_Active Appearance Models :Active Appearance Models——1998
此为计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面. 1. Active Appearance Models 活动表观模型和活动轮廓模型基本思想来源 Snake,现在 ...
- Linux ppp 数据收发流程
转:http://blog.csdn.net/yangzheng_yz/article/details/11526671 PPP (Point-to-Point)提供了一种标准的方法在点对点的连接上传 ...