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) = 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
题目大意 (题目太简洁,不需要大意)
因为,所以最终一定会到达边界情况。
所以我们考虑如果a,b的gcd不为1,那么f(a, b - gcd(a, b))在干的事情相当于把b表示成gcd(a, b) * x的形式,每次递归就相当于就是让x减少某个数,如果设g = gcd(a, b),那么就有f(a. b) = f(a / g, b / g)。
如果a和b的gcd是1,那么我们考虑下一个和a不互质的数。这个数一定是a的某个质因子的倍数,所以我们根号大暴力将a质因数分解,然后for一遍,挨个计算不超过b的最大的是pi的倍数的数,然后继续上面的做法,递归求解。
因为当gcd不为1时,至少为2,所以递归的层数不超过层,因为a至多有log2a个不同的质因子,所以总时间复杂度为
。
Code
/**
* Codeforces
* Problem#837E
* Accepted
* Time: 15ms
* Memory: 2048k
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
typedef bool boolean;
#define smax(a, b) a = max(a, b)
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} #define LL long long template<typename T>
T gcd(T a, T b) {
return (b == ) ? (a) : (gcd(b, a % b));
} LL a, b; inline void init() {
readInteger(a);
readInteger(b);
LL g = gcd(a, b);
a /= g, b /= g;
} vector<LL> fac;
void getFactor(LL x) {
fac.clear();
for(LL i = ; i * i <= x; i++) {
if((x % i) == ) {
while((x % i) == ) x /= i;
fac.push_back(i);
}
}
if(x > ) fac.push_back(x);
} LL f(LL a, LL b) {
if(b <= ) return b;
getFactor(a);
LL near = , g;
for(int i = ; i < (signed)fac.size(); i++)
smax(near, b / fac[i] * fac[i]);
g = gcd(a, near);
return b - near + f(a / g, near / g);
} inline void solve() {
printf(Auto, f(a, b));
} int main() {
init();
solve();
return ;
}
Codeforces 837E Vasya's Function - 数论的更多相关文章
- 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 - 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)) ...
- 递推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 - 1225D]Power Products 【数论】 【分解质因数】
[CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
随机推荐
- mac系统搭建SVN版本控制
版权声明:本文为博主原创文章,未经博主允许不得转载. SmartSVN 8.6和Keygen for mac(完美破解)http://pan.baidu.com/s/1bnm72qz 一.里面包含Sm ...
- uft调用rfc接口
RFC接口函数调用: 以下代码是封装好的,为了提供给UFT工具调用,使用c#写成dll. 类型项目分成两个: 1.baseConfigModel.cs //sap配置登录信息,属性实体类 using ...
- a标签下载;页面传参row对象先转换成字符串。
jsp:添加一列 <th data-options="field:'id',width:180,formatter: rowformater" width="20% ...
- C Alyona and Spreadsheet Codeforces Round #401(Div. 2)(思维)
Alyona and Spreadsheet 这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢? [题目链接]Alyona and Spre ...
- python爬虫-基础入门-爬取整个网站《2》
python爬虫-基础入门-爬取整个网站<2> 描述: 开场白已在<python爬虫-基础入门-爬取整个网站<1>>中描述过了,这里不在描述,只附上 python3 ...
- VS编译后直接复制DLL库文件到其他目录下
项目目录:SourceCode\公共组件\KApiClient\ 要复制的目的目录: SourceCode\公共组件\DllLibrary\ApiClient 则在项目 KApiClient下添加如下 ...
- oracle数据库数值类型
---恢复内容开始--- 内容摘自网络 Oracle的数值类型有int,number,float,decimal,numberic等. NUMBER类型 定义 定义格式NUMBER (prec ...
- C++/JAVA/C#子类调用父类函数情况[留存]
时间久了就容易记不清了,特留存备用查看 c++ 1.构造函数调用 常用初始化列表 或者显示调用 1.1同一个类中构造函数调用构造函数 尽量不要这样做,因为结果不确定!避免麻烦(C++11增加 ...
- ASP.NET JSON(转http://www.360doc.com/content/14/0615/21/18155648_386887590.shtml)
概念介绍还是先简单说说Json的一些例子吧.注意,以下概念是我自己定义的,可以参考.net里面的TYPE的模型设计如果有争议,欢迎提出来探讨!1.最简单:{"total":0} t ...
- 任务调度工具 Apache Airflow 初识
参考文章: Apache Airflow (incubating) Documentation — Airflow ... 任务调度神器 airflow 之初体验 airflow 介绍 - 简书(原文 ...