这里简述一下莫比乌斯函数:
若d=1 那么μ(d)=1
若d=p1p2…pr (r个不同质数,且次数都为一)μ(d)=(-1)^r
其余 μ(d)=0
题目传送:HDU - 1695 - GCD
题意:求[1,n],[1,m]中gcd为k的两个数的对数
思路:这里可以转化一下,也就是[1,n/k],[1,m/k]之间互质的数的个数,模板题
AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;
const int maxn = 1000005;
bool vis[maxn];
int prime[maxn];
int mu[maxn];
int tot;
void init() {
memset(vis, 0, sizeof(vis));
mu[1] = 1;
tot = 0;
for(int i = 2; i < maxn; i ++) {
if(!vis[i]) {
prime[tot ++] = i;
mu[i] = -1;
}
for(int j = 0; j < tot; j ++) {
if(i * prime[j] >= maxn) break;
vis[i * prime[j]] = true;
if(i % prime[j] == 0) {
mu[i * prime[j]] = 0;
break;
}
else {
mu[i * prime[j]] = -mu[i];
}
}
}
}
int main() {
init();
int T;
int a, b, c, d, k;
scanf("%d", &T);
for(int cas = 1; cas <= T; cas ++) {
scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
if(k == 0) {
printf("Case %d: 0\n", cas);
continue;
}
b /= k;
d /= k;
if(b > d) swap(b, d);
LL ans = 0;
for(int i = 1; i <= b; i ++) {
ans += (LL)mu[i] * (b / i) * (d / i);
}
LL t = 0;
for(int i = 1; i <= b; i ++) {
t += (LL)mu[i] * (b / i) * (b / i);
}
ans -= t / 2;
printf("Case %d: %I64d\n", cas, ans);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u014355480/article/details/47347923