Educational Codeforces Round 81 (Rated for Div 2.) - D. Same GCDs - Euler function expanding European +

Title link: https: //codeforces.com/contest/1295/problem/D
Title effect:
various embodiments
give you a a and m. Ask how many x (0 <= x <m ) such that gcd (a, m) = gcd (a + x, m).
Here Insert Picture Description
Ideas: The expanded European Theorem:
GCD (A + X, m) = GCD ((A + X)% m, m)
(A + X)% of the value of m may be [0, m-1].
GCD (A + X, m) = GCD (A, m)
. 1: If gcd (a, m) = 1 . It is the desire gcd (X, m) = 1 (0 <= X <m) X = 0 is not satisfied. M is the Euler function.
2:! If GCD = gcd (a, m) = 1. It is the desire gcd (X / GCD, m / GCD) = 1 (0 / GCD <= X / GCD <m / GCD) X = 0 / GCD satisfied. It is the Euler function m / GCD of.

#include<bits/stdc++.h>
#define LL long long
using namespace std;
 
LL euler(LL n)
{
    LL res=n;
    for(LL i=2;i*i<=n;i++)
    {
        if(n%i==0)//第一次找到的必为素因子
            res=res/i*(i-1);
        while(n%i==0)//把该素因子全部约掉
            n/=i;
    }
    if(n>1)
        res=res/n*(n-1);
 
    return res;
}
 
int main()
{
    int T;
    scanf("%d", &T);
    while(T--){
        LL a, m;
        scanf("%lld%lld", &a, &m);
        if(__gcd(a,m)==1){
            printf("%lld\n", euler(m));
        }
        else{
            printf("%lld\n", euler(m/__gcd(a, m)));
        }
    }
    return 0;
}
Published 384 original articles · won praise 22 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_21433411/article/details/104114517