题目地址

题目链接

题解

注,下方\((i,j)\)均指\(gcd(i,j)\),以及证明过程有一定的跳步,请确保自己会莫比乌斯反演的基本套路。

介绍本题的\(O(n)\)和\(O(n\sqrt{n})\)做法,本题还有\(O(nlogn)\)做法,需要用到欧拉函数,或者是从质因子角度考虑也可以得到另外一个\(O(n)\)做法。

题目就是求

\[\prod_{i=1}^n\prod_{j=1}^n\frac{ij}{(i,j)^2}
\]

考虑分解一下

\[\prod_{i=1}^n\prod_{j=1}^n\frac{ij}{(i,j)^2}=\frac{\prod_{i=1}^n\prod_{j=1}^nij}{\prod_{i=1}^n\prod_{j=1}^n(i,j)^2}
\]

对于分子可得

\[\begin{aligned}
&\prod_{i=1}^n\prod_{j=1}^nij\\
&=\prod_{i=1}^ni\prod_{j=1}^nj\\
&=\prod_{i=1}^ni*n!\\
&=(n!)^{2n}
\end{aligned}
\]

对于分母,我们考虑莫比乌斯反演

\[\begin{aligned}
&\prod_{i=1}^n\prod_{j=1}^n(i,j)^2\\
&=\prod_{d=1}^nd^{2\sum_{i=1}^n\sum_{j=1}^n[(i,j)=d]}\\
&=\prod_{d=1}^nd^{2\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{d}\rfloor}[(i,j)=1]}\\
&=\prod_{d=1}^nd^{2\sum_{k=1}^{\lfloor\frac{n}{d}\rfloor}\mu(k)\lfloor\frac{n}{kd}\rfloor^2}\\
\end{aligned}
\]

至此,枚举\(d\),对指数整除分块,即可\(O(n\sqrt{n})\)解决此题。

容易发现\(\lfloor\frac{n}{d}\rfloor\)是可以整除分块的。那么怎么处理区间\([l,r]\)的\(d\)呢,将它展开,其实就是\(\frac{r!}{(l-1)!}\),由于出题人卡空间,所以可以直接计算阶乘而不是预处理(复杂度同样是\(O(n)\),每个数只会被遍历一次)

那么就可以做到\(O(n)\)解决本题了。

#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std; const int mod = 104857601;
const int p = 104857600;
const int N = 1000010; bool vis[N];
short mu[N];
int pr[N], cnt = 0;
int fac; int power(int a, int b, int Mod) {
int ans = 1;
while(b) {
if(b & 1) ans = (ll)ans * a % Mod;
a = (ll)a * a % Mod;
b >>= 1;
}
return ans % Mod;
} void init(int n) {
mu[1] = 1;
for(int i = 2; i <= n; ++i) {
if(!vis[i]) pr[++cnt] = i, mu[i] = -1;
for(int j = 1; j <= cnt && i * pr[j] <= n; ++j) {
vis[i * pr[j]] = 1;
if(i % pr[j] == 0) break;
mu[i * pr[j]] = -mu[i];
}
mu[i] += mu[i - 1];
}
fac = 1;
for(int i = 1; i <= n; ++i) fac = (ll)fac * i % mod;
} int n; int calc2(int n) {
int ans = 0;
for(int l = 1, r; l <= n; l = r + 1) {
r = n / (n / l);
ans = (ans + (ll)(n / l) * (n / l) % p * (mu[r] - mu[l - 1] + p) % p) % p;
}
return ans % p;
} int main() {
scanf("%d", &n);
init(n);
int ans = 1;
int sum = power((ll)fac * fac % mod, n, mod);
for(int l = 1, r; l <= n; l = r + 1) {
r = n / (n / l); fac = 1ll;
for(int i = l; i <= r; ++i) fac = (ll)fac * i % mod;
int t = power((ll)fac * fac % mod, calc2(n / l), mod);
ans = (ll)ans * t % mod;
}
printf("%lld\n", (ll)sum * power(ans, mod - 2, mod) % mod);
}

LuoguP5221 Product的更多相关文章

  1. uva 11059 maximum product(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK

  2. [LeetCode] Product of Array Except Self 除本身之外的数组之积

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  3. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. vector - vector product

    the inner product Givens two vectors \(x,y\in \mathbb{R}^n\), the quantity \(x^\top y\), sometimes c ...

  5. 1 Maximum Product Subarray_Leetcode

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. Leetcode Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. Where product development should start

    We all need to know our customers in order to create products they’ll actually buy. This is why the  ...

  8. [LintCode] Product of Array Except Self 除本身之外的数组之积

    Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WI ...

  9. sp_addlinkedserver '(null)' is an invalid product name

    使用SSMS 2008客户端工具逆向生成了创建链接服务器的脚本时,在测试环境执行是报如下错误:'(null)' is an invalid product name. USE [master] GO ...

随机推荐

  1. html5-label标签

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  2. D. Duff in Beach

    题意  数字串a[0---n-1], 通过不断的重复组成了 b[0,---l-1]l<10^18, 让你计算出 长度小于等于k的最长非递减子序列,满足,取得第 i 个取得是 L1 第i+1个取得 ...

  3. jQuery属性--addClass()和removeClass()

       addClass(class|fn) 概述 为每个匹配的元素添加指定的类名 参数 class  一个或多个要添加到元素中的CSS类名,请用空格分开: function(index, class) ...

  4. QScrollBar & QSlider & QDial

    [1]滚动条 & 滑块 & 表盘 Qt示例工程: (1)slidergroup.h #include <QGroupBox> QT_BEGIN_NAMESPACE clas ...

  5. python 文件描述符

    先上一张图 文件描述符是内核为了高效管理已经被打开的文件所创建的索引, ----非负整数 ----用于指代被打开的文件 ----所有执行i/o操作的系统调用都是通过文件描述符完成的 进程通过文件描述符 ...

  6. python locust 性能测试:locust安装和一些参数介绍

    安装参考 https://www.cnblogs.com/fnng/p/6081798.html <虫师大大的,很详细> ps:python3.7暂不支持locust:python3安装建 ...

  7. Linux服务器---邮件服务器dovecot

    安装dovecot Dovecot是CentOS系统中著名的POP3/IMAP服务器实现.POP3/IMAP是从邮件服务器中读取邮件时使用的协议,POP3将邮件全部从服务器中拖取下来:IMAP则每次从 ...

  8. 20165316 实验一 Java开发环境的熟悉

    实验一 Java开发环境的熟悉 基础-Java环境的构建和简单程序 实验要求 建立"自己学号exp1"的目录 在"自己学号exp1"目录下建立src,bin等目 ...

  9. Python智能检测编码并转码

    #安装包工具 $pip3 install chardet #直接打开文件,中文显示乱码 >>> import chardet >>> f = open('test. ...

  10. nfs共享文件搭建

    Linux NFS服务器的安装与配置详解 一.NFS服务简介  NFS是Network  File System(网络文件系统).主要功能是通过网络让不同的服务器之间可以共享文件或者目录.NFS客户端 ...