LuoguP5221 Product
题目地址
题解
注,下方\((i,j)\)均指\(gcd(i,j)\),以及证明过程有一定的跳步,请确保自己会莫比乌斯反演的基本套路。
介绍本题的\(O(n)\)和\(O(n\sqrt{n})\)做法,本题还有\(O(nlogn)\)做法,需要用到欧拉函数,或者是从质因子角度考虑也可以得到另外一个\(O(n)\)做法。
题目就是求
\]
考虑分解一下
\]
对于分子可得
&\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}
\]
对于分母,我们考虑莫比乌斯反演
&\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的更多相关文章
- uva 11059 maximum product(水题)——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK
- [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 ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- vector - vector product
the inner product Givens two vectors \(x,y\in \mathbb{R}^n\), the quantity \(x^\top y\), sometimes c ...
- 1 Maximum Product Subarray_Leetcode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Leetcode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 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 ...
- [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 ...
- sp_addlinkedserver '(null)' is an invalid product name
使用SSMS 2008客户端工具逆向生成了创建链接服务器的脚本时,在测试环境执行是报如下错误:'(null)' is an invalid product name. USE [master] GO ...
随机推荐
- html5-css选择器
/*/**{color: red}p{color: green}#div1{background: blue;padding-top: 15px;}.kk{background: blue;borde ...
- sitecore系列教程之更改您的个人设置
在Sitecore控制面板中,您可以设置个人设置,例如密码或区域和语言选项,以使应用程序满足您的需求. 要更改您的个人设置: 在Sitecore Launchpad上,单击“ 控制面板”. 在“控制面 ...
- skynet 报错 skynet 服务缺陷 Lua死循环
我的报错如下: 看起来是skynet中lua死循环,实际上,可能只是本地配置出了问题,比如,我的数据库连接不上了,因为我把别人的配置更新到我本地了,吗,mysql秘密不对 解决办法就是将配置文件中的, ...
- webservice 生成客户端代码
使用 jdk 自带工具 wsimport wsimport -keep http://webservice/url?wsdl
- SVM支撑向量机原理
转自:http://blog.csdn.net/v_july_v/article/details/7624837 目录(?)[-] 支持向量机通俗导论理解SVM的三层境界 前言 第一层了解SVM 1分 ...
- python递归的例子
例子1:递归实现嵌套列表求和 #encoding=utf-8 a=[[1,2,3], [4,5,6], [7,8,9]]def listsum(L): result=0 for i i ...
- 前端框架VUE----组件的创建
vue的核心基础就是组件的使用,玩好了组件才能将前面学的基础更好的运用起来.组件的使用更使我们的项目解耦合.更加符合vue的设计思想MVVM. 那接下来就跟我看一下如何在一个Vue实例中使用组件吧! ...
- asyncio queue
from asyncio import Queue,sleep import asyncio from threading import Thread import time qu=Queue() # ...
- awk中截取IP字段
由于文本的特殊性,IP字段可能并不是在特定的字段中. 借助awk的match()函数进行匹配截取 awk --re-interval '($0 ~ "xxx"){match($0, ...
- python之字符编码(三)
一.字符编码的分类: 计算机由美国人发明,最早的字符编码为ASCII,只规定了英文字母数字和一些特殊字符与数字的对应关系.最多只能用 8 位来表示(一个字节),即:2**8 = 256,所以,ASCI ...