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$一定是减去若干个相同的$gcd$, 并且每次减的$gcd$一定是递增的, 并且一定是在$gcd$最接近$b$的时候开始减, 可以预处理出所有这样的位置, 然后模拟.
#include <iostream>
#include <cstdio>
#include <math.h>
#include <queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define pb push_back
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll x,y;
vector<ll> fac, q;
int main() {
cin>>x>>y;
int mx = sqrt(x+0.5);
REP(i,1,mx) if (x%i==0) {
fac.pb(i);
if (x/i!=i) fac.pb(x/i);
}
for (ll t:fac) q.pb(y/t*t);
sort(q.begin(),q.end(),greater<ll>());
q.erase(unique(q.begin(),q.end()),q.end());
q.erase(q.begin()),q.pb(0);
ll now = y, ans = 0;
for (ll t:q) {
ll g = gcd(x,now);
if ((now-t)%g==0) {
ans += (now-t)/g;
now = t;
}
}
cout<<ans<<endl;
}
Vasya's Function CodeForces - 837E (gcd)的更多相关文章
- 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 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 837 E Vasya's Function
Discription Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = ...
- Educational Codeforces Round 26 [ D. Round Subset ] [ E. Vasya's Function ] [ F. Prefix Sums ]
PROBLEM D - Round Subset 题 OvO http://codeforces.com/contest/837/problem/D 837D 解 DP, dp[i][j]代表已经选择 ...
随机推荐
- CF1197B
CF1197B 题意: 出n个柱子,每个柱子一个圆盘,其半径各不相同,每次只能将柱子上只有一个圆盘的移到相邻位置,问能否全部移到一个柱子上. 解法: 思路题. 如果所有盘子都能移动到同一个柱子上,那么 ...
- CSP2019懵逼记
CSP2019 考场二日游 CJ 旅游团 本来我是准备咕掉的, 但是被强 ♂ 烈要求更博了 Day -INF ~ Day -1 专题巩固和联考 前面半个月疯狂爆炸 后面半个月状态恢复了, 考得还行 联 ...
- redis几种模式的部署(Windows下实现)
<参考>http://www.cnblogs.com/ruiati/p/6374152.html 1. 自行下载redis客户端.redis官方不支持Windows系统,所以官网上是下 ...
- pycham更换主题
在pycham的file-->Setting-->Editor-->Colors&Fonts
- oracle查找表索引信息
select owner,index_name,index_type from all_indexes where owner='xxxx' and table_name='xxx' select * ...
- jinja2-宏,include, import
一 宏 宏类似常规编程语言中的函数.它们用于把常用行为作为可重用的函数,取代 手动重复的工作.如果宏在不同的模板中定义,你需要首先使用 import,比如 {% macro input(name, v ...
- 营销H5项目-BugList+解决方案+方法
作者会持续更新,后续会整合SF.gg上 其他小伙伴整理的资料 动态改变微信title var $body = $('body'); document.title = '五班老同学(35)'; var ...
- poj1734
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9078 Accepted: 3380 ...
- python脚本实现药品名自动翻译2
python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&ut ...
- 网络编程三要素之IP
用来标示我们计算机在互联网上的唯一性 每个设备在网络中的唯一标识 每台网络终端在网络中都有一个独立的地址,我们在网络中传输数据就是使用这个地址. ipconfig:查看本机IP192.168.12.4 ...