其他分享
首页 > 其他分享> > BZOJ 2301: [HAOI2011]Problem b

BZOJ 2301: [HAOI2011]Problem b

作者:互联网

2301: [HAOI2011]Problem b
思路:
莫比乌斯反演+整除分块
代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb emplace_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head

const int N = 5e4 + 10;
int sum[N], mu[N], prime[N], cnt;
bool not_p[N];
void seive() {
    mu[1] = 1;
    for (int i = 2; i < N; ++i) {
        if(!not_p[i]) prime[++cnt] = i, mu[i] = -1;
        for (int j = 1; j <= cnt && i*prime[j] < N; ++j) {
            not_p[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];
}
inline LL solve(int n, int m) {
    if(n <= 0 || m <= 0) return 0;
    LL ans = 0;
    int up = min(n, m);
    for (int l = 1, r; l <= up; l = r+1) {
        r = min(n/(n/l), m/(m/l));
        ans += (n/l)*1LL*(m/l)*(sum[r]-sum[l-1]);
    }
    return ans;
}
int t, a, b, c, d, k;
int main() {
    seive();
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
        printf("%lld\n", solve(b/k, d/k)-solve((a-1)/k, d/k)-solve(b/k, (c-1)/k)+solve((a-1)/k, (c-1)/k));
    }
    return 0;
}

标签:GCC,2301,HAOI2011,pragma,pair,Problem,optimize,define
来源: https://www.cnblogs.com/widsom/p/11514274.html