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 ...
随机推荐
- [转帖]Intel 上一代 可扩展CPU的简单报价
8.1万元人间毒物!Intel 28核铂金版Xeon 8180零售上市 http://news.mydrivers.com/1/541/541670.htm 猜你想看:英特尔 CPU处理器 Xeon ...
- 1.3.3 并发容器类MAP/LIST/SET/QUEUE
HashMap 下标计算方法:hashCode & (length-1) ,&按位与操作,二进制相同位都是1,该位才为1 JDK1.7与JDK1.8中HashMap区别: JDK1.8 ...
- Photon Server 实现注册与登录(四) --- 服务端响应登陆和注册
前面已经整理过了服务端代码,MyGameServer.cs 和 ClientPeer.cs 对请求和响应进行了拆分.接下来处理对前端的响应 一.响应登陆请求 之前整理中,响应前端请求主要在类Clien ...
- 码云以及Git的使用
码云以及Git的使用 码云就是一个远程管理的仓库,Git是用来上传和下载数据的工具. 首先访问网站 https://gitee.com/ 进行注册 注册完成后,进入如下页面 点击新建仓库 设置自己的仓 ...
- tensorflow lite 之生成 tflite 模型文件
下载最新的的tensorflow源码. 1.配置 tflite 文件转换所需环境 安装 bazel 编译工具 https://docs.bazel.build/versions/master/inst ...
- S02_CH16 等精度频率计实验
S02_CH16 等精度频率计实验 在了解了AXI总线之后,今天我们自己动手设计一个带AXI4-Lite总线的IP,来完成频率计的实验. 频率计虽然小,但是也算五脏俱全,涉及到zynq的方方面面,比如 ...
- Job和Service
Job及CronJob: ---apiVersion: batch/v1kind: Jobmetadata: name: job-demospec: template: metadata: ...
- 牛客 197E 01串
大意: 给定01串, 单点修改, 询问给定区间$[l,r]$, 假设$[l,r]$从左往右得到的二进制数为$x$, 每次操作增加或减少2的幂, 求最少操作数使得$x$为0. 线段树维护2*2矩阵表示低 ...
- editormd 富文本编辑器转 html
// html <div id="markdown-view"> <textarea id="markdownView" style=&quo ...
- Qt调用VS生成的dll
预备知识: 1.如果在没有导入库文件(.lib),而只有头文件(.h)与动态链接库(.dll)时,我们才需要显示调用,如果这三个文件都全的话,我们就可以使用简单方便的隐式调用. 2.通常Windo ...