Product function and linear sieve
Linear sieve prime number
Ensure that each number will only be screened by its minimum quality factor (different from that each number in the e-sieve will be screened by all its quality factors to make the complexity too high)
int pri[N],tot,zhi[N];//A representation of 1 is not a prime number void sieve() { zhi[1]=1; for (int i=2;i<=n;i++) { if (!zhi[i]) pri[++tot]=i; for (int j=1;j<=tot&&i*pri[j]<=n;j++) { zhi[i*pri[j]]=1; if (i%pri[j]==0) break; } } }
All linear sieve product functions must be based on linear sieve prime numbers.
Linear sieve Mobius function
int mu[N],pri[N],tot,zhi[N]; void sieve() { zhi[1]=mu[1]=1; for (int i=2;i<=n;i++) { if (!zhi[i]) pri[++tot]=i,mu[i]=-1; for (int j=1;j<=tot&&i*pri[j]<=n;j++) { zhi[i*pri[j]]=1; if (i%pri[j]) mu[i*pri[j]]=-mu[i]; else {mu[i*pri[j]]=0;break;} } } }
Linear sieve Euler function
int phi[N],pri[N],tot,zhi[N]; void sieve() { zhi[1]=phi[1]=1; for (int i=2;i<=n;i++) { if (!zhi[i]) pri[++tot]=i,phi[i]=i-1; for (int j=1;j<=tot&&i*pri[j]<=n;j++) { zhi[i*pri[j]]=1; if (i%pri[j]) phi[i*pri[j]]=phi[i]*phi[pri[j]]; else {phi[i*pri[j]]=phi[i]*pri[j];break;} } } }
Number of linear sieve approximations
d(i) is the number of divisors of i
d(i)=∏ki=1(ai+1)
Maintain the number of times that the minimum value factor of each number appears (i.e. a1)
int d[N],a[N],pri[N],tot,zhi[N]; void sieve() { zhi[1]=d[1]=1; for (int i=2;i<=n;i++) { if (!zhi[i]) pri[++tot]=i,d[i]=2,a[i]=1; for (int j=1;j<=tot&&i*pri[j]<=n;j++) { zhi[i*pri[j]]=1; if (i%pri[j]) d[i*pri[j]]=d[i]*d[pri[j]],a[i*pri[j]]=1; else {d[i*pri[j]]=d[i]/(a[i]+1)*(a[i]+2);a[i*pri[j]]=a[i]+1;break;} } } }
Sum of linear sieve divisors
On the sum of the divisor of σ (i) for i
σ(i)=∏ki=1(∑aij=0pji)
Maintain low(i) as the exponential power of the minimum quality factor of i, that is, pa11, sum(i) as the contribution of the minimum quality factor of i to the answer, that is, ∑ a1j=0pj1
(this thing may explode int, I don't care so much here.)
int low[N],sum[N],sigma[N],pri[N],tot,zhi[N]; void sieve() { zhi[1]=low[1]=sum[1]=sigma[1]=1; for (int i=2;i<=n;i++) { if (!zhi[i]) low[i]=pri[++tot]=i,sum[i]=sigma[i]=i+1; for (int j=1;j<=tot&&i*pri[j]<=n;j++) { zhi[i*pri[j]]=1; if (i%pri[j]==0) { low[i*pri[j]]=low[i]*pri[j]; sum[i*pri[j]]=sum[i]+low[i*pri[j]]; sigma[i*pri[j]]=sigma[i]/sum[i]*sum[i*pri[j]]; break; } low[i*pri[j]]=pri[j]; sum[i*pri[j]]=pri[j]+1; sigma[i*pri[j]]=sigma[i]*sigma[pri[j]]; } } }
General product function of linear sieve
If we want to filter out the product function f(x) linearly, we must be able to quickly calculate the following function values:
1,f(1)
2. f § (p is prime)
3. f(pk) (p is prime)
In fact, it is the function value of all numbers with prime factor number less than or equal to 1.
The definition of the above-mentioned function values will be given for the common product functions. For a custom product function (such as Dirichlet convolution), we need to calculate the value of the above function.
We assume that we have completed the calculation of the above function values, and now we need to screen out all the function values corresponding to the numbers containing at least two prime factors.
Obviously, a number with at least two prime factors can be decomposed into the product of two coprime numbers which are not 1. At this point, we can use f (x y) = f (x) f (y) to calculate the corresponding function value.
The following content needs to fully understand the linear sieve prime number above.
In the process of screening, i * prij will be screened by multiplying i by prij.
If i is uniquely decomposed to pa11pa22 pakk, there must be prij < = P1.
This doesn't need to be proved, because when prij=p1, it break s.
i f prij < P1, then prij and i are mutually prime, which can directly f(i * prij)=f(i) * f(prij)
If prij=p1, then we need to record a lowi for i, which is the exponential power of the minimum value factor in i, that is, lowi=pa11 (the pa11 in the unique decomposition).
If i is used to get rid of lowi, the minimum quality factor in the result must be greater than p1, and because prij=p1, it can be seen that gcd(i/lowi,lowi * prij)=1. Then f(i * prij)=f(i/lowi) * f(lowi * prij)
Note that when lowi=i, it means that the number is the power of a prime number, which requires the special definition above.
void sieve() { zhi[1]=low[1]=1; f[1]=Yes1Direct definition; for (ll i=2;i<=n;i++) { if (!zhi[i]) low[i]=pri[++tot]=i,f[i]=Direct definition of prime number; for (ll j=1;j<=tot&&i*pri[j]<=n;j++) { zhi[i*pri[j]]=1; if (i%pri[j]==0) { low[i*pri[j]]=low[i]*pri[j]; if (low[i]==i) f[i*pri[j]]=Defining several powers of prime number(Generally by f[i]Recurrence); else f[i*pri[j]]=f[i/low[i]]*f[low[i]*pri[j]]; break; } low[i*pri[j]]=pri[j]; f[i*pri[j]]=f[i]*f[pri[j]]; } } }
in addition
For a function ∑ d| x f (d) g (XD) in the form of Dirichlet convolution, if f(x) or g(x) is not a product function, when the data range is small (e.g. 106), violent screening can be considered, that is, enumerating a d to calculate which X can be contributed, and the complexity is O(∑ ni=1 ⌊ ni ⌋), that is, the complexity of the sieve. If the data range is large (107 a sieve can't run), some related properties of this function need to be considered.