CodeForces - 1295D Same GCDs (Euler function)

Topic links: Click here

The number of x is given two positive integers a and m, then x is given range [0, m), it is now required to satisfy gcd (a, m) = gcd (a + x, m): subject to the effect

Title Analysis: Because the Euclidean definition: gcd (a, b) = gcd (b, a mod b) shows that, when i ∈ [a, m], and i ∈ [m, 2 * m], gcd (i , m) i is equal to the same number, because each time the number of the second modulo therefore has periodicity, and the subject is required we find i ∈ [a, m + a -1] at a number, since the length of a complete cycle, we may wish to convert their problems i ∈ [0, m], the number satisfying gcd (i, m) = = gcd (a, m) is

The remaining will be able to solve the Euler function:

\sum_{i=1}^{n}[gdc(i,n)=d]=\sum_{i=1}^{n}[gdc(\frac{i}{d},\frac{n}{d})=1]=\phi (\frac{n}{d})

I.e. seeking m / gcd (a, m) is a function of the Euler

Code:

#include<iostream>
#include<cstdio> 
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;
 
typedef long long LL;
 
const int inf=0x3f3f3f3f;

const int N=1e5+100;

LL eular(LL n)
{
    LL ans=n;
    for(LL i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            ans=ans/i*(i-1);
            while(n%i==0)
                n/=i;
        }
    }
    if(n>1) 
		ans=ans/n*(n-1);
    return ans;
}

int main()
{
//	freopen("input.txt","r",stdin);
//	ios::sync_with_stdio(false);
	int w;
	cin>>w;
	while(w--)
	{
		LL a,m;
		scanf("%lld%lld",&a,&m);
		m/=__gcd(a,m);
		printf("%lld\n",eular(m));
	}
 
 
 
 
 
	
	
	
	
	
	
	
	
	
	return 0;
}

 

Published 577 original articles · won praise 18 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_45458915/article/details/104114132