Codeforces 837E. Vasya's Function
题意:
- f(a, 0) = 0;
- f(a, b) = 1 + f(a, b - gcd(a, b))
- 输出f(a,b)
a=A*gcd(a,b) b=B*gcd(a,b)
一次递归后,变成了 f(A*gcd(a,b),(B-1)*gcd(a,b))
若gcd(A,(B-1))=1,那么 这一层递归的gcd(a,b)仍等于上一层递归的gcd(a,b)
也就是说,b-gcd(a,b),有大量的时间减的gcd(a,b)相同,
若计算出减了S次相同的gcd(a,b)
那就可以直接由f(a,b)到 f(a,b-S*gcd(a,b))+ S
设T是A的任意因子
那么S满足 (B-S)%T=0,且S最小
移项得 S=B%T
即S是B对A的所有因子取模得到的数中最小的那个
新的一次递归中,
a ' =a,b ' =(B-S)*gcd(a,b),gcd ' = gcd*T
如何得到T和S?
枚举所有因子会TLE
我们发现,整个过程a始终没有改变,只是参与了求gcd
对于输入的a,b
令A=a/gcd(a,b),B=b/gcd(a,b)
这样A,B 就没有公因子
这样 原来的b-S*gcd 相当于 现在的B-S
求出A的所有素因子
因为A为定值,所以下一次求S用A的素因子时,可以从上一次求S用的A的素因子剩下的里选
就是说
如果这次某个因子是B的因子,那么下一次就不会用这个因子了,B/=这个因子
即这个因子参与构成新的gcd
如果这次某个因子不是B的因子,下一次就可以考虑它
#include<vector>
#include<iostream>
using namespace std;
typedef long long LL;
LL ans;
vector<LL>p;
vector<LL> :: iterator it;
LL gcd(LL a,LL b) { return !b ? a:gcd(b,a%b); }
void work(LL y)
{
if(!y) return;
LL k=y;
for(it=p.begin();it!=p.end();++it) k=min(k,y%(*it));
ans+=k; y-=k;
vector<LL>q;
for(it=p.begin();it!=p.end();++it)
if(y%(*it)) q.push_back(*it);
else y/=(*it);
swap(p,q);
work(y);
}
int main()
{
LL a,b;
cin>>a>>b;
LL g=gcd(a,b);
a/=g; b/=g;
for(LL i=;i*i<=a;i++)
while(a%i==)
{
p.push_back(i);
a/=i;
}
if(a>) p.push_back(a);
work(b);
cout<<ans;
}
1 second
256 megabytes
standard input
standard output
Vasya is studying number theory. He has denoted a function f(a, b) such that:
- f(a, 0) = 0;
- f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b.
Vasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calculating this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly.
The first line contains two integer numbers x and y (1 ≤ x, y ≤ 1012).
Print f(x, y).
3 5
3
6 3
1
Codeforces 837E. Vasya's Function的更多相关文章
- 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 - 数论
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 ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- ural 1353. Milliard Vasya's Function(背包/递归深搜)
1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning ma ...
- ural 1353. Milliard Vasya's Function(dp)
1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning ma ...
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
- Vasya's Function CodeForces - 837E (gcd)
大意: 给定$a,b$, $1\le a,b\le 1e12$, 定义 $f(a,0)=0$ $f(a,b)=1+f(a,b-gcd(a,b))$ 求$f(a,b)$. 观察可以发现, 每次$b$一定 ...
- Codeforces 837 E Vasya's Function
Discription Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = ...
随机推荐
- [Luogu2664]树上游戏
题面戳我 sol 点分.我们面临的最主要一个问题,就是如何在\(O(n)\)的时间内算出所有LCA为根的点对的贡献,还要分别累加到它们自己的答案中去. \(num_i\):每一种颜色的数量.你可以认为 ...
- haproxy的stick table复制
在上一篇文章中,分析了haproxy的stick table特性和用法,其中特性之一也是很实用的特性是stick table支持在haproxy多个节点之间进行复制(replication). 本文仅 ...
- Java求最大公约数和最小公倍数
最大公约数(Greatest Common Divisor(GCD)) 基本概念 最大公因数,也称最大公约数.最大公因子,指两个或多个整数共有约数中最大的一个.a,b的最大公约数记为(a,b),同样的 ...
- HTTP架构介绍(1) Web服务器和代理服务器
HTTP应用协议本身是不能运行的,它需是需要架构在硬件和软件解决方案上,才能在万维网上提供高效的传输服务. 在这系列的文章中,我们将会了解到以下概念: Web服务器 代理服务器 缓存 网关.信道和中继 ...
- Java仪器数据文件解析-PDF文件
一.概述 使用pdfbox可生成Pdf文件,同样可以解析PDF文本内容. pdfbox链接:https://pdfbox.apache.org/ 二.PDF文本内容解析 File file = new ...
- [转]【安卓笔记】AsyncTask源码剖析
[转][安卓笔记]AsyncTask源码剖析 http://blog.csdn.net/chdjj/article/details/39122547 前言: 初学AsyncTask时,就想研究下它的实 ...
- 实用的Docker入门
1 Docker概述 Docker和虚拟机一样,都拥有环境隔离的能力,但它比虚拟机更加轻量级,可以使资源更大化地得到应用.首先来看Docker的架构图: 理解其中几个概念: Client(Docker ...
- Zookeeper技术分享
内容整理自组内分享PPT 一.概述 ZooKeeper 遵循一个简单的客户端-服务器模型,其中客户端 是使用服务的节点(即机器),而服务器 是提供服务的节点.ZooKeeper 服务器的集合形成了一个 ...
- jni 类初始化失败(nested exception is java.lang.NoClassDefFoundError)
nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.netease.facedetec ...
- Python进程-实现
multiprocessing模块介绍 python中的多线程无法利用CPU资源,在python中大部分计算密集型任务使用多进程.如果想要充分地使用多核CPU的资源(os.cpu_count()查看) ...