莫比乌斯反演例题集---(自用)

P3455 [POI2007]ZAP-Queries

求 解 ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = k ] 求解\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=k] i=1nj=1m[gcd(i,j)=k]
反 演 过 程 : 反演过程:
∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = k ] \sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=k] i=1nj=1m[gcd(i,j)=k]

∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ ε [ g c d ( i , j ) = 1 ] \sum_{i=1}^{\left \lfloor \frac{n}{k} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{k} \right \rfloor}\varepsilon[gcd(i,j)=1] i=1knj=1kmε[gcd(i,j)=1]

枚 举 d : 枚举d: d

∑ d = 1 ⌊ m i n ( ⌊ n k ⌋ , ⌊ m k ⌋ ) ⌋ ∑ d ∣ T μ ( d ) ⌊ n T ⌋ ⌊ m T ⌋ \sum_{d=1}^{\left \lfloor min(\left \lfloor \frac{n}{k} \right \rfloor,\left \lfloor \frac{m}{k} \right \rfloor)\right \rfloor}\sum_{d|T}\mu(d)\left \lfloor \frac{n}{T} \right \rfloor \left \lfloor \frac{m}{T} \right \rfloor d=1min(kn,km)dTμ(d)TnTm

T 换 k d 得 : T换kd得: Tkd

∑ d = 1 ⌊ m i n ( ⌊ n k ⌋ , ⌊ m k ⌋ ) ⌋ μ ( d ) ⌊ n k d ⌋ ⌊ m k d ⌋ \sum_{d=1}^{\left \lfloor min(\left \lfloor \frac{n}{k} \right \rfloor,\left \lfloor \frac{m}{k} \right \rfloor)\right \rfloor}\mu(d)\left \lfloor \frac{n}{kd} \right \rfloor \left \lfloor \frac{m}{kd} \right \rfloor d=1min(kn,km)μ(d)kdnkdm

先 预 处 理 莫 比 乌 斯 函 数 , 然 后 整 数 分 块 做 , 复 杂 度 为 O ( n + T n ) 先预处理莫比乌斯函数,然后整数分块做,复杂度为O(n + T\sqrt n) O(n+Tn )

Code

#include 

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pdd;

#define INF 0x7f7f7f
#define mem(a, b) memset(a , b , sizeof(a))
#define FOR(i, x, n) for(int i = x;i <= n; i++)

// const ll mod = 998244353;
// const int maxn = 1e5 + 10;
// const double eps = 1e-6;

const int N = 5e5 + 10;

int mu[N]; // 莫比乌斯函数
bool is_prime[N];
int prime[N];
int cnt;

ll sum[N]; // 记录莫比乌斯函数前缀和


void Mobi() // 莫比乌斯函数初始化
{
    mu[1] = 1;
    is_prime[0] = is_prime[1] = true;
    for(int i = 2;i < N; i++) {
        if (!is_prime[i]) {
            mu[i] = -1;
            prime[++cnt] = i;
        }
        for (int j = 1; j <= cnt && i * prime[j] < N; j++) {
            is_prime[i * prime[j]] = true;
            if (i % prime[j] == 0) {
                mu[i * prime[j]] = 0;
                break;
            }
            mu[i * prime[j]] = -mu[i];
        }
    }

    for(int i = 1;i < N; i++) {
        sum[i] = sum[i - 1] + mu[i];
    }
}

ll k;

ll Ans(ll n, ll m) {
    ll ans = 0;
    n /= k, m /= k;
    int mx = min(n, m);
    for(int l = 1, r;l <= mx; l = r + 1) {
        r = min(n / (n / l), m / (m / l));
        ans += (n / l) * (m / l) * (sum[r] - sum[l - 1]);
    }
    return ans;
}

void solve() {
    Mobi();
    int T;
    cin >> T;
    while(T--) {
        ll n, m;
        cin >> n >> m >> k;
        cout << Ans(n, m) << endl;
    }
}


signed main() {
    ios_base::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);
#ifdef FZT_ACM_LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}

P2257 YY的GCD

你可能感兴趣的:(数学)