P3704 [SDOI2017]数字表格 题解

一道莫反套路题,其思路基本贯穿幽灵乐团

下面钦定 \(n \le m\),这样做答案不变,规定所有除法都是整除(所以不要乱约分)。

\[\prod_{i=1}^{n}\prod_{j=1}^{m}f_{\gcd(i,j)} \]

枚举 \(\gcd(i,j)\),注意这一步之后 \([gcd(i,j)=d]\) 是在指数上的。

\[\prod_{d=1}^{n}\prod_{i=1}^{n}\prod_{j=1}^{m}f_{d}^{[\gcd(i,j)=d]} \]

然后将后面两个 \(\prod\) 丢到指数上。

\[\prod_{d=1}^{n}f_d^{\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=d]} \]

然后指数上就是一个熟悉的形式(不会的见文末),得到:

\[\prod_{d=1}^{n}f_d^{\sum_{t=1}^{n/d}\mu(t) \times \frac{n}{td} \times \frac{n}{td}} \]

套路式的,枚举 \(T=td\)\(d \mid T\),同时将指数上的 \(\sum\) 拉下来,价格括号,就有:

\[\prod_{T=1}^{n}(\prod_{d \mid T}f_d^{\mu(\frac{T}{d})})^{\frac{n}{T} \times \frac{m}{T}} \]

然后中间括号括起来的这部分是可以预处理的,复杂度是调和级数 \(O(\sum_{i=1}^{n}\dfrac{n}{i})\) 也就是 \(O(n \log n)\),预处理一下然后整除分块即可。

注意指数开 long long

GitHub:CodeBase-of-Plozia

Code:

/*
========= Plozia =========
	Author:Plozia
	Problem:P3704 [SDOI2017]数字表格
	Date:2022/3/9
========= Plozia =========
*/

#include <bits/stdc++.h>

typedef long long LL;
const int MAXN = 1e6 + 5;
const LL P = 1e9 + 7;
int t, n, m, mu[MAXN], Prime[MAXN], cnt_Prime;
LL f[MAXN], sum[MAXN], inv[MAXN];
bool book[MAXN];

int Read()
{
	int sum = 0, fh = 1; char ch = getchar();
	for (; ch < '0' || ch > '9'; ch = getchar()) fh -= (ch == '-') << 1;
	for (; ch >= '0' && ch <= '9'; ch = getchar()) sum = sum * 10 + (ch ^ 48);
	return sum * fh;
}
int Max(int fir, int sec) { return (fir > sec) ? fir : sec; }
int Min(int fir, int sec) { return (fir < sec) ? fir : sec; }
LL ksm(LL a, LL b = P - 2, LL p = P)
{
	if (b == -1) b = P - 2;
	LL s = 1 % p;
	for (; b; b >>= 1, a = a * a % p)
		if (b & 1) s = s * a % p;
	return s;
}

void init()
{
	f[1] = 1; book[1] = mu[1] = 1; inv[1] = 1; int tmp = MAXN - 5;
	for (int i = 2; i <= tmp; ++i) f[i] = (f[i - 1] + f[i - 2]) % P, inv[i] = ksm(f[i]);
	for (int i = 2; i <= tmp; ++i)
	{
		if (!book[i]) { Prime[++cnt_Prime] = i; mu[i] = -1; }
		for (int j = 1; j <= cnt_Prime; ++j)
		{
			if (i * Prime[j] > tmp) break ;
			book[i * Prime[j]] = 1;
			if (i % Prime[j] == 0) { mu[i * Prime[j]] = 0; break ; }
			mu[i * Prime[j]] = -mu[i];
		}
	}
	for (int i = 1; i <= tmp; ++i) sum[i] = 1;
	for (int i = 1; i <= tmp; ++i)
	{
		if (!mu[i]) continue ;
		for (int j = i; j <= tmp; j += i)
			sum[j] = sum[j] * ((mu[i] == 1) ? f[j / i] : inv[j / i]) % P;
	}
	sum[0] = 1; inv[0] = 1;
	for (int i = 1; i <= tmp; ++i) sum[i] = sum[i - 1] * sum[i] % P;
	for (int i = 1; i <= tmp; ++i) inv[i] = ksm(sum[i]);
}

int main()
{
	t = Read(); init();
	while (t--)
	{
		n = Read(), m = Read(); LL ans = 1;
		if (n > m) std::swap(n, m);
		for (int l = 1, r; l <= n; l = r + 1)
		{
			r = Min(n / (n / l), m / (m / l));
			ans = ans * ksm(sum[r] * inv[l - 1] % P, 1ll * (n / l) * (m / l)) % P;
		}
		printf("%lld\n", ans);
	}
	return 0;
}

关于 \(\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=d]\) 的化简(第一步除法不是整除,后面的都是):

\[\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i/d,j/d)=1] \]

\[\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}[\gcd(i,j)=1] \]

\[\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}\sum_{t \mid \gcd(i,j)=1}\mu(t) \]

\[\sum_{t=1}^{n/d}\mu(t)\sum_{i=1}^{n/d}\sum_{j=1}^{n/d}[t \mid \gcd(i,j)] \]

\[\sum_{t=1}^{n/d}\dfrac{n}{td}\dfrac{m}{td} \]

这还看不懂请重修莫反以及推式子。

posted @ 2022-04-17 19:01  Plozia  阅读(30)  评论(0编辑  收藏  举报