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) = ...
随机推荐
- Nginx负载均衡——基础功能
熟悉Nginx的小伙伴都知道,Nginx是一个非常好的负载均衡器.除了用的非常普遍的Http负载均衡,Nginx还可以实现Email,FastCGI的负载均衡,甚至可以支持基于Tcp/UDP协议的各种 ...
- Git基本命令 -- 创建Git项目
在这里下载git:https://git-scm.com/ 安装的时候, 如果是windows系统的话, 可以勾选unix的命令行工具, 这样在windows命令行下会多出很多命令, 例如ls. Gi ...
- Android Studio 封装的类的继承
有个封装好的Firebase.java文件,放到项目中直接使用就可以,这个需要继承一个AbstractFirebase类,在广告代码中,可以等到加广告的时候来加这个文件. 这个地方的继承,因为是ads ...
- Win10下Ubuntu bash上手
第一次发表博客,可能写的不是很好,希望大家谅解! 今天咱们来上手一下Windows10下的bash,首先这款bash是基于Ubuntu操作系统的一个移植,也是方便开发和学习Linux中的shell命令 ...
- NancyFX 第八章 内容协商
在Web框架的范畴内,一切都是基于REST的-- 从返回包含CSS.JavaScript的网页的路由路径,到那些返回JSON数据的URL. 无论你怎么看它,两者都是必须的.我们使用一组URL来呈现UR ...
- 基于etcd的Rabbitmq队列订阅负载均衡
go-qb Load balancer for rabbitmq queue subscribing Feature Rabbitmq queue subscription load balancin ...
- tensorflow实现最基本的神经网络 + 对比GD、SGD、batch-GD的训练方法
参考博客:https://zhuanlan.zhihu.com/p/27853521 该代码默认是梯度下降法,可自行从注释中选择其他训练方法 在异或问题上,由于训练的样本数较少,神经网络简单,训练结果 ...
- Activity 与 springMvc相整合
准备环境: springMvc框架及Activity所需要的jar: 创建spring-activity.xml文件,里面内容: <?xml version="1.0" en ...
- centos6.x上安装Java-1.8.0
author : headsen chen date : 2017-12-04 10:32:44 notice :This article is created by headsen chen h ...
- 边做边学入门微信小程序之仿豆瓣评分
微信小程序由于适用性强.逻辑简要.开发迅速的特性,叠加具有海量活跃用户的腾讯公司背景,逐渐成为了轻量级单一功能应用场景的较佳承载方式,诸如电影购票.外卖点餐.移动商城.生活服务等场景服务提供商迅速切入 ...