BZOJ3739 : DZY loves math VIII
显然当且仅当$\gcd(i,j)=1$时才对答案有贡献,化简得
\[\begin{eqnarray*}
ans&=&\sum_{i=1}^n\sum_{j=1}^i\mu(ij)[\gcd(i,j)=1]\\
&=&\sum_{i=1}^n\sum_{j=1}^i\mu(ij)\sum_{d|i,d|j}\mu(d)\\
&=&\sum_{i=1}^n\mu(i)\sum_{d|i}\mu(d)\sum_{j=1}^{\frac{i}{d}}\mu(dj)
\end{eqnarray*}\]
设
\[S(i,d)=\sum_{j=1}^{\frac{i}{d}}\mu(dj)\]
则
\[ans=\sum_{i=1}^n\mu(i)\sum_{d|i}\mu(d)S(i,d)\]
当且仅当$i$是square-free的时候,才对答案有贡献。此时将$i$分解质因数,然后暴力搜索所有约数,一边搜索一边更新$S(i,d)$以及$ans$即可。
#include<cstdio>
#define N 10000010
int T,n,i,j,A,B,q[1010],tot,p[700000],v[N],s[N];char mu[N];short f[N];
void dfs(int x,int y){
if(x==tot){
f[y]+=A;
if(mu[y]>0)B+=f[y];else B-=f[y];
return;
}
dfs(x+1,y*p[x]),dfs(x+1,y);
}
int main(){
scanf("%d",&T);
for(i=1;i<=T;i++){
scanf("%d",&q[i]);
if(q[i]>n)n=q[i];
}
for(mu[1]=v[1]=1,i=2;i<=n;i++){
if(!v[i])p[tot++]=v[i]=i,mu[i]=-1;
for(j=0;j<tot;j++){
if(i*p[j]>n)break;
v[i*p[j]]=p[j];
if(i%p[j])mu[i*p[j]]=-mu[i];else break;
}
}
for(i=1;i<=n;i++){
s[i]=s[i-1];
if(mu[i]){
for(tot=0,j=i;j>1;j/=v[j])p[tot++]=v[j];
A=mu[i],B=0,dfs(0,1);
if(mu[i]>0)s[i]+=B;
if(mu[i]<0)s[i]-=B;
}
}
for(i=1;i<=T;i++)printf("%d\n",s[q[i]]);
return 0;
}
BZOJ3739 : DZY loves math VIII的更多相关文章
- [BZOJ3561] DZY Loves Math VI
(14.10.28改) 本来只想写BZOJ3739:DZY Loves Math VIII的,不过因为和VI有关系,而且也没别人写过VI的题解,那么写下. 不过我还不会插公式…… http://www ...
- BZOJ 3309: DZY Loves Math
3309: DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 761 Solved: 401[Submit][Status ...
- 【BZOJ】3309: DZY Loves Math 莫比乌斯反演优化
3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...
- BZOJ 3512: DZY Loves Math IV [杜教筛]
3512: DZY Loves Math IV 题意:求\(\sum_{i=1}^n \sum_{j=1}^m \varphi(ij)\),\(n \le 10^5, m \le 10^9\) n较小 ...
- ●BZOJ 3309 DZY Loves Math
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3309 题解: 莫比乌斯反演,线筛 化一化式子: f(x)表示x的质因子分解中的最大幂指数 $ ...
- DZY Loves Math 系列详细题解
BZOJ 3309: DZY Loves Math I 题意 \(f(n)\) 为 \(n\) 幂指数的最大值. \[ \sum_{i = 1}^{a} \sum_{j = 1}^{b} f(\gcd ...
- 【BZOJ3561】DZY Loves Math VI (数论)
[BZOJ3561]DZY Loves Math VI (数论) 题面 BZOJ 题解 \[\begin{aligned} ans&=\sum_{i=1}^n\sum_{j=1}^m\sum_ ...
- BZOJ 3561 DZY Loves Math VI
BZOJ 3561 DZY Loves Math VI 求\(\sum_{i=1}^{n}\sum_{j=1}^{m}\text{lcm}(i,j)^{\gcd(i,j)}\),钦定\(n\leq m ...
- 【BZOJ3309】DZY Loves Math(莫比乌斯反演)
[BZOJ3309]DZY Loves Math(莫比乌斯反演) 题面 求 \[\sum_{i=1}^a\sum_{j=1}^bf(gcd(a,b))\] 其中,\(f(x)\)表示\(x\)分解质因 ...
随机推荐
- [Effective JavaScript 笔记]第50条:迭代方法优于循环
"懒"程序员才是好程序员.复制和粘贴样板代码,一但代码有错误,或代码功能修改,那么程序在修改的时候,程序员需要找到所有相同功能的代码一处处进行修改.这会使人重复发明轮子,而且在别人 ...
- unity3d iPhone文件目录介绍
原地址:http://cl314413.blog.163.com/blog/static/190507976201210259126559/ 如何查看iPhone文件存放目录?首先需要越狱,越狱后打开 ...
- PHP随笔
php(PHP开发) PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,易于学习,使用广泛 ...
- Flatten Binary Tree to Linked List
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...
- RadioButtonList单选和RequiredFieldValidator验证是否选中
<asp:RadioButtonList ID="Radio2" RepeatDirection="Horizontal" runat="ser ...
- c++0x新特性实例(比较常用的)
//array #include <array> void Foo() { array<> a; generate(a.begin(),a.end(),rand); sort( ...
- iOS 使用interface builder 创建太复杂的constrains时容易产生crash
今天写程序,遇到了crash,在界面初始化时不会有,想切换到别的tab页就报错了.主要内容如下: Cannot find an outgoing row head for incoming head ...
- HDU1850 Being a Good Boy in Spring Festival(博弈)
Being a Good Boy in Spring Festival Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I ...
- Cookie的使用与实现记住用户名案例
学习web开发,使用Cookie是不可避免的,个人感觉Cookie的使用和ASP.NET中的Session非常像,只不过Cookie是保存在客户端,而Session是在服务器端,两者都以记录信息为目的 ...
- Jpush推送模块
此文章已于 14:17:10 2015/3/24 重新发布到 鲸歌 Jpush推送模块 或以上版本的手机系统. SDK集成步骤 .导入 SDK 开发包到你自己的应用程序项目 • 解压 ...