HDU 2841 容斥 或 反演

$n,m <= 1e5$ ,$i<=n$,$j<=m$,求$(i⊥j)$对数

 

/** @Date    : 2017-09-26 23:01:05
  * @FileName: HDU 2841 容斥 或 反演.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

LL pri[N];
LL phi[N];
LL sum[N];
LL mu[N];
int c = 0;
void prime()
{
	MMF(phi);
	phi[1] = 1;
	mu[1] = 1;
	for(int i = 2; i < N; i++)
	{
		if(!phi[i]) pri[c++] = i, phi[i] = i - 1, mu[i] = -1;
		for(int j = 0; j < c && i * pri[j] < N; j++)
		{
			phi[i * pri[j]] = 1;
			if(i % pri[j] == 0)
			{
				phi[i * pri[j]] = phi[i] * pri[j];
				mu[i * pri[j]] = 0;
				break;
			}
			else phi[i * pri[j]] = phi[i] * (pri[j] - 1), mu[i * pri[j]] = -mu[i];
		}
	}
	sum[0] = 0;
	for(int i = 1; i < N; i++)
		sum[i] = sum[i - 1] + mu[i];
} 
int main()
{
	prime();
	int T;
	cin >> T;
	while(T--)
	{
		LL n, m;
		cin >> n >> m;
		int mi = min(n, m);
		LL ans = 0;
		for(int i = 1, last; i <= mi; i = last + 1)
		{
			last = min((n/(n/i)) ,(m/(m/i)));
			ans += (n / i) * (m / i) * (sum[last] - sum[i - 1]);
		}
		cout << ans << endl;
	}
    return 0;
}
posted @ 2017-09-27 00:19  Lweleth  阅读(159)  评论(0编辑  收藏  举报