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,b),那么b-GCD(a,b) = (B-1)*GCD(a,b),如果此时A和B-1依然互质,那么GCD不变下一次还是要执行b-GCD(a,b)。那么GCD什么时候才会变化呢?就是说找到一个最小的S,使得(B-S)%T=0其中T是a的任意一个因子。变形得到:B%T=S于是我们知道S=min(B%T)。也就是说b剪掉了S次相同的一个GCD之后,ab有了新的GCD。新的GCD等于原来的GCD*T,可以把a、b都/T,同时GCD*T,这样问题化归为上述同样的问题,进行迭代
当B和A公约数不为1的时候(开始的时候,或者B减了一定次数1的时候),就相当于A和B同除以gcd(A,B),然后B继续一次减1。
这样只要每次计算出每次B要减多少次1才能和A有不为1的公约数。
那么预处理出A的质因数,然后每次对A的质因数判断一下,哪个最近(也就是模最小)即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
LL x, y;
cin >> x >> y;
LL g = __gcd(x, y);
x /= g, y /= g;
vector<LL> a;
for (LL i = ; i * i <= x; ++ i) {
while (x % i == ) {
x /= i;
a.push_back(i);
}
}
if (x > ) a.push_back(x);
LL ans = ;
while (y) {
LL g = y;
for (LL i : a) {
g = min(g, y % i);
}
ans += g;
y -= g;
vector<LL> b;
for (LL i : a) {
if (y % i == ) {
y /= i;
}
else {
b.push_back(i);
}
}
a.swap(b);
}
cout << ans << endl;
}
Codeforces 837E Vasya's Function 数论 找规律的更多相关文章
- 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 | Educational Codeforces Round 26
/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...
- 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)) ...
- 【BZOJ1432】[ZJOI2009]Function(找规律)
[BZOJ1432][ZJOI2009]Function(找规律) 题面 BZOJ 洛谷 题解 这...找找规律吧. #include<iostream> using namespace ...
- LightOJ 13361336 - Sigma Function (找规律 + 唯一分解定理)
http://lightoj.com/volume_showproblem.php?problem=1336 Sigma Function Time Limit:2000MS Memory L ...
- Codeforces Gym 100114 A. Hanoi tower 找规律
A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...
- codeforces Gym 100418D BOPC 打表找规律,求逆元
BOPCTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- Codeforces Round #242 (Div. 2)C(找规律,异或运算)
一看就是找规律的题.只要熟悉异或的性质,可以秒杀. 为了防止忘记异或的规则,可以把异或理解为半加运算:其运算法则相当于不带进位的二进制加法. 一些性质如下: 交换律: 结合律: 恒等律: 归零律: 典 ...
- 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)
题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...
随机推荐
- golang depth read map
Foreword: I optimized and improved the below solution, and released it as a library here: github.com ...
- Spring Cloud Bus 自动更新配置
---恢复内容开始--- Spring Cloud Config 结合 Spring Cloud bus 实现 git 仓库提交配置文件 触发消息队列 应用自动更新配置 1. config 服务端 添 ...
- Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder)
Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...
- 【C/C++】对于可重入、线程安全、异步信号安全几个概念的理解
由于前段时间,程序偶尔异常挂起不工作,检查后发现时死锁了,原因就是:在信号处理函数里面调用了fprintf. printf等io函数是需要对输出缓冲区加锁,这类函数对本身是线程安全的,但是对信号处理函 ...
- 利用docker搭建本地私有镜像仓库
主机名 角色 sht-sgmhadoopcm-01 Docker Repository sht-sgmhadoopnn-01 Docker Client 1. 在两台节点分别安装docker http ...
- [转帖]一张图让你看懂InnoDB
一张图让你看懂InnoDB 2018年05月10日 10:02:34 灵魂自由的忙人 阅读数 299 https://blog.csdn.net/xiaoyi23000/article/details ...
- Oracle 用户管理权限
Oracle 用户管理权限 一.创建用户的Profile文件 SQL> create profile student limit // student为资源文件名 FAILED_LOGIN_AT ...
- swift MT报文解析处理
swift 官方资料:https://www2.swift.com/knowledgecentre/publications/us5mc_20180720/2.0?topic=alec.htm#gen ...
- Tomcat 设置80端口
1:修改tomcat配置 vi /usr/local/tomcat/conf/server.xml 找到 Connector port="8080" protocol=" ...
- workerman 实践 及 不能多人连接的问题
官网:https://www.workerman.net/ 手册地址:https://www.workerman.net/doc 追加内容: 请在开发前多读读 开发必读http://doc.worke ...