大意: 给定$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)的更多相关文章

  1. CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26

    /* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...

  2. 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)) ...

  3. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  4. 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 ...

  5. 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 ...

  6. 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) = ...

  7. 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 ...

  8. 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) =  ...

  9. 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]代表已经选择 ...

随机推荐

  1. Docker容器数据券

    1.是什么? 2.能干嘛? 3.数据卷 ——容器内添加 方式一:直接命令添加 1).命令 2).在宿主机上新建并添加内容 3).查看容器内相应共享文件夹,发现宿主机的文件夹下发生变化,会同步到容器内的 ...

  2. ROS Topic 常用指令

    rostopic list rosnode list一樣,就是列出目前運行中的topic有哪些. rostopic echo <topic_name> 接下來這個指令比較重要啦,就是去監聽 ...

  3. 美国药品销售额top200

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&ut ...

  4. postgresql之 drop & delete & truncate

    官网:https://www.postgresql.org/docs/8.1/sql-droptable.html Name DROP TABLE -- remove a table Synopsis ...

  5. kubernetes监控(12)

    一.Weave Scope 1. weave scope 容器地图 创建 Kubernetes 集群并部署容器化应用只是第一步.一旦集群运行起来,我们需要确保一起正常,所有必要组件就位并各司其职,有足 ...

  6. eNSP V100R002C00B500 Setup模拟CE6800

    本人操作系统:Windows7  64 下载需要的安装包: eNSP V100R002C00B500    官网下载地址:点击这里 CE6800 官网下载地址:点击这里 USG6000V        ...

  7. 不能在jsp页面<c:choose>对标签中使用<!---->进行注释

    jsp页面<c:choose>标签中使用<!---->进行注释,访问出现异常,如下: 标签范围:

  8. checklist和基础安全知识

    checklist和基础安全知识 https://book.yunzhan365.com/umta/rtnp/mobile/index.html 网络安全科普小册子 http://sec.cuc.ed ...

  9. Elasticsearch删除数据操作,你必须知道的一些坑

    前两天有同事打电话问我,说ES删除数据有没有什么坑? 我当时就问,是删索引还是删索引里的数据?她回答说是删数据,我说查出这些数据直接删除就好了,没有什么坑... 后来想想,关于ES数据的删除,之前确实 ...

  10. The request was rejected because the URL contained a potentially malicious String ";"报错解决

    报错信息 浏览器中看到的报错 错误摘要: The request was rejected because the URL contained a potentially malicious Stri ...