HDU 6390 GuGuFishtion(欧拉函数+莫比乌斯反演)


GuGuFishtion

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1094    Accepted Submission(s): 405


 

Problem Description

Today XianYu is too busy with his homework, but the boring GuGu is still disturbing him!!!!!!
At the break time, an evil idea arises in XianYu's mind.
‘Come on, you xxxxxxx little guy.’
‘I will give you a function ϕ(x) which counts the positive integers up to x that are relatively prime to x.’
‘And now I give you a fishtion, which named GuGu Fishtion, in memory of a great guy named XianYu and a disturbing and pitiful guy GuGu who will be cooked without solving my problem in 5 hours.’
‘The given fishtion is defined as follow:

Gu(a,b)=ϕ(ab)ϕ(a)ϕ(b)


And now you, the xxxxxxx little guy, have to solve the problem below given m,n,p.’

(∑a=1m∑b=1nGu(a,b))(modp)


So SMART and KINDHEARTED you are, so could you please help GuGu to solve this problem?
‘GU GU!’ GuGu thanks.

 

 

Input

Input contains an integer T indicating the number of cases, followed by T lines. Each line contains three integers m,n,p as described above.
1≤T≤3
1≤m,n≤1,000,000
max(m,n)<p≤1,000,000,007
And given p is a prime.

 

 

Output

Please output exactly T lines and each line contains only one integer representing the answer.

 

 

Sample Input

1

5 7 23

 

 

Sample Output

2

 

 

Source

2018 Multi-University Training Contest 7

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6396 6395 6394 6393 6392 

 

【思路】

经过一番玄妙的转化,我们得知Gu(a, b) = \frac{gcd(a, b)}{\phi(gcd(a, b))}(注意除法是在mod P意义下的),因此我们枚举1 <= r <= min(n, m),所求就变成了求1 <= a <= n,1 <= b <= m时,满足gcd(a, b) = r的a、b数对的个数。然后就是经典的莫比乌斯反演了,由于莫比乌斯反演的过程类似于给一个调和数列求和,是O(logN)级别的,整个复杂度就变成了O(NlogN)的了。

 

【代码】

//******************************************************************************
// File Name: 1005.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: 2018年08月13日 星期一 15时11分13秒
//******************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;

const int MAXN = 1e6 + 5;

int t, m, n, mod;
vector<int> prime;
bool done[MAXN];
ll mobius[MAXN], phi[MAXN], inv[MAXN];

void get_mobius_phi(int n)
{
	memset(done, false, sizeof(done));
	memset(mobius, 0, sizeof(mobius));
	mobius[1] = phi[1] = 1;
	for (int i = 2; i < n; i++) {
		if (!done[i]) {
			prime.push_back(i);
			mobius[i] = -1;
			phi[i] = i - 1;
		}
		for (int j = 0; j < (int)prime.size() && (ll)i * prime[j] < n; j++) {
			done[i * prime[j]] = true;
			if (i % prime[j] == 0) {
				phi[i * prime[j]] = phi[i] * prime[j];
				break;
			}
			else {
				mobius[i * prime[j]] = -mobius[i];
				phi[i * prime[j]] = phi[i] * phi[prime[j]];
			}
		}
	}
}

ll solve(int p, int q)
{
	ll ans = 0;
	for (int i = 1; i <= min(p, q); i++)
		ans = (ans + (ll)mobius[i] * (p / i) * (q / i)) % mod;
	return ans;
}

int main()
{
	get_mobius_phi(MAXN);
	scanf("%d", &t);
	while (t--) {
		scanf("%d %d %d", &m, &n, &mod);
		if (n >= m) swap(n, m);
		inv[1] = 1;
		for (int i = 2; i <= n; i++)
			inv[i] = (mod - mod / i) * inv[mod % i] % mod;
		ll ans = 0;
		for (int i = 1; i <= n; i++)
			ans = (ans + (ll)i * solve(n / i, m / i) % mod * inv[phi[i]] % mod) % mod;
		printf("%lld\n", ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值