数论题还是好恶心啊。

题目大意:给你两个不超过1e12的数 x,y,定义一个f ( x, y ) 如果y==0 返回 0 否则返回1+ f ( x , y - gcd( x , y ) );

思路:我们设gcd ( x , y) 为G,那么 设 x  = A*G,y = B*G,我们考虑减去多少个G时x y 的gcd会改变,我们设减去

k个G的时候 x和y 的gcd为改变,即 A*G 和 ( B - k ) * G 的 gcd 改变了,什么情况下会改变呢,就是A 和( B -  k )的gcd

不为 1 ,这样我们就不断地找出 k 就能得出答案。那么现在问题变成了怎么快速地求出 k ,我们设 A 中的某个因子为

r,则( B - k )%r == 0 , 设( B - k ) / r的商为 q ,则 B - k = q * r  变形一下变成,B = k + q * r ,两边对 r  取余,B % r = k

这样我们就能用 B 和 A中的质因数求出 k ,那么我们每次需要记录的就是,B 是 A和B的gcd 的多少倍就,以及A的没有被

用过的质因数。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a,b,ans=;
vector<ll> va;
void work(ll y)// y值表示目前 b的值为 gcd(a,b)的y倍。
{
if(y==) return;
ll k=y;
for(int i=;i<va.size();i++)
{
k=min(k,y%va[i]);
}
ans+=k;y-=k;
vector<ll> vb;
for(int i=;i<va.size();i++)
{
if(y%va[i]==) y/=va[i];
else vb.push_back(va[i]);//没有用过的a的质因数。
}
va=vb;
work(y);
}
int main()
{
cin>>a>>b;
ll gcd=__gcd(a,b);
a/=gcd;b/=gcd;//保证了a和b没有公共的因子。
for(ll i=;i*i<=a;i++)
{
while(a%i==)
{
va.push_back(i);
a/=i;
}
}
if(a>) va.push_back(a);
for(int i=;i<va.size();i++) printf("%d ",va[i]);
work(b);
cout<<ans<<endl;
return ;
}

Educational Codeforces Round 26 E - Vasya's Function的更多相关文章

  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. Educational Codeforces Round 26

    Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...

  3. CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

    按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...

  4. CodeForces 837D - Round Subset | Educational Codeforces Round 26

    /* CodeForces 837D - Round Subset [ DP ] | Educational Codeforces Round 26 题意: 选k个数相乘让末尾0最多 分析: 第i个数 ...

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

  6. Educational Codeforces Round 13 D. Iterated Linear Function 水题

    D. Iterated Linear Function 题目连接: http://www.codeforces.com/contest/678/problem/D Description Consid ...

  7. Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/678/D 简单的矩阵快速幂模版题 矩阵是这样的: #include <bits/stdc++.h&g ...

  8. Educational Codeforces Round 26 B,C

    B. Flag of Berland 链接:http://codeforces.com/contest/837/problem/B 思路:题目要求判断三个字母是否是条纹型的,而且宽和高相同,那么先求出 ...

  9. Educational Codeforces Round 54 E. Vasya and a Tree(树上差分数组)

    https://codeforces.com/contest/1076/problem/E 题意 给一棵树(n<=3e5),m(3e5)次查询,每次查询u,d,x,表示在u的子树中,给距离u&l ...

随机推荐

  1. Regex实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. Sqoop异常:Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject

    18/12/07 01:09:03 INFO mapreduce.ImportJobBase: Beginning import of staffException in thread "m ...

  3. D- 泛型练习 ,继承,方法

    unit Unit3; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  4. Android APP常见的5类内存泄露及解决方法

    1.static变量引起的内存泄漏 因为static变量的生命周期是在类加载时开始 类卸载时结束,也就是说static变量是在程序进程死亡时才释放,如果在static变量中 引用了Activity 那 ...

  5. js实现获取两个日期之间所有日期最简单的方法

    Date.prototype.format = function() { var s = ''; var mouth = (this.getMonth() + 1)>=10?(this.getM ...

  6. Java InputStream 、 InputStreamReader和BufferedReader

    https://blog.csdn.net/zgljl2012/article/details/47267609 在Java中,上述三个类经常用于处理数据流,下面介绍一下三个类的不同之处以及各自的用法 ...

  7. oracle新建对象 权限管理

    代码 CREATE USER target IDENTIFIED BY target ; GRANT CONNECT, RESOURCE TO target; 刚刚创建的oracle实例中会内建两个用 ...

  8. Linux内核调试方法【转】

    转自:http://www.cnblogs.com/shineshqw/articles/2359114.html kdb:只能在汇编代码级进行调试: 优点是不需要两台机器进行调试. gdb:在调试模 ...

  9. shell脚本收集服务器基本信息并入库

    # cat check_all.sh #!/bin/bash #create by hexm @2016.6 #date=`date +%Y%m%d` #DIST_FILE=/app/healthch ...

  10. 京东在html5页面中打开本地app的解决方案

    转:https://blog.csdn.net/CameloHuang/article/details/64476385 从html5打开本地的app–如果本地没有app就跳转到下载页面,大家都会认为 ...