Title description:
f(n) = _n x = 1x satisfies (x,n)=1g(n) = _n x = 1F (x) (satisfies x|n)
Find the value of g(n).
Solution 1:
For n<=104, the solution can be directly enumerated.
Solution two:
For n <= 107, it can be solved in two steps:
(1) First enumerate all the X satisfying x|n in n, then the problem can be transformed into finding f(x).
(2) When calculating f(x), we can consider the use of inclusion and exclusion: after obtaining all the prime factors of x, it is easy to think that if the number chosen is odd, then add its multiple sum, and if the number chosen is even, subtract its multiple sum.
For example, when x=60:
f(60)=(∑60x=1x)−t(2)−t(3)−t(5)+t(2∗3)+t(2∗5)+t(3∗5)−t(2∗3∗5)
Where t(i) denotes that I is the sum of its multiples in 1_x.
Analyzing time complexity:
Step 1 is O(2x) and X is the number of prime factors of n.
Step 2 is O(22x), but it's far less than that, so it's still possible for n to be within 107.
Solution three:
For n <= 1010, what should we do?
Back to the idea of Solution 2, when we find the value of f(x), if I know which numbers in 1_x are mutually prime with x, the sum of these numbers is the value of f(x).
Obviously, these mutually prime numbers occur in pairs, that is, if (i,x)=1, then (x_i,x)=1.
Because i+x_i=x, for each pair of numbers that are mutually prime to x, the sum is X.
Then, through a series of transformations, the problem becomes how to quickly find the number of coprimes between X and 1_x.
Now consider the function phi, which represents the number of coprimes with X in 1_x.
Then x = x (1_1p1)(1_1p2)(1_1p3)... (1_1pn) (phi(1)=1)
pi is the qualitative factor of x.
After finding the value of function phi, the problem becomes very simple.
T1 Code:
const
maxn=50000;
minn=10000;
var
a,y,l,w,g:array[0..minn] of int64;
bz:array[1..maxn] of boolean;
hash:array[1..maxn] of longint;
t,n,k,ans,len,sum,x,tt,ttt,tot,pr:int64;
i,j,p:longint;
procedure dg(k,sum:longint);
var
bz:boolean;
X:longint;
begin
if sum>n then exit;
x:=sum mod 9983;
bz:=true;
while hash[x]<>0 do
begin
if hash[x]=sum then
begin
bz:=false;
break;
end;
x:=(x+1) mod 9983;
end;
hash[x]:=sum;
if bz then
begin
inc(w[0]);
w[w[0]]:=sum;
end;
if k>l[0] then exit;
dg(k+1,sum*l[k]);
dg(k+1,sum);
end;
function phi(x:int64):int64;
var
xx,w:int64;
begin
w:=x;
k:=1;
phi:=0;
ttt:=0;
l[0]:=0;
while (x>1) and (k<=a[0]) do
begin
tt:=x;
while (x mod a[k]=0) and (k<=a[0]) do
begin
x:=x div a[k];
inc(ttt);
end;
if x<>tt then
begin
inc(phi);
inc(l[0]);
l[l[0]]:=a[k];
tot:=ttt;
pr:=a[k];
end;
inc(k);
end;
if x>1 then
begin
inc(l[0]);
l[l[0]]:=x;
end;
xx:=w;
for j:=1 to l[0] do
xx:=xx*(l[j]-1) div l[j];
exit(xx);
end;
begin
readln(t);
for i:=2 to maxn do
begin
if not bz[i] then
begin
inc(a[0]);
a[a[0]]:=i;
end;
for j:=1 to a[0] do
begin
if a[j]*i>maxn then break;
bz[a[j]*i]:=true;
if i mod a[j]=0 then break;
end;
end;
for p:=1 to t do
begin
readln(n);
x:=n;
k:=1;
l[0]:=0;
w[0]:=0;
while (x>1) and (k<=a[0]) do
begin
tt:=x;
while (x mod a[k]=0) and (k<=a[0]) do
begin
inc(l[0]);
l[l[0]]:=a[k];
x:=x div a[k];
end;
inc(k);
end;
if x>0 then
begin
inc(l[0]);
l[l[0]]:=x;
end;
fillchar(hash,sizeof(hash),0);
dg(1,1);
ans:=0;
for i:=2 to w[0] do
inc(ans,phi(w[i])*w[i] div 2);
writeln(ans+1);
end;
end.
The time complexity is close to O(sqrt(n). It is actually large.
Phi function
In fact, phi is the famous Euler function.
The application of this function is very extensive, and it is a point of knowledge that must be understood in the theory of simple numbers.
Next, I will briefly talk about:
The basic properties that are evident in PHI are:
(1) if the prime number is x,y, and Y (x y) = (x_1)(y_1)
(2) phi is a product function.
That is, phi(n)*phi(m)=phi(n m). (Obviously, as long as one of the mutually prime numbers in N and M is mutually prime with n m$)