大意: 给定$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. mysql测试记录

    一直觉得mysql的Memony内存引擎挺好,其他数据库都没有,正好有空,所以试试. 版本:mysql-installer-community-8.0.17.0 os:windows10 SSD硬盘, ...

  2. java设计模式简述

    1.代理模式:有一个接口或者顶层类(可以是抽象的)A,一个实现类B,一个代理类C,代理类C之所以能够是代理类,是因为1.C也实现了A.2.C持有A的依赖,用来注入真实的实现B.3.C的实现方法中实际调 ...

  3. 启动maven项目时报错Failed to start component [StandardEngine[Tomcat]]: A child container failed during start

    详细错误信息:Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run (default-cli) on ...

  4. MapReduce Combiner

    Combiner编程(可选步骤,视情况而定!) combiner最基本是实现本地key的归并,combiner具有类似本地的reduce功能. 如果不用combiner,那么所有的结果都是reduce ...

  5. Spring Security整合JWT,实现单点登录,So Easy~!

    前面整理过一篇 SpringBoot Security前后端分离,登录退出等返回json数据,也就是用Spring Security,基于SpringBoot2.1.4 RELEASE前后端分离的情况 ...

  6. 安装Rabbitmq脚本

    安装RabbitMQ时需要先安装erlang插件 [root@ZHONG-LONG javascripts]# vim -erlang.sh #!/bin/sh ######安装erl插件 PRO=/ ...

  7. Linux下使用 Nginx

    1. 下载Nginx 下载地址 下载完成后,上传到linux服务器,我上传到了 /opt文件夹下. 2. 安装Nginx前的准备 要想使用Nginx需要安装PCRE库和zlib库,否则直接安装Ngin ...

  8. 免费下载 SetupVPN CRX 3.7.0 for Chrome OR QQ浏览器

    免费下载 SetupVPN CRX 3.7.0 for Chrome OR QQ浏览器 Lifetime Free VPN(微劈嗯) 下载setupvpn 3.7.0的crx文件, 打开chrome的 ...

  9. 前端 - 轮询, 长轮训, websocket

    轮询 概念 ajax 轮询的原理很简单,让浏览器间隔几秒就发送一次请求,询问服务器是否有新信息 代码 function showUnreadNews() { $(document).ready(fun ...

  10. mac 终端杀进程

    killall 进程名 killall chromedriver 查看进程名的·方法: