1. 程式人生 > >Bzoj 2190 儀仗隊(莫比烏斯反演)

Bzoj 2190 儀仗隊(莫比烏斯反演)

那不 n) endif php () 莫比烏斯反演 long lang lld

題面

bzoj

洛谷

題解

看這個題先大力猜一波結論

#include <cstdio>
#include <cstring>
#include <algorithm>
using std::min; using std::max;
using std::swap; using std::sort;
using std::__gcd;
typedef long long ll;

template<typename T>
void read(T &x) {
    int flag = 1; x = 0; char ch = getchar();
    while(ch < ‘0‘ || ch > ‘9‘) { if(ch == ‘-‘) flag = -flag; ch = getchar(); }
    while(ch >= ‘0‘ && ch <= ‘9‘) x = x * 10 + ch - ‘0‘, ch = getchar(); x *= flag;
}

const int N = 4e4 + 10;
int n, ret;
bool a[N][N];

int main () {
#ifdef OFFLINE_JUDGE
    freopen("233.in", "r", stdin);
    freopen("233.out", "w", stdout);
#endif
    scanf("%d", &n);
    for(int i = 2; i <= n; ++i)
        for(int j = 2; j <= n; ++j) {
            int tmp = __gcd(i, j);
            int tmpi = i / tmp, tmpj = j / tmp;
            if(!a[tmpi][tmpj]) ++ret, a[tmpi][tmpj] = true;
        }
    printf("%d\n", ret + 2);		
    return 0;
}

然後:

技術分享圖片

很接近了,仔細一想,應該是:

#include <cstdio>
#include <cstring>
#include <algorithm>
using std::min; using std::max;
using std::swap; using std::sort;
using std::__gcd;
typedef long long ll;

template<typename T>
void read(T &x) {
    int flag = 1; x = 0; char ch = getchar();
    while(ch < ‘0‘ || ch > ‘9‘) { if(ch == ‘-‘) flag = -flag; ch = getchar(); }
    while(ch >= ‘0‘ && ch <= ‘9‘) x = x * 10 + ch - ‘0‘, ch = getchar(); x *= flag;
}

const int N = 4e4 + 10;
int n, ret;
bool a[N][N];

int main () {
#ifdef OFFLINE_JUDGE
    freopen("233.in", "r", stdin);
    freopen("233.out", "w", stdout);
#endif
    scanf("%d", &n);
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j) {
            int tmp = __gcd(i, j);
            if(tmp == 1) ++ret/*, a[tmpi][tmpj] = true*/;
        }
    printf("%d\n", ret);
    return 0;
}

然後過了:

技術分享圖片

那不就是$Bzoj1101\ Zap$了,直接蒯(註意特判一下$n==1$的情況)

#include <cstdio>
#include <cstring>
#include <algorithm>
using std::min; using std::max;
using std::swap; using std::sort;
typedef long long ll;

template<typename T>
void read(T &x) {
    int flag = 1; x = 0; char ch = getchar();
    while(ch < ‘0‘ || ch > ‘9‘) { if(ch == ‘-‘) flag = -flag; ch = getchar(); }
    while(ch >= ‘0‘ && ch <= ‘9‘) x = x * 10 + ch - ‘0‘, ch = getchar(); x *= flag;
}

const int N = 4e4 + 10;
int t, n, mu[N], g[N], prime[N], cnt;
long long sum[N]; bool notprime[N];

void getmu(int k) {
    mu[1] = 1;
    for(int i = 2; i <= k; ++i) {
        if(!notprime[i]) prime[++cnt] = i, mu[i] = -1;
        for(int j = 1; j <= cnt && prime[j] * i <= k; ++j) {
            notprime[prime[j] * i] = true;
            if(!(i % prime[j])) break;
            mu[prime[j] * i] = -mu[i];
        }
    }
    for(int i = 1; i <= k; ++i)
        sum[i] = sum[i - 1] + 1ll * mu[i];
}

int main () {
#ifdef OFFLINE_JUDGE
    freopen("233.in", "r", stdin);
    freopen("233.out", "w", stdout);
#endif
    getmu(40000); 
    read(n); ll ans = n > 1 ? 2 : 0; --n;
    for(int l = 1, r; l <= n; l = r + 1) {
        r = n / (n / l);
        ans += (sum[r] - sum[l - 1]) * (n / l) * (n / l);
    }
    printf("%lld\n", ans);
    return 0;
}

Bzoj 2190 儀仗隊(莫比烏斯反演)