Rinne Loves Sequence

Posted H-w-H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rinne Loves Sequence相关的知识,希望对你有一定的参考价值。

Rinne Loves Sequence


推式子

∑ i = 1 n ∑ j = i + 1 n [ g c d ( a i , a j ) = 1 ] ∑ i = 1 n ∑ j = 1 n [ g c d ( i , j ) = 1 ] × c n t i × c n t j − ∑ i = 1 n [ g c d ( i , i ) = 1 ] ∑ d = 1 n μ ( d ) ∑ i = 1 n ∑ j = 1 n c n t i × c n t j ∑ d = 1 n μ ( d ) ∑ i = 1 n d ∑ j = 1 n d c n t i d × c n t j d \\sum_{i=1}^n\\sum_{j=i+1}^n[gcd(a_i, a_j)=1]\\\\ \\sum_{i=1}^n\\sum_{j=1}^n[gcd(i,j)=1]\\times cnt_i\\times cnt_j-\\sum_{i=1}^n[gcd(i,i)=1]\\\\ \\sum_{d=1}^n\\mu(d)\\sum_{i=1}^n\\sum_{j=1}^ncnt_i\\times cnt_j\\\\ \\sum_{d=1}^n\\mu(d)\\sum_{i=1}^\\frac nd\\sum_{j=1}^\\frac ndcnt_{id}\\times cnt_{jd}\\\\ i=1nj=i+1n[gcd(ai,aj)=1]i=1nj=1n[gcd(i,j)=1]×cnti×cntji=1n[gcd(i,i)=1]d=1nμ(d)i=1nj=1ncnti×cntjd=1nμ(d)i=1dnj=1dncntid×cntjd

解释

添加一个数产生的贡献是:找到当前这个数的所有因子,用 μ ( d ) × c n t d \\mu(d)\\times cnt_d μ(d)×cntd表示当前这个数对应的 d d d产生的贡献。

同理删除一个数的贡献就是减去 μ ( d ) × c n t d \\mu(d)\\times cnt_d μ(d)×cntd


C o d e Code Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int long long
const int N = 5e5+10;
int p[N], tot, mu[N];
bool st[N];
vector<int> fac[N];
void init() {
    mu[1] = 1;
    for(int i=2; i<N; i++) {
        if(!st[i]) p[tot++] = i, mu[i] = -1;
        for(int j=0; j<tot&&i*p[j]<N; j++) {
            st[i*p[j]] = 1;
            if(i % p[j] == 0) break;
            mu[i*p[j]] = -mu[i];
        }
    }
    for(int i=1; i<N; i++) {
        for(int j=i; j<N; j+=i) {
            fac[j].push_back(i);
        }
    }
}
int vis[N], num[N];
signed main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    init();
    ll n, ans = 0;
    scanf("%lld", &n);
    for(int i=1; i<=n; i++) {
        int f, x;
        cin >> f >> x;
        if(f == 1) {
            if(vis[x]) continue;
            vis[x] = 1;
            for(auto it : fac[x]) {
                ans += mu[it] * num[it];
                num[it]++;
            }
        }
        else if(f == 2) {
            if(!vis[x]) continue;
            vis[x] = 0;
            for(auto it : fac[x]) {
                num[it]--;
                ans -= mu[it] * num[it];
            }
        }    
        else {
            cout << ans << endl;
        }
    }
    return 0;
}

以上是关于Rinne Loves Sequence的主要内容,如果未能解决你的问题,请参考以下文章

Rinne Loves Edges

Rinne Loves Edges

NC 22598. Rinne Loves Edges

5805 NanoApe Loves Sequence

5806 NanoApe Loves Sequence Ⅱ(尺取法)

BC#86 1003NanoApe Loves Sequence Ⅱ[two-pointer]