cf edu81 D math

A strange, this is not BCD is sprouting new. First posted a D bar. It does not appear to be difficult but I will not. Read only solution to a problem almost understand. Perhaps there is a simpler explanation?

The meaning of problems: To you ask how many x, such that for a given a and m, (a, m) = (a + x, m), here 1 <= a <m <10 ^ 10

The answer is m / (a, m) is the Euler function value

First, (a + x, m) = ((a + x)% m, m). Because if a + x> m, then (a + x, m) = (a + xm, m) = ((a + x)% m, m), and if a + x <= m clearly

So the problem and minimize the number of x '= (a + x)% m, 0 <= x' <m, such that (a, m) = (x ', m)

And 'to give (, m (x a d = (a, m) = x)' / d, m / d) = 1, and the above each step can be thrust reverser ( '/ d available x' of x, the x 'available x), therefore for m / d Euler function values ​​on the line

The following code posted

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll gcd(ll a,ll b)
{
	if (b==0) return a;
	else return gcd(b,a%b);
}
int main()
{
	ll a,m,i,ans,n,t;
	cin>>t;
	while (t--)
	  {
	  	cin>>a>>m;
	  	n=m/gcd(a,m);ans=n;
	  	for (i=2;i<=int(sqrt(n));i++)
	  	  {
	  	  	if (n%i==0) ans=ans/i*(i-1); //先除以i再乘i-1,避免爆掉long long 
	  	  	while (n%i==0) n/=i;
			}
		if (n>1) ans=ans/n*(n-1);
		cout<<ans<<endl;
	  }
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/edmunds/p/12296381.html