【NOI2010】【BZOJ2005】能量采集(莫比乌斯反演、乱搞)
2018-01-16 22:10
591 查看
Description
click meSolution
2.1 乱搞
设f(x)=∑ni=1∑mj=1[gcd(i,j)=x],F(x)=∑ni=1∑mj=1[x|gcd(i,j)]。F(x)是很容易求的,f(x)即F(x)减去所有f(kx),(k>1),从n往1倒过来考虑,每次将其倍数的贡献减去即可。
2.2 莫比乌斯反演
可以乱搞水过的题为什么要用莫反呢?F(x)=⌊nx⌋⌊mx⌋
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; } //兰有秀兮菊有芳,怀佳人兮不能忘。 // -- 刘彻《秋风辞》
相关文章推荐
- [bzoj2005][莫比乌斯反演][Noi2010]能量采集
- bzoj 2005 [Noi2010]能量采集 O(n)莫比乌斯反演
- BZOJ 2005: [Noi2010]能量采集 [莫比乌斯反演]
- 【bzoj2005】[Noi2010]能量采集 莫比乌斯反演
- BZOJ2005: [Noi2010]能量采集 莫比乌斯反演的另一种方法——nlogn筛
- 【莫比乌斯反演】BZOJ2005 [NOI2010]能量采集
- BZOJ 2005 NOI2010 能量采集 莫比乌斯反演
- 【数论】【莫比乌斯反演】【线性筛】bzoj2005 [Noi2010]能量采集
- bzoj2005: [Noi2010]能量采集 莫比乌斯反演
- bzoj 2005: [Noi2010]能量采集【莫比乌斯反演】
- 【BZOJ2005】【NOI2010】能量采集(莫比乌斯反演,容斥原理)
- bzoj 2005: [Noi2010]能量采集 筛法||欧拉||莫比乌斯
- bzoj2005 [Noi2010]能量采集(Mobius反演,欧拉函数)
- [BZOJ2005][Noi2010]能量采集 容斥+数论
- BZOJ 2005: [Noi2010]能量采集(莫比乌斯反演)
- bzoj 2005: [Noi2010]能量采集
- 【BZOJ2005】[Noi2010]能量采集 欧拉函数
- bzoj2005 [Noi2010]能量采集
- BZOJ2005:[Noi2010]能量采集——题解
- BZOJ 2005: [Noi2010]能量采集