Today we'll learn about Euler functions.
Definition: Euler function is the number of numbers that are less than N and N coprime. For example φ (8) = 4, because 1,3,5,7 and 8 coprime.
The formula for Euler functions is:, wherein PI represents the mass factor of X. Special statement, φ (1) = 1.
Note: Each of the quality is only one. such as 12=2*2*3 φ (12) =12* (1-1/2) * (1-1/3) = 4.
There are a few more special ones.
1: If x is a^k, then----is coprime with him except a multiple of a.
2. If n is odd, then.
3. If n is a prime number, then all numbers less than n are coprime with him.
Prove the pit first, and then fill it out later.
Phi (x) =x (1-1/P1) at calculation .... X (1-1/PI) in (1-1/PI) can be converted to x/pi* (pi-1) to avoid the generation of decimals, while in addition to the back multiplication can avoid overflow.
For a single number x, the Euler function phi (x) is calculated as follows:
Starts at 2 to enumerate whether n is the factor of x, and if so, ans=ans/n* (n-1) and then always remove N. Since n is the factor of x, there is no occurrence of a decimal after division.
The enumeration to X has been removed until the 1 description has been completed.
Single number of Euler function implementation code:
int Euler (int x) {
int res = X,now = 2;
while (x>1) {
if (x%now==0) {
res/=now;
Res*= (now-1);
while (x%now==0) X/=now;
}
now++;
}
return res;
}
Similar to the Sieve method of prime numbers, Euler functions also have a sieve with a degree of complexity that is linear, as follows:
First the corresponding Euler value Euler[i] is initialized to itself, and then the i<= is enumerated starting at 2, and a euler[ki]=euler[ki]/i* (i-1) operation is performed for all multiples of I that are less than MAXN.
Linear Euler function implementation code:
#define MAXN 3000001 //controllable filter range 1-MAXN
int EULER[MAXN];
void Euler_init () {
euler[1]=1;
for (int i=2;i<maxn;i++)
euler[i]=i; Initialize
for (int i=2;i<maxn;i++)
if (euler[i]==i) //To determine if a prime number if Euler[i]!=i description has been performed for
(int j=i;j <maxn;j+=i)
euler[j]=euler[j]/i* (i-1);//First Division is to prevent the overflow of intermediate data
}
Just say no fake handles.
Topic Exercise:
HDU2824 Link http://acm.hdu.edu.cn/showproblem.php?pid=2824
The term is very simple, with a given interval range within the value of the Euler function, the interval does not exceed (2,3000000).
Train of thought: first linear sieve again, then use an array to save 1-n and, then each ask [l,r] direct output SUM[R]-SUM[L-1] can.
Note that you want to save with long long. But open 2 arrays. An array holds the Euler function value an array is stored and burst memory, so it needs to be optimized to save an array directly from the front and stack in an array stored in Euler's value.
Code:
//*author:handsome how *//
//************************************************************************//
#include <bits/stdc++. h>
#define MAX 3000001
typedef long LL;
using namespace std;
ll Euler[max];
void Init () {
euler[1]=1;
for (int i=2;i<max;i++)
euler[i]=i;
for (int i=2;i<max;i++)
if (euler[i]==i) for
(int j=i;j<max;j+=i)
euler[j]=euler[j]/i* (i-1);
The first Division is to prevent the overflow of intermediate data for
(int i=2;i<max;i++) euler[i]+=euler[i-1];
You can save an array of
int main ()
{
Init ()
by adding [1,i-1] and stacking to euler[i]. int a,b;
while (~SCANF ("%d%d", &a,&b)) printf ("%i64d\n", Euler[b]-euler[a-1));
return 0;
}
Here is a question about the Euler function.
Interface Xun Co Mathematics i
Time limit:1000ms Memory limit:65536k
Total submit:26 Accepted:2
Description
The recent interface bear has a difficult time learning fast power, and he wants to know the result, but when N is big, the fast power is powerless. Can you help the interface bear to work out the results?
Input
Multiple sets of test data
The first Behavior test group number T (t≤20000),
The next line of T rows consists of two integers n and mod, (0≤n≤100, 1≤mod≤100).
Output
The result of the above formula output per line
Sample Input
2
0 7
1 55
Sample Output
6
16
The answer is simple, a power function for the remainder, but the exponential ratio is large, can not be solved by fast power, here with a formula of number theory:
Which is the Euler function of C. With this knowledge of number theory we can get this problem out, pay attention to the formula requirements, X>=phi (c), so if n is relatively small we directly fast power to take the mold.
We noted that N and MoD are relatively small, speed data 2W Group, we first sieve the Euler function faster.
Code:
*author:handsome How *////************************************************************************//#include <
bits/stdc++.h> #define FUR (I,A,B) for (int i= (a); i<= (b), i++) #define FURR (I,A,B) for (int i= (a); i>= (b); i--)
using namespace Std;
typedef long Long LL; inline void gn (long long&x) {int Sg=1;char c;while ((C=getchar ()) < ' 0 ' | | C> ' 9 ') &&c!= '-'); c== '-'?
(sg=-1,x=0):(x=c-' 0 ');
while ((C=getchar ()) >= ' 0 ' &&c<= ' 9 ') x=x*10+c-' 0 '; x*=sg; } inline void gn (INT&X) {Long long t;gn (t); x=t} inline void gn (unsigned long long&x) {Long long t;gn (t); x=t;} Inlin e void gn (DOUBLE&X) {double t;scanf ("%lf", &t); x=t;} inline void gn (Long double&x) {double t;scanf ("%lf")
&T); x=t;}
----------------------------------------------------------const int MAXN = 100+5;
int euler[maxn+5]; void Euler () {fur (I,1,MAXN) euler[i] =I
Fur (I,2,MAXN) if (euler[i]==i) for (int j = i;j<100;j+=i) euler[j] = euler[j]/i* (i-1);
int powmod (int a,int x,int mod) {int ret = 1;
while (x) {if (x&1) ret = ret * a% mod;
A = a * a% mod;
x>>=1;
return ret;
int main () {Euler ();
int T;
GN (T);
int ans;
while (t--) {int n,md;
GN (n);
GN (MD);
if (n<=3) {//n Relatively small direct fast power modulo int x = 1;
Fur (i,1,n) x*=6;
Ans = powmod (6,X,MD);
} else{int x = Powmod (6,N,EULER[MD]) + EULER[MD];
Ans = powmod (6,X,MD);
printf ("%d\n", ans);
return 0; }
Not to be continued ...