[SDOI 2017]数字表格
Description
记 $f_i$ 为 $fibonacci$ 数列的第 $i$ 项。
求 $$\prod_{i=1}^n\prod_{j=1}^mf_{gcd(i,j)}$$
对质数取模,多组询问。
$1\leq t\leq 1000,1\leq n,m\leq 10^6$
Solution
$$\begin{aligned}\Rightarrow&\prod_{d=1}^{min{n,m}}f(d)^{\sum\limits_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}[gcd(i,j)=1]}\=&\prod_{d=1}^{min{n,m}}f(d)^{\sum\limits_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}\sum\limits_{k\mid gcd(i,j)}\mu(k)}\=&\prod_{d=1}^{min{n,m}}f(d)^{\sum\limits_{k=1}^{min\left{\left\lfloor\frac{n}{d}\right\rfloor,\left\lfloor\frac{m}{d}\right\rfloor\right}}\mu(k)\left\lfloor\frac{n}{kd}\right\rfloor\left\lfloor\frac{m}{kd}\right\rfloor}\end{aligned}$$
令 $T=kd$ $$\prod_{T=1}^{min{n,m}}\prod_{d\mid T}f(d)^{\mu\left(\frac{T}{d}\right)\left\lfloor\frac{n}{T}\right\rfloor\left\lfloor\frac{m}{T}\right\rfloor}$$
我们把其中类似于狄利克雷卷积形式的东西记做 $F(T)$ $$\prod_{T=1}^{min{n,m}}F(T)^{\left\lfloor\frac{n}{T}\right\rfloor\left\lfloor\frac{m}{T}\right\rfloor}$$
那么可以枚举因子来求 $F(T)$ ,显然可以在近似于 $O(n\ln~n)$ 的时限内预处理出来。然后数论分块的复杂度为 $O(t \sqrt n)$ 。
Code
//It is made by Awson on 2018.2.22
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 1e6;
const int yzh = 1e9+7;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(LL x) {if (x < 0) putchar('-'); print(Abs(x)); }
int n, m, t, mu[N+5], f[N+5], F[N+5];
int isprime[N+5], prime[N+5], tot, inv[N+5];
int quick_pow(int a, int b) {
int ans = 1;
while (b) {
if (b&1) ans = 1ll*ans*a%yzh;
b >>= 1, a = 1ll*a*a%yzh;
}
return ans;
}
void get_pre() {
inv[1] = inv[2] = f[1] = f[2] = 1; for (int i = 3; i <= N; i++) f[i] = (f[i-1]+f[i-2])%yzh, inv[i] = quick_pow(f[i], yzh-2);
for (int i = 0; i <= N; i++) isprime[i] = F[i] = 1; isprime[1] = 0, mu[1] = 1;
for (int i = 2; i <= N; i++) {
if (isprime[i]) prime[++tot] = i, mu[i] = -1;
for (int j = 1; j <= tot && i*prime[j] <= N; j++)
if (i%prime[j] != 0) isprime[i*prime[j]] = 0, mu[i*prime[j]] = -mu[i];
else {isprime[i*prime[j]] = 0, mu[i*prime[j]] = 0; break; }
}
for (int i = 1; i <= N; i++)
for (int j = 1; j*i <= N; j++)
if (mu[j] == 1) F[i*j] = 1ll*F[i*j]*f[i]%yzh;
else if (mu[j] == -1) F[i*j] = 1ll*F[i*j]*inv[i]%yzh;
for (int i = 2; i <= N; i++) F[i] = 1ll*F[i]*F[i-1]%yzh;
}
void work() {
get_pre(); read(t);
while (t--) {
read(n), read(m); if (n > m) Swap(n, m);
int ans = 1;
for (int i = 1, last; i <= n; i = last+1) {
last = Min(n/(n/i), m/(m/i));
ans = 1ll*ans*quick_pow(1ll*F[last]*quick_pow(F[i-1], yzh-2)%yzh, 1ll*(n/i)*(m/i)%(yzh-1))%yzh;
}
writeln(ans);
}
}
int main() {
work(); return 0;
}