• CodeForces


    /*
    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
    ", ans);
    }
    

      

    我自倾杯,君且随意
  • 相关阅读:
    pronunciation techniques
    contraction prnounciation
    洛谷P2097 资料分发1 题解
    洛谷P1482 Cantor表(升级版) 题解
    洛谷P1615 西游记公司 题解
    洛谷P2676 超级书架 题解
    洛谷P1554 梦中的统计 题解
    洛谷P1720 月落乌啼算钱 题解
    洛谷P1420 最长连号 题解
    洛谷P1634 禽兽的传染病 题解
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/7293581.html
Copyright © 2020-2023  润新知