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,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)。也就是说b剪掉了S次相同的一个GCD之后,ab有了新的GCD。新的GCD等于原来的GCD*T,可以把a、b都/T,同时GCD*T,这样问题化归为上述同样的问题,进行迭代

当B和A公约数不为1的时候(开始的时候,或者B减了一定次数1的时候),就相当于A和B同除以gcd(A,B),然后B继续一次减1。

这样只要每次计算出每次B要减多少次1才能和A有不为1的公约数。

那么预处理出A的质因数,然后每次对A的质因数判断一下,哪个最近(也就是模最小)即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
    LL x, y;
    cin >> x >> y;
    LL g = __gcd(x, y);
    x /= g, y /= g;
    vector<LL> a;
    for (LL i = 2; i * i <= x; ++ i) {
        while (x % i == 0) {
            x /= i;
            a.push_back(i);
        }
    }
    if (x > 1) a.push_back(x);
    LL ans = 0;
    while (y) {
        LL g = y;
        for (LL i : a) {
            g = min(g, y % i);
        }
        ans += g;
        y -= g;
        vector<LL> b;
        for (LL i : a) {
            if (y % i == 0) {
                y /= i;
            }
            else {
                b.push_back(i);
            }
        }
        a.swap(b);
    }
    cout << ans << endl;
}

Codeforces 837E Vasya's Function 数论 找规律

时间: 2024-08-22 22:33:07

Codeforces 837E Vasya's Function 数论 找规律的相关文章

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 | 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能除就除,不能

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 Gym 100114 A. Hanoi tower 找规律

A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description you the conditions of this task. There are 3 pivots: A, B, C. Initially, n disks of different diameter are placed on the pivot A: the smallest dis

LightOJ 13361336 - Sigma Function (找规律 + 唯一分解定理)

http://lightoj.com/volume_showproblem.php?problem=1336 Sigma Function Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1336 Description Sigma function is an interesting function in Number Theor

Codeforces Round #242 (Div. 2)C(找规律,异或运算)

一看就是找规律的题.只要熟悉异或的性质,可以秒杀. 为了防止忘记异或的规则,可以把异或理解为半加运算:其运算法则相当于不带进位的二进制加法. 一些性质如下: 交换律: 结合律: 恒等律: 归零律: 典型应用:交换a和b的值:a=a^b^(b=a); #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<

Codeforces Round #204 (Div. 2)——A找规律——Jeff and Digits

Jeff's got n cards, each card contains either digit 0, or digit 5. Jeff can choose several cards and put them in a line so that he gets some number. What is the largest possible number divisible by 90 Jeff can make from the cards he's got? Jeff must

POJ 2800 Joseph’s Problem 数论找规律

Description 求 Input 两个整数n和k(1<=n,k<=1e9) Output 输出 Sample Input 5 3 Sample Output 7 暴力超时,这样就打下表找下余数的规律.输入100,27,一下子就可以看出来,倒着的看,是一段一段的等差序列. 例如100 25 除数 1    2    3    4    5    6    7    8    9     10     11    12   13 14  15  ......25   26........ 商

CodeForces - 848A From Y to Y (找规律)

http://codeforces.com/problemset/problem/848/A 题目大意:刚开始集合里面都是单字符可认为是字符串,然后让你去合并任意两个串合并要消耗∑c=(a~z) f ( s , c ) * f ( t , c ) 的能量,其中 f ( s , c )表示字符串s中单字符c的个数.现在已知把所有字符合成一行字符串要花费的能量,问这个字符串可能的是? 解题思路:当 t 是和 s 中字符都相同的单字符时s和t组合的结果就是s的长度,对于一种字符来说增加的能量就是 l*