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

/*
CodeForces - 837E - Vasya‘s Function [ 数论 ]  |  Educational Codeforces Round 26
题意:
	f(a, 0) = 0;
	f(a, b) = 1 + f(a, b-gcd(a, b));
	求 f(a, b) , a,b <= 1e12
分析:
	b 每次减 gcd(a, b) 等价于 b/gcd(a,b) 每次减 1
	减到什么时候呢,就是  b/gcd(a,b)-k 后 不与 a 互质
	可先将 a 质因数分解,b能除就除,不能除就减到最近的a的因子的倍数,即模拟整个过程

	由于 a 至多只有 64个因子 (a <= 2^64) ,复杂度挺低
*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL INF = 1e18;
const int N = 1e5;
LL a, b;
map<LL, int> mp;
map<LL, int>::iterator it;
void GetFactors(LL x)
{
    for (LL i = 2; i*i <= x; i++)
    {
        if (x % i == 0)
        {
            while (x % i == 0)
            {
                mp[i]++;
                x /= i;
            }
        }
    }
    if (x != 1) mp[x] = 1;
}
int main()
{
    scanf("%lld%lld", &a, &b);
    GetFactors(a);
    LL ans = 0;
    while (b)
    {
        for (it = mp.begin(); it != mp.end(); it++)
        {
            while ( (it->second) > 0 && b % (it->first) == 0)
            {
                b /= it->first;
                --(it->second);
            }
        }
        LL mi = INF, x = -1;
        for (it = mp.begin(); it != mp.end(); it++)
        {
            if ((it->second) > 0 && b % (it->first) < mi)
            {
                mi = b % (it->first);
                x = it->first;
            }
        }
        if (x == -1)
        {
            ans += b; break;
        }
        ans += mi;
        b -= mi;
    }
    printf("%lld\n", ans);
}

  

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

时间: 2024-09-30 18:54:42

CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26的相关文章

Codeforces 837E Vasya&#39;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,b),那么b-GCD(a,b) = (B-1)*GCD(a,b),如果此时A和B-1依然互质,那么GCD不变下一次还是要执行b-GCD(a,b).那么GCD什么时候才会变化呢?就是说找到一个最小的S,使得(B-S)%T=0其中T是a的任意一个因子.变形得到:B%T=S于是我们知道S=min(B%T).也

Codeforces 837E Vasya&#39;s Function - 数论

Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b. Vasya has two numbers x and y, and he wants to calculate f(x, y).

Codeforces 837E. Vasya&#39;s Function

http://codeforces.com/problemset/problem/837/E 题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)) 输出f(a,b) a=A*gcd(a,b)    b=B*gcd(a,b) 一次递归后,变成了 f(A*gcd(a,b),(B-1)*gcd(a,b)) 若gcd(A,(B-1))=1,那么 这一层递归的gcd(a,b)仍等于上一层递归的gcd(a,b) 也就是说,b-gcd(a,b),有大量的时间减的

CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)

/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #include <bits/stdc++.h> using namespace std; const int N = 2e5+5; int a[N], b[N], c[N], n; int aa[N], bb[N]; bool cmp1(int x, int y) { return a[x] > a[y

Educational Codeforces Round 26 E - Vasya&#39;s Function

数论题还是好恶心啊. 题目大意:给你两个不超过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

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 48

Educational Codeforces Round 48 C.Vasya And The Mushrooms 思路很简单,走法有一个统一形式就是先上下走,然后到某个位置左右一个来回.然后就推一下,后边那段的递推式子,枚举改变走法的位置即可.看出做法之后发现要推个式子,于是跑去写D了...然后D一开始思路错了...凉啊 #include <bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;++i) #define per(i,a,b

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

Educational Codeforces Round 59 (Rated for Div. 2) DE题解

Educational Codeforces Round 59 (Rated for Div. 2) D. Compression 题目链接:https://codeforces.com/contest/1107/problem/D 题意: 给出一个n*(n/4)的矩阵,这个矩阵原本是一些01矩阵,但是现在四个四个储存进二进制里面,现在给出的矩阵为0~9以及A~F,表示0~15. 然后问这个矩阵能否压缩为一个(n/x)*(n/x)的矩阵,满足原矩阵中大小为x*x的子矩阵所有数都相等(所有子矩阵构