大意: 给定$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. 微信小程序开发-踩坑

    异步请求处理 详情描述: 微信小程序的wx.request({})请求时异步处理,以下代码 wx.reuest({ url:"https://XXXA", method:" ...

  2. 解决IntelliJ无法导入maven包的问题

    使用如下的pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...

  3. Linux设备驱动程序 之 kmalloc

    原型 kmalloc的原型如下: void *kmalloc(size_t size, gfp_t flags) 第一个参数是要分配的块的大小,第二个参数是分片标志: flags标志 最常用的标志是G ...

  4. java课后实验性问题2

    课后作业一:计算组合数 程序设计思想: 从键盘获取组合数,判断是否构成组合数.分别用三种方法计算组合数输出. 程序流程图: import java.util.Scanner; public class ...

  5. count(1) 与 count(*) 比较

    1.  count(1) and count(*) 当表的数据量大些时,对表作分析之后,使用count(1)还要比使用count(*)用时多了! 从执行计划来看,count(1)和count(*)的效 ...

  6. java中json的使用和解析

    1.创建json对象 1.1 创建JSONObject对象 使用map初始化json @Test public void test1() { Map<String, Object> map ...

  7. SQL-W3School-高级:SQL NULL 值

    ylbtech-SQL-W3School-高级:SQL NULL 值 1.返回顶部 1. NULL 值是遗漏的未知数据. 默认地,表的列可以存放 NULL 值. 本章讲解 IS NULL 和 IS N ...

  8. 用jeecg做个项目第二讲(Datagrid数据列表效果详解)

    1.列表界面 2.流程状态的效果 <t:dgCol title="流程状态" field="bpmStatus" queryMode="sing ...

  9. asp.net MVC AngularJS

    http://www.cnblogs.com/powertoolsteam/category/834105.html

  10. 灵活配置tomcat根目录网站

    <Host name="localhost" appBase="webapps" unpackWARs="true" autoDepl ...