Bzoj 2190 仪仗队(莫比乌斯反演)
题面
题解
看这个题先大力猜一波结论
#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;
}