显然当且仅当$\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的更多相关文章

  1. [BZOJ3561] DZY Loves Math VI

    (14.10.28改) 本来只想写BZOJ3739:DZY Loves Math VIII的,不过因为和VI有关系,而且也没别人写过VI的题解,那么写下. 不过我还不会插公式…… http://www ...

  2. BZOJ 3309: DZY Loves Math

    3309: DZY Loves Math Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 761  Solved: 401[Submit][Status ...

  3. 【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) ...

  4. 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较小 ...

  5. ●BZOJ 3309 DZY Loves Math

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3309 题解: 莫比乌斯反演,线筛 化一化式子: f(x)表示x的质因子分解中的最大幂指数 $ ...

  6. DZY Loves Math 系列详细题解

    BZOJ 3309: DZY Loves Math I 题意 \(f(n)\) 为 \(n\) 幂指数的最大值. \[ \sum_{i = 1}^{a} \sum_{j = 1}^{b} f(\gcd ...

  7. 【BZOJ3561】DZY Loves Math VI (数论)

    [BZOJ3561]DZY Loves Math VI (数论) 题面 BZOJ 题解 \[\begin{aligned} ans&=\sum_{i=1}^n\sum_{j=1}^m\sum_ ...

  8. 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 ...

  9. 【BZOJ3309】DZY Loves Math(莫比乌斯反演)

    [BZOJ3309]DZY Loves Math(莫比乌斯反演) 题面 求 \[\sum_{i=1}^a\sum_{j=1}^bf(gcd(a,b))\] 其中,\(f(x)\)表示\(x\)分解质因 ...

随机推荐

  1. unity3D 搞定任意ios插件

    原地址:http://www.cnblogs.com/U-tansuo/archive/2012/11/22/unity_ios-plugin.html 说起unity调ios插件,好多淫比较头痛,探 ...

  2. Clustering by fast search and find of density peaks

    参考:http://www.52ml.net/16296.html 这个算法的优点就在于,它首先一步就能找到聚类中心,然后划分类别.而其他算法需要反复迭代才能找到中心聚类. 就是不知道代码该怎么写.. ...

  3. Delphi与C语言类型转换对照

    When converting C function prototypes to Pascal equivalent declarations, it's important to substitut ...

  4. DELPHI设置枚举类型size

    delphi枚举类型长度默认为2个字节(单字),而在C中枚举为4个字节(双字),如果需要跨这两个平台编程,传输结构时会由于数据长度不一造成灾难. 经过查找资料,原来delphi可以通过{$Z+} {$ ...

  5. TortoiseSVN中图标的含义

    今天在使用svn时发现有好多不认识了,所以查了下svn帮助手册.借此总结了下 svn 中图标的含义 一个新检出的工作复本使用绿色的勾做重载.表示Subversion状态 正常. 在开始编辑一个文件后, ...

  6. MYSQL索引失效的各种情形总结

    1) 没有查询条件,或者查询条件没有建立索引  2) 在查询条件上没有使用引导列  3) 查询的数量是大表的大部分,应该是30%以上.  4) 索引本身失效 5) 查询条件使用函数在索引列上,或者对索 ...

  7. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  8. Selenium webdriver 学习总结-元素定位

    Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要 ...

  9. Java中Set集合的使用

    除了List之外,Set集合接口也经常使用,Set接口中存放的元素是无序的并且是不可重复的,因此被称为数据集: Set接口因为是无序的,所以没有提供像List一样的set方法来修改元素,查找,添加.删 ...

  10. 【动态规划】The Triangle

    问题 E: [动态规划]The Triangle 时间限制: 1 Sec  内存限制: 128 MB提交: 24  解决: 24[提交][状态][讨论版] 题目描述 73 88 1 02 7 4 44 ...