当前位置:网站首页>Number theoretic function and its summation to be updated

Number theoretic function and its summation to be updated

2022-07-05 04:45:00 Strezia

Mainly record the relevant topics , Skip knowledge explanation .

Example 1 Number theory is divided into blocks

P3935 Calculating

Title Description

if x x x The result of decomposing the prime factor is x = p 1 k 1 p 2 k 2 ⋯ p n k n x=p_1^{k_1}p_2^{k_2}\cdots p_n^{k_n} x=p1k1p2k2pnkn, Make f ( x ) = ( k 1 + 1 ) ( k 2 + 1 ) ⋯ ( k n + 1 ) f(x)=(k_1+1)(k_2+1)\cdots (k_n+1) f(x)=(k1+1)(k2+1)(kn+1), seek ∑ i = l r f ( i ) \sum_{i=l}^rf(i) i=lrf(i) Yes 998   244   353 998\,244\,353 998244353 Results of die taking .
Time complexity O ( n ) O(\sqrt n) O(n).

Ideas

The first thing to know is f ( x ) f(x) f(x) In fact, that is x x x The approximate number of .( Every prime factor has k i k_i ki Then you can take 0 , 1 , . . . , k i 0,1,...,k_i 0,1,...,ki altogether k i + 1 k_i+1 ki+1 Sort and combine , From the unique decomposition theorem, it is obvious that the products are different ) Because the topic requires interval and , Then it is normally transformed into prefix and form a n s = ∑ i = 1 r f ( x ) − ∑ i = 1 l − 1 f ( x ) ans=\sum_{i=1}^rf(x)-\sum_{i=1}^{l-1}f(x) ans=i=1rf(x)i=1l1f(x) , set up S ( n ) = ∑ i = 1 n f ( x ) S(n) = \sum_{i=1}^nf(x) S(n)=i=1nf(x), The problem is transformed into seeking S ( n ) S(n) S(n) , That is to say seek 1 − n 1-n 1n this n n n The sum of the number of factors .

1 − n 1-n 1n There are factors i i i There are a total of ⌊ n i ⌋ \lfloor\frac ni\rfloor in , Directly use number theory to divide into blocks .

Code

int ll, rr, n;
int cal(int l, int r) {
    
	int ansr = 0, ansl = 0;
	n = rr;
    for(int l=1,r;l<=n;l=r+1){
    
        r=n/(n/l);
        ansr+=(n/l)*(r-l+1) % P;	
        ansr %= P;
    }
    n = ll;
    for(int l=1,r;l<=n;l=r+1){
    
        r=n/(n/l);
        ansl+=(n/l)*(r-l+1) % P;	
        ansl %= P;
    }
    ansr = (ansr - ansl + P) % P;
    return ansr;
}
void solve() {
    
    cin >> ll >> rr;
    ll--;
    cout << cal(ll, rr) << endl;
}

Example 2 Number theory is divided into blocks

P2303 [SDOI2012] Longge The problem of

Title Description

Given an integer n n n, You need to figure out ∑ i = 1 n gcd ⁡ ( i , n ) \sum\limits_{i=1}^n \gcd(i, n) i=1ngcd(i,n), n < 1 0 9 n<10^9 n<109

Ideas

be aware g c d ( i , n ) gcd(i,n) gcd(i,n) The maximum value of is n n n The approximate number of , So consider classification by value , And it appears g c d gcd gcd Problems often need to find a way to figure out g c d ( i , j ) = 1 gcd(i,j)=1 gcd(i,j)=1, So we change the original formula into ∑ d ∣ n d ∑ i = 1 n [ g c d ( i , n ) = d ] = ∑ d ∣ n d ∑ i = 1 n d [ g c d ( i , n d ) = 1 ] = ∑ d ∣ n d ϕ ( n d ) \sum_{d|n}d\sum_{i=1}^n[gcd(i,n)=d ]=\sum_{d|n}d\sum_{i=1}^{\frac nd}[gcd(i,\frac nd)=1]=\sum_{d|n}d\phi(\frac nd) dndi=1n[gcd(i,n)=d]=dndi=1dn[gcd(i,dn)=1]=dndϕ(dn)
You can enumerate n n n Directly find the divisor of , Time complexity O ( n ) O(\sqrt n) O(n).

Code

void solve() {
    
    cin >> n;
    ll ans = 0;
    for(ll i = 1; i * i <= n; i++) {
    
    	if(n % i == 0) {
    
	    	ans += i * phi(n/i);
	    	if(n/i!=i) {
    
	    		ans += (n/i) * phi(i);
	    	}
    	}
    }
    cout << ans << endl;
}

Mobius function

 Insert picture description here

Convolution is often used

ϵ = μ ∗ 1 , I d = ϕ ∗ 1 \epsilon=\mu *1, Id=\phi*1 ϵ=μ1,Id=ϕ1 .
namely ϵ ( n ) = ∑ d ∣ n μ ( d ) \epsilon(n) = \sum_{d|n}\mu(d) ϵ(n)=dnμ(d) . among ϵ ( n ) = 1 \epsilon(n)= 1 ϵ(n)=1 If and only if n = 1 n=1 n=1, otherwise = 0 =0 =0 .

Cited example

Given n , m n, m n,m, Find two tuples ( x , y ) (x,y) (x,y) The number of , Satisfy 1 ≤ x ≤ n 1\leq x\leq n 1xn , 1 ≤ y ≤ m 1\leq y\leq m 1ym , And g c d ( x , y ) = 1 gcd(x,y)=1 gcd(x,y)=1 . n , m ≤ 1 0 7 n,m\leq 10^7 n,m107 , at most 1 0 4 10^4 104 Group data .

Ideas

∑ x = 1 n ∑ y = 1 m [ g c d ( x , y ) = 1 ] = ∑ x = 1 n ∑ y = 1 m ϵ ( g c d ( x , y ) ) = ∑ x = 1 n ∑ y = 1 m ∑ d ∣ g c d ( x , y ) μ ( d ) \sum_{x=1}^n\sum_{y=1}^m[gcd(x,y)=1]=\sum_{x=1}^n\sum_{y=1}^m\epsilon (gcd(x,y))=\sum_{x=1}^n\sum_{y=1}^m\sum_{d|gcd(x,y)}\mu(d) x=1ny=1m[gcd(x,y)=1]=x=1ny=1mϵ(gcd(x,y))=x=1ny=1mdgcd(x,y)μ(d)
= ∑ x = 1 n ∑ y = 1 m ∑ d ∣ x , d ∣ y μ ( d ) = = ∑ d = 1 m i n ( n , m ) μ ( d ) ⌊ n d ⌋ ⌊ m d ⌋ =\sum_{x=1}^n\sum_{y=1}^m\sum_{d|x,d|y}\mu(d)==\sum_{d=1}^{min(n,m)}\mu(d)\lfloor\frac nd\rfloor\lfloor\frac md\rfloor =x=1ny=1mdx,dyμ(d)==d=1min(n,m)μ(d)dndm
Pretreatment of the above formula μ \mu μ And its prefix and , Press ⌊ n d ⌋ ⌊ m d ⌋ \lfloor\frac nd\rfloor\lfloor\frac md\rfloor dndm The value segment of , You can ask each group O ( n ) O(\sqrt n) O(n) solve the problem .

Example 3 Mobius inversion

[SDOI2015] About a number and

Title Description

set up d ( x ) d(x) d(x) by x x x The approximate number of , Given n , m n,m n,m, seek
∑ i = 1 n ∑ j = 1 m d ( i j ) \sum_{i=1}^n\sum_{j=1}^md(ij) i=1nj=1md(ij)
【 Data range 】
about 100 % 100\% 100% The data of , 1 ≤ T , n , m ≤ 50000 1\le T,n,m \le 50000 1T,n,m50000.

Ideas

First, there are conclusions :
d ( i j ) = ∑ x ∣ i ∑ y ∣ j [ g c d ( x , y ) = 1 ] d(ij)=\sum_{x|i}\sum_{y|j}[gcd(x,y)=1] d(ij)=xiyj[gcd(x,y)=1]

Then the original formula :
∑ i = 1 n ∑ j = 1 m d ( i j ) = ∑ i = 1 n ∑ j = 1 m ∑ x ∣ i ∑ y ∣ j [ g c d ( x , y ) = 1 ] = ∑ x = 1 n ∑ y = 1 m [ g c d ( x , y ) = 1 ] ⌊ n x ⌋ ⌊ m y ⌋ \sum_{i=1}^n\sum_{j=1}^md(ij)=\sum_{i=1}^n\sum_{j=1}^m\sum_{x|i}\sum_{y|j}[gcd(x,y)=1]=\sum_{x=1}^n\sum_{y=1}^m[gcd(x,y)=1]\lfloor\frac nx\rfloor\lfloor\frac my\rfloor i=1nj=1md(ij)=i=1nj=1mxiyj[gcd(x,y)=1]=x=1ny=1m[gcd(x,y)=1]xnym
Bring the conclusion of the cited example into :
= ∑ d = 1 m i n ( n , m ) μ ( d ) ∑ x = 1 ⌊ n d ⌋ ∑ y = 1 ⌊ m d ⌋ ⌊ n x d ⌋ ⌊ m y d ⌋ = ∑ d = 1 m i n ( n , m ) μ ( d ) ∑ x = 1 ⌊ n d ⌋ ∑ y = 1 ⌊ m d ⌋ ⌊ ⌊ n d ⌋ x ⌋ ⌊ ⌊ m d ⌋ y ⌋ =\sum_{d=1}^{min(n,m)}\mu(d)\sum_{x=1}^{\lfloor\frac nd\rfloor}\sum_{y=1}^{\lfloor\frac md \rfloor}\lfloor\frac n{xd}\rfloor\lfloor\frac m{yd}\rfloor=\sum_{d=1}^{min(n,m)}\mu(d)\sum_{x=1}^{\lfloor\frac nd\rfloor}\sum_{y=1}^{\lfloor\frac md \rfloor}\lfloor\frac{\lfloor\frac nd\rfloor}x\rfloor\lfloor\frac {\lfloor \frac md\rfloor}y\rfloor =d=1min(n,m)μ(d)x=1dny=1dmxdnydm=d=1min(n,m)μ(d)x=1dny=1dmxdnydm
Make f ( n ) = ∑ i = 1 n ⌊ n i ⌋ f(n)=\sum_{i=1}^n\lfloor\frac ni\rfloor f(n)=i=1nin , be
= ∑ d = 1 m i n ( n , m ) μ ( d ) f ( ⌊ n d ⌋ ) f ( ⌊ m d ⌋ ) =\sum_{d=1}^{min(n, m)}\mu(d)f(\lfloor\frac nd\rfloor)f(\lfloor\frac md\rfloor) =d=1min(n,m)μ(d)f(dn)f(dm)
also f ( n ) = ∑ i = 1 n ⌊ n i ⌋ = ∑ k = 1 n d ( k ) f(n)=\sum_{i=1}^n\lfloor\frac ni\rfloor=\sum_{k=1}^nd(k) f(n)=i=1nin=k=1nd(k)
Therefore, it can pass the Euler sieve O ( n ) O(n) O(n) Preprocessing d ( n ) d(n) d(n), By prefix and, we get f ( n ) f(n) f(n), You can ask every time O ( n ) O(\sqrt n) O(n) answer .

Code

int mu[maxn], prim[maxn], sum[maxn], cnt;
int g[maxn];
bool vis[maxn];
void get_mu(int n)
{
    
    mu[1]=1;
    for(int i=2;i<=n;i++)
    {
    
        if(!vis[i]){
    prim[++cnt]=i;mu[i]=-1;}
        for(int j=1;j<=cnt&&prim[j]*i<=n;j++)
        {
    
            vis[prim[j]*i]=1;
            if(i%prim[j]==0)break;
            else mu[i*prim[j]]=-mu[i];
        }
    }
    for(int i=1;i<=n;i++)sum[i]=sum[i-1]+mu[i];
    for(int i=1;i<=n;i++)
    {
    
        long long ans=0;
        for(int l=1,r;l<=i;l=r+1)
        {
    
            r=(i/(i/l));
            ans+=1ll*(r-l+1)*1ll*(i/l);
        }
        g[i]=ans;
    }
}
void solve() {
    
    int n, m;
    cin >> n >> m;
    int ans = 0;
    int mn = min(n, m);
    for(int l = 1, r; l <= mn; l = r + 1){
    
    	r = min(n/(n/l), m/(m/l));
        ans += (sum[r] - sum[l-1]) * g[n/l] * g[m/l];
    }
    cout << ans << endl;
}
原网站

版权声明
本文为[Strezia]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050443495827.html