【NOI2010】【BZOJ2005】能量采集(莫比乌斯反演、乱搞)

Description

click me

Solution

2.1 乱搞

f(x)=ni=1mj=1[gcd(i,j)=x]F(x)=ni=1mj=1[x|gcd(i,j)]
F(x)是很容易求的,f(x)F(x)减去所有f(kx),(k>1),从n1倒过来考虑,每次将其倍数的贡献减去即可。

2.2 莫比乌斯反演

可以乱搞水过的题为什么要用莫反呢?
F(x)=nxmx
f(x)=x|dF(d)μ(dx)
直接反演即可。。

Code

3.1

/**************************
 * Au: Hany01
 * Date: Jan 16th, 2018
 * Prob: bzoj2005 & noi2010 LuanGao
 * Email: hany01@foxmail.com
**************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void File()
{
#ifdef hany01
    freopen("bzoj2005.in", "r", stdin);
    freopen("bzoj2005.out", "w", stdout);
#endif
}

const int maxn = 100005;

int n, m;
LL g[maxn], f[maxn], Ans;

int main()
{
    File();
    if ((n = read()) > (m = read())) swap(n, m);
    For(i, 1, n) g[i] = (n / i) * 1ll * (m / i);
    Fordown(i, n, 1) {
        f[i] = g[i];
        for (register int j = i << 1; j <= n; j += i) f[i] -= f[j];
        Ans += f[i] * i;
    }
    For(i, 1, n) cout << f[i] << ' ' ;
    cout << endl;
    cout << (Ans << 1) - n * 1ll * m << endl;
    return 0;
}
//看朱成碧思纷纷,憔悴支离为忆君。
//    -- 武则天《如意娘》

3.2

/**************************
 * Au: Hany01
 * Date: Jan 16th, 2018
 * Prob: bzoj2005 & noi2010 Mobius
 * Email: hany01@foxmail.com
**************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void File()
{
#ifdef hany01
    freopen("bzoj2005.in", "r", stdin);
    freopen("bzoj2005.out", "w", stdout);
#endif
}

const int maxn = 100005;

int n, m, mu[maxn], pr[maxn >> 1], cnt, np[maxn];
LL Ans, f[maxn];

inline void Get_mu()
{
    mu[1] = 1;
    For(i, 2, n) {
        if (!np[i]) pr[++ cnt] = i, mu[i] = -1;
        for (register int j = 1; j <= cnt && i * pr[j] <= n; ++ j) {
            np[i * pr[j]] = 1;
            if (!(i % pr[j])) { mu[i * pr[j]] = 0; break; }
            mu[i * pr[j]] = -mu[i];
        }
    }
}

inline LL F(LL x) { return (n / x) * (m / x); }
int main()
{
    File();
    if ((n = read()) > (m = read())) swap(n, m);
    Get_mu();
    For(i, 1, n) for (int j = 1; j * i <= n; ++ j)
        Ans += mu[j] * 1ll * F(i * 1ll * j) * ((i << 1) - 1);
    cout << Ans << endl;
    return 0;
}
//兰有秀兮菊有芳,怀佳人兮不能忘。
//    -- 刘彻《秋风辞》
发布了393 篇原创文章 · 获赞 410 · 访问量 7万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览